I'm trying to insert multiple rows into MySql with only one INSERT INTO
statement using the implode
function. It was found from here.
My sample code goes below.
$sql[]=array();
$len=count($colour_id);
for($i=0;$i<$len;$i++)
{
$sql[]='('.$colour_id[$i].', '.$_POST['prod_id'].')';
}
$l=count($sql);
foreach($sql as $temp)
{
echo $temp;
}
echo 'insert into product_colour (colour_id, prod_id)values '.implode(',', $sql);
The above code simply initializes the $sql
array and the foreach
loop iterates over the array and displays the content of the array as follows.
Array(1, 1)(2, 1)(3, 1)
but while echoing the last statement (insert statement), it shows the following error.
Notice: Array to string conversion in C:\wamp\www\wagafashion\ProductColour.php on line 70
insert into product_colour (colour_id, prod_id)values Array,(1, 1),(2, 1),(3, 1)
(line no 70 means the last line in the above code snippet). What changes should be made so that I can insert the values stored in the array into MySql database?