Jump to content

Can't insert array into mysql query


sabinmash
 Share

Recommended Posts

I have tried many ways of storing an array of data that comes from a multiple selection form into a mysql table, including serialization.  I am currently trying to do it by forming a string, and using the string in the insert query, but keep getting errors, or just nothing happening.  I am not sure what I am doing wrong. 

 

$categoryString = array();	

if ($categoryArray){
		 foreach ($categoryArray as $category){
			$categoryString[] =  $category.'<br />';}
}
$categoryquery= "INSERT INTO categoryname VALUES('$categoryString')";	

mysql_query($categoryquery);

 

Thank you for your time as always.

Link to comment
Share on other sites

Ah silly me, didn't even name the table.

 

I have what seems like the right syntax now.  I can echo out what looks like the correct VALUES syntax, that should be going into the db, but still nothing happens.  If I take away the concatenation of the string, inside the values parameters, I just get a database category name with 4 words in it, inside of having them each in their own cell.

 

	if ($categoryArray){
		 foreach ($categoryArray as $category){
			$categoryString .= "'$".$category."',";
		}
}		
echo $categoryString;

$categoryquery = "INSERT INTO category (categoryname) VALUES(".$categoryString.")";	

Link to comment
Share on other sites

This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.