View Full Version : MySQL adding variable

02-23-2009, 11:56 PM
Hi first post here.
This is my first real experience with PHP and MySQL. I have the following code which allows me to put information from a form into my database.
However I'm having trouble importing one thing. Here is my code.


$sql="INSERT INTO items (Name, Description, Price, Button, pic)

if (!mysql_query($sql,$con))
die('Error: ' . mysql_error());
echo "Thankyou, You have just added $_POST[item] to the database";

I want it to post the file name of the image but also with the "../upload/uploads/"on the beginning of it.
All other items enter fine into the database but i get this error
does anyone have any ideas?
Thanks all

02-24-2009, 12:43 AM
Can you attach the file showing the error? I am blocked from getting to imageshack. Or just copy/paste the text of the error.

Old Pedant
02-24-2009, 12:44 AM
Not a PHP person, at all, but just looking at the pattern you have there I would say you are simply missing the surrounding apostrophes:

$sql="INSERT INTO items (Name, Description, Price, Button, pic)
VALUES ('$item','$description','$price','$button','../upload/uploads/$picture2')";

02-24-2009, 12:44 AM
Nevermind, I see your problem-- each string in the query needs to be enclosed in single quotes; you're missing single quotes around the last string.

$sql="INSERT INTO items (Name, Description, Price, Button, pic)

Old Pedant
02-24-2009, 12:46 AM
Fumigator: Just says there's a syntax error in the SQL

near '/upload/uploads)'


Added: LOL! Too little too late! Story of my life.

Old Pedant
02-24-2009, 12:49 AM
FWIW: You could also solve it thus:

$picture2="../upload/uploads/" . $_FILES['uploadedfile']['name'];

$sql="INSERT INTO items (Name, Description, Price, Button, pic)
VALUES ('$item','$description','$price','$button','$picture2')";

Assuming I am understanding what's going on in the PHP code.

02-24-2009, 01:05 AM
Thanks guys worked like a charm :D