Multi Insert to Database From a Dir

Discussion in 'PHP' started by ruud, Jul 7, 2007.

  1. #1
    I want to insert this games and images, these have same names. I couldnt find where I mistake. Please write me some ideas. I'm waiting for good ideas. Thanks. I will rep who solve my problem. :)

    This is my code, where is mistake?

    
    $dir='mydir'; 
    $open=opendir($dir); 
    while ($file=readdir($open)) { 
        if (is_file($file)) { 
            $file=substr($file,0,-4); 
            mysql_query("insert into oyunlar (id, name, image, catid, hit, categori, url) values ('', '$file', 'mydir/$file.jpg', '1', '0', 'Sport', 'mydir/$file.swf')"); 
      
     } 
    } 
    closedir($open); 
    Code (markup):

     
    ruud, Jul 7, 2007 IP
  2. ErsinAcar

    ErsinAcar Peon

    Messages:
    201
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Can you show me the error?
     
    ErsinAcar, Jul 7, 2007 IP
  3. ansi

    ansi Well-Known Member

    Messages:
    1,483
    Likes Received:
    65
    Best Answers:
    0
    Trophy Points:
    100
    #3
    trying checking to see if the directory is opened to start with.
    
    if( $open = opendir($mydir) )
    {
        //it's open
    }
    else
    {
        // error
    }
    
    PHP:
     
    ansi, Jul 7, 2007 IP
  4. ruud

    ruud Peon

    Messages:
    308
    Likes Received:
    16
    Best Answers:
    0
    Trophy Points:
    0
    #4
    ErsinAcar, there is no error text on the screen from php.

    ansi, i controlled if it was opened or not as you say. it opened correctly. But it was not inserted into database again.
     
    ruud, Jul 7, 2007 IP
  5. TheBorg

    TheBorg Peon

    Messages:
    144
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #5
    After the line:
    $file=substr($file,0,-4);
    PHP:
    put:
    echo $file."<br>";
    PHP:
    to see what happens.

    Add:
    
    or die(mysql_error());
    PHP:
    behind your query to see why it fails.
     
    TheBorg, Jul 7, 2007 IP
  6. ansi

    ansi Well-Known Member

    Messages:
    1,483
    Likes Received:
    65
    Best Answers:
    0
    Trophy Points:
    100
    #6
    try something like this...
    
    <?php
    $dir = "mydir";
    if ($handle = opendir($dir))
    {
    	while (false !== ($file = readdir($handle)))
    	{
    		if ($file != "." && $file != "..")
    		{
    			$sql = "insert into tbl1(foo,bar) values('".$file."','".sprintf("%.02f",(filesize($file) / 1024))." kb')";
    			// send query : $result = mysql_query($sql); blah blah
    		}
    	}
    }
    else
    	die("unable to open ".$dir);
    
    ?>
    
    PHP:
     
    ansi, Jul 7, 2007 IP
  7. ruud

    ruud Peon

    Messages:
    308
    Likes Received:
    16
    Best Answers:
    0
    Trophy Points:
    0
    #7
    TheBorg, I did you say. But screen is same again. It didnt list files opened.

    ansi, there is no instering and no error..
     
    ruud, Jul 7, 2007 IP
  8. ansi

    ansi Well-Known Member

    Messages:
    1,483
    Likes Received:
    65
    Best Answers:
    0
    Trophy Points:
    100
    #8
    did you edit it to put what you want where you want and actually send the query? in my example, it's just that an example with ficticious information.
     
    ansi, Jul 7, 2007 IP
  9. ruud

    ruud Peon

    Messages:
    308
    Likes Received:
    16
    Best Answers:
    0
    Trophy Points:
    0
    #9
    I solve my problem. I see my mistake.

    Mistake ; I put in same file name jpg and swf to a folder. Example ; xxx.jpg and xxx.swf

    So it took 2 same file name "xxx", so it didnt wors.

    Now i put only swf files to folder, and run php file. it was done. :)

    thanks for all opinions.

    Reps added. :)
     
    ruud, Jul 7, 2007 IP
  10. ansi

    ansi Well-Known Member

    Messages:
    1,483
    Likes Received:
    65
    Best Answers:
    0
    Trophy Points:
    100
    #10
    glad i could help. if i did. hehe
     
    ansi, Jul 7, 2007 IP