vet911 Posted September 13, 2014 Share Posted September 13, 2014 I am having trouble trying to save my image file names to mysql after reading a dir. I am trying to use the file below. Any help would be appreciated. I was originallly getting the files to list but they we not showing up in the database. Now the page shows up blank. <?php require("config_0.php"); // Connect to server and select database. mysql_connect($dbhost, $dbuser, $dbpass)or die("cannot connect"); mysql_select_db("test")or die("cannot select DB"); $files = glob("images/*.jpg"); for ($i=1; $i<count($files); $i++) { $num = $files[$i]; $sql="INSERT INTO images (url) VALUES ('$num')"; if (!mysql_query($sql)) { die('Error: ' . mysql_error()); } echo '<img src="'.$num.'" alt="random image">'." "; } ?> Quote Link to comment Share on other sites More sharing options...
ginerjm Posted September 13, 2014 Share Posted September 13, 2014 If you turn on php error checking you might see your errors. If you are getting a blank page you may have errors. You might also read the manual about these functions. Note the special RED highlighted paragraphs that tell you not to use them. BTW - your logic on the loop is flawed. You're missing the last file with this code. Also - you are saving a filename but you do not necessarily have the path to the files. If you do a glob for "*.*" you get the files in the directory where the script is run from. But once you store them in your table you have no reference to where the files are located. Quote Link to comment Share on other sites More sharing options...
QuickOldCar Posted September 13, 2014 Share Posted September 13, 2014 In addition are missing the mysql_query() so not saving them to database $sql=mysql_query("INSERT INTO images (url) VALUES ('$num')"); if (!$sql) { die('Error: ' . mysql_error()); } Quote Link to comment Share on other sites More sharing options...
vet911 Posted September 13, 2014 Author Share Posted September 13, 2014 Okay, I added error checking and the mysql_query and now I' getting a listing of the file names and the pictures show up. I am still not getting anything in the mysql database. No error messages are showing up. I think the problem in in the sql INSERT statement. I've listed the program below. <?php error_reporting(E_ALL | E_NOTICE); ini_set('display_errors', '1'); require("config_0.php"); // Connect to server and select database. mysql_connect($dbhost, $dbuser, $dbpass)or die("cannot connect"); mysql_select_db("test")or die("cannot select DB"); $files = glob("image/*.jpg"); for ($i=1; $i<count($files); $i++) { $num = $files[$i]; echo "$files[$i]"; echo "<br/>"; $sql=mysql_query("INSERT INTO images (url) VALUES ('$num')"); if (!$sql) { die('Error: ' . mysql_error()); } echo '<img src="'.$num.'" alt="random image">'." "; } ?> Quote Link to comment Share on other sites More sharing options...
ginerjm Posted September 13, 2014 Share Posted September 13, 2014 You had the query call In your original code. Don't know what other reader was looking at. Methinks you are not looking at your db correctly when checking it. Quote Link to comment Share on other sites More sharing options...
CroNiX Posted September 13, 2014 Share Posted September 13, 2014 Since $files is an array, $i should start at 0, not 1 in the for() loop. You're skipping the first image. Please also note that the mysql extension is deprecated, meaning it will no longer be available in php in the future. If you want to write code that will work on future versions of php without having to rewrite all of your queries, it's highly suggested you use the PDO or mysqli extension. Quote Link to comment Share on other sites More sharing options...
vet911 Posted September 13, 2014 Author Share Posted September 13, 2014 I use phpMyAdmin to view the database, it doesn't put anything in the columns except id which is generated. img_id, image, url are all zeros. Any help would be appreciated and or thoughts to correct this. Quote Link to comment Share on other sites More sharing options...
jcbones Posted September 13, 2014 Share Posted September 13, 2014 What does your database structure look like. You could be sending the database data that it isn't expecting for that column. Quote Link to comment Share on other sites More sharing options...
vet911 Posted September 13, 2014 Author Share Posted September 13, 2014 Here is a picture of the database structure. Quote Link to comment Share on other sites More sharing options...
Solution jcbones Posted September 13, 2014 Solution Share Posted September 13, 2014 Change the url column to a varchar(30). The image might also need to be a varchar, although I don't know what you are using it for.FYI int is for integers, which are whole numbers 0-9. This is why you are getting all 0's. Quote Link to comment Share on other sites More sharing options...
vet911 Posted September 13, 2014 Author Share Posted September 13, 2014 Thanks for the info, that was the problem with the database, completely my error before I started. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.