Log in

View Full Version : MySQL Error - data not going into database!



calumogg
10-09-2007, 02:59 PM
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:


<?
$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.

calumogg
10-11-2007, 05:27 PM
Does anyone have any idea about this?
Sorry to be push but I really need this script to work.

insanemonkey
10-11-2007, 05:33 PM
do you get a mysql error or any error when you goto that page...?

post mysql error.

boogyman
10-11-2007, 05:34 PM
put a clause on the query, so you will know if it errors out


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?

calumogg
10-11-2007, 05:36 PM
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

calumogg
10-11-2007, 05:41 PM
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.

calumogg
10-12-2007, 09:37 AM
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:

<?
$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);

?>