Jump to content

Can't insert array into mysql query


sabinmash

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
https://forums.phpfreaks.com/topic/250186-cant-insert-array-into-mysql-query/
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.")";	

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.