View Full Version : saving image path to db..

11-16-2009, 01:06 PM
Guys, would someone please show me how to save an image path (url) to a database. I have the images saved in a folder inside the file system. I would like to send information including the image path to the databse and have it retrieved with a pagination script. This is the code I'm using to send to the database. All the info I send to the db gets retrieved minus the image. Thanks

$con = mysql_connect("localhost","root","root"); //Replace with your actual MySQL DB Username and Password
if (!$con)
die('Could not connect: ' . mysql_error());
mysql_select_db("testing", $con); //Replace with your MySQL DB Name
$name=mysql_real_escape_string($_POST['name']); //This value has to be the same as in the HTML form file
$email=mysql_real_escape_string($_POST['email']); //This value has to be the same as in the HTML form file
$subject=mysql_real_escape_string($_POST['subject']); //This value has to be the same as in the HTML form file
$sql="INSERT INTO news (name,email,subject) VALUES ('$name','$email','$subject')"; /*form_data is the name of the MySQL table where the form data will be saved.
name and email are the respective table fields*/
if (!mysql_query($sql,$con)) {
die('Error: ' . mysql_error());
echo "The form data was successfully added to your database.";

11-16-2009, 05:20 PM
I usually have a field in my DB for just the filename.

Then I hard code the path to the file and just echo out the filename.

Mywebsite/Images/<?php echo $myfile; ?>

You could just add the extra field to your INSERT statement like this.

$sql="INSERT INTO news (name,email,subject,file) VALUES ('$name','$email','$subject', $file)";

Hope this helps.

Regards Paul

11-16-2009, 11:46 PM
Thanks Paul, I will try and get back..