Insert MySQL Query not running

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Red Blaze
Forum Commoner
Posts: 40
Joined: Mon Mar 27, 2006 3:45 pm

Insert MySQL Query not running

Post by Red Blaze »

Code: Select all

for ($i = 0; $i < count($filename); $i++)
	{
	mysql_query("INSERT INTO carts (itemname,userid) VALUES (". $filename[$i] .",". $userid .")");
	}
It's a loop I'm running, but it doesn't want to work properlly. $filename is a series a checkboxes with the name filename[]. I may be doing something wrong in the mysql_query because nothing gets stored. Where did I do the typo? I tried quotes/no quotes on the variables for $filename[$i] and $userid, but I still don't get any results. No errors, no information stored in the database. Thank you.

~RB
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Is $filename[$i] always a number?

Code: Select all

...
mysql_query("INSERT INTO carts (itemname,userid) VALUES (". $filename[$i] .",". $userid .")") or die(mysql_error());
...
Red Blaze
Forum Commoner
Posts: 40
Joined: Mon Mar 27, 2006 3:45 pm

Post by Red Blaze »

feyd wrote:Is $filename[$i] always a number?

Code: Select all

...
mysql_query("INSERT INTO carts (itemname,userid) VALUES (". $filename[$i] .",". $userid .")") or die(mysql_error());
...
After more research, it wasn't suppose to be in quotes. It was suppose to be '$filename[$i]','$userid'.

filename is not a number, it's actually a filename. Userid is always a number, though.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

there's your problem then.

Code: Select all

...
mysql_query("INSERT INTO `carts` (`itemname`, `userid`) VALUES ('" . mysql_real_escape_string($filename[$i]) ."', '". intval($userid) ."')") or die(mysql_error()); 
...
Post Reply