Results 1 to 7 of 7

Thread: MySQL Error - data not going into database!

  1. #1
    Join Date
    Jun 2007
    Posts
    72
    Thanks
    3
    Thanked 3 Times in 3 Posts

    Default MySQL Error - data not going into database!

    Hi, I have made a script that scans a folder for pictures and then is supposed to insert the pictures into a database but it isnt inserting the data and I dont know why. Here is the code I am using:
    PHP Code:
    <?
    $host
    ="*****"// Host name
    $username="*****"// Mysql username
    $password="*****"// Mysql password
    $db_name="*****"// Database name
    $tbl_name="*****"// Table name

    // Connect to server and select database.
    mysql_connect("$host""$username""$password")or die("cannot connect server ");
    mysql_select_db("$db_name")or die("cannot select DB");

    $files = array();
        
    // Define the full path to the folder whose contents you want to list
    $path ".";
            
    //File Types allowed
    $pattern="\.(jpg|jpeg|png|gif|bmp)$";    
            
    // Open the directory
    $dir_handle = @opendir($path) or die("Error opening $path");
        
    // Loop through the files
        
    while ($file readdir($dir_handle)) {
        if(
    eregi($pattern$file))

    //Prints the pictures  
    echo "<a href=\"$file\">$file</a><br />";
      
    $sql="INSERT INTO $tbl_name (file_name, path_-_large, path_-_thumbnail)VALUES('$file', 'http://www.calumogg.co.uk/landscapes/thumbs/$file', 'http://www.calumogg.co.uk/landscapes/thumbs/$file')";
    $result=mysql_query($sql);

    }

    // Close
    closedir($dir_handle);
    ?>
    It is printing the pictures fine, but nothing else. Any Help would be greatly appreciated.
    Last edited by calumogg; 10-11-2007 at 05:27 PM.

  2. #2
    Join Date
    Jun 2007
    Posts
    72
    Thanks
    3
    Thanked 3 Times in 3 Posts

    Default

    Does anyone have any idea about this?
    Sorry to be push but I really need this script to work.

  3. #3
    Join Date
    Oct 2005
    Posts
    255
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    do you get a mysql error or any error when you goto that page...?

    post mysql error.
    Hey new design new look, goto xudas for personal webdsign help.. (:

  4. #4
    Join Date
    Jul 2006
    Location
    just north of Boston, MA
    Posts
    1,806
    Thanks
    13
    Thanked 72 Times in 72 Posts

    Default

    put a clause on the query, so you will know if it errors out
    Code:
    if( !mysql_query($sql) )
         print $result = "error occured ". mysql_error();
    else
         print $result = "successfully inserted file";
    also by enclosing a variable inside a quotes it will be read as a string.


    and if there is an error in the query I am going to guess its on path_-_large, path_-_thumbnail are those the EXACT field names?

  5. #5
    Join Date
    Jun 2007
    Posts
    72
    Thanks
    3
    Thanked 3 Times in 3 Posts

    Default

    No it doesnt say that I am getting any errors, the picture list is still being displayed, but the data just isnt going into the database. Here is a link to the file http://calumogg.co.uk/landscapes/thumbs/test.php in action

  6. #6
    Join Date
    Jun 2007
    Posts
    72
    Thanks
    3
    Thanked 3 Times in 3 Posts

    Default

    Thanks for the reply, I have changed the database field names and it added the data, but it has added two of everything, and not just the jpg files...

    Edit:
    I just removed the code that shows whats going on on the MySQL side and it added one of everything, but still other files are being added it.

  7. #7
    Join Date
    Jun 2007
    Posts
    72
    Thanks
    3
    Thanked 3 Times in 3 Posts

    Default

    Iv sorted it so that only the jepgs are added into the data base, but is there anyway that I can order the pictures by filename before the data is added?
    Here is the latest version of the code I am using:
    PHP Code:
    <?
    $host
    ="*****"// Host name
    $username="*****"// Mysql username
    $password="*****"// Mysql password
    $db_name="*****"// Database name
    $tbl_name="*****"// Table name

    // Connect to server and select database.
    mysql_connect("$host""$username""$password")or die("cannot connect server ");
    mysql_select_db("$db_name")or die("cannot select DB");

    // Define the full path to the folder whose contents you want to list
    $path ".";

    //File Types allowed
    $pattern="\.(jpg|jpeg)$";

    // Open the directory
    $dir_handle = @opendir($path) or die("Error opening $path");

    // Loop through the files and filter out no jpgs
        
    while(false !== ($file readdir($dir_handle))){
                   if(
    eregi($pattern$file)){

    //Prints the list of pictures
        
    echo "<a href=\"$file\">$file</a><br />";

    //Insert data into database
    $sql="INSERT INTO $tbl_name (file_name, large, thumbnail)VALUES('$file', 'http://www.calumogg.co.uk/landscapes/thumbs/$file', 'http://www.calumogg.co.uk/landscapes/thumbs/$file')";
        
    $result=mysql_query($sql);

    }
    }

    // Close
    closedir($dir_handle);

    ?>

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •