...

View Full Version : MySQL adding variable



danhann9
02-23-2009, 10: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.


$item="$_POST[item]";
$price="$_POST[price]";
$description="$_POST[description]";
$picture2=$_FILES['uploadedfile']['name'];


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

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
http://img407.imageshack.us/my.php?image=print.jpg
does anyone have any ideas?
Thanks all

Fumigator
02-23-2009, 11:43 PM
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-23-2009, 11:44 PM
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')";

Fumigator
02-23-2009, 11:44 PM
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)
VALUES
('$item','$description','$price','$button','../upload/uploads/$picture2')";

Old Pedant
02-23-2009, 11:46 PM
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-23-2009, 11:49 PM
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.

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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum