Forum Moderators: coopster

Message Too Old, No Replies

insert query with implode multidimensional array

something's not quite right - SOS

         

deejay

3:55 am on Feb 19, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi All

I've got a checkbox form where users can select multiple records to be inserted into a table.

My insert query keeps throwing an error though, and I've gone positively cross-eyed trying to figure out why. Can you help?

Error message:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '6','10624'),('6','10623')')' at line 2

my processing page code is as follows:

$array = $_POST['array'];
$insertvalues=array();

foreach($array as $key => $value) {
print "$key: $value[p] $value[b]<br>";
if(!$value[b]){
}
else
{
$insertvalues[]="('{$value['p']}','{$value['b']}')";
}
}

if(count($insertvalues >0)){
$query = mysql_query("INSERT INTO projectblockyear (projectid, blkyr)
VALUES('".implode(",",$insertvalues)."')")
or die (mysql_error());

--------------------------------------------------

print_r($insertvalues) produces:
Array ( [0] => ('6','10624') [1] => ('6','10623') )

deejay

9:30 am on Feb 19, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Progress! Of a sort.

Query now reads:

$query = mysql_query("INSERT INTO projectblockyear (projectid, blkyr) VALUES(".implode(",",$insertvalues).")")
or die (mysql_error());

... but produces a new error:

Operand should contain 1 column(s)

What I've found indicates that this is a mismatch between the number of table fields I've specified and the number of insert values? Any suggestions?

deejay

9:52 am on Feb 19, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Got it!

$query = mysql_query("INSERT INTO `projectblockyear` (projectid, blkyr) VALUES ".implode(",",$insertvalues)."")
or die (mysql_error());