Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP Array insert into MySQL table as individual rows
    primarykey
    data
    text
    <p>I am trying to insert multiple rows in a MySQL table from PHP arrays. I managed with with help of other members to get set of values in a pair of brackets but when i try to insert this i get "<strong>Error: Column count doesn't match value count at row 1</strong>" I donot know where am i going wrong. my codes are as below: (The number of values i get vary according to user input)</p> <pre><code> $docno1=array(); $serialno = array(); $acc_name = array(); $debit = array(); $credit = array(); for ($i=1;$i&lt;=$rowcount;$i++) { //echo 'Accountname'.$i.' :'.($_GET['accname'.$i]).'&lt;br&gt;'; $docno1 [] = ($_GET['docno']); array_unshift($docno1,""); unset($docno1[0]); $serialno [] = $i; array_unshift($serialno,""); unset($serialno[0]); $acc_name[] = ($_GET['accname'.$i]); array_unshift($acc_name,""); unset($acc_name[0]); $debit[] = ($_GET['DrAmount'.$i]); array_unshift($debit,""); unset($debit[0]); $credit[] = ($_GET['CrAmount'.$i]); array_unshift($credit,""); unset($credit[0]); } $sum_dr = array_sum ($debit); $sum_cr = array_sum ($credit); echo ' values of $multi&lt;br&gt;'; $multi = array( ($docno1), ($serialno), //Array for a row of fields ($acc_name), ($debit), ($credit), ($docno1) ); print_r($multi); $new = array(); foreach($multi as $key=&gt;$value) { $new[] = "'".implode("','", $value)."'"; } echo '&lt;br&gt;Values of $new &lt;br&gt;'; print_r($new); $query = "(".implode("), (",$new).")"; echo $query.'&lt;br&gt;'; mysql_query("INSERT INTO docitems (`docno`,`itemno`,`accountname`,`debit`,`credit`, `picrefno`) VALUES ".$query.";") or die('Error: ' . mysql_error()); echo "Inserted successfully"; die; </code></pre> <p><strong>The results i get are :</strong></p> <pre><code> values of $multi Array ( [0] =&gt; Array ( [1] =&gt; 3434 [2] =&gt; 3434 ) [1] =&gt; Array ( [1] =&gt; 1 [2] =&gt; 2 ) [2] =&gt; Array ( [1] =&gt; Lemon [2] =&gt; Kidney Beans ) [3] =&gt; Array ( [1] =&gt; 20 [2] =&gt; 10 ) [4] =&gt; Array ( [1] =&gt; 0 [2] =&gt; 0 ) [5] =&gt; Array ( [1] =&gt; 3434 [2] =&gt; 3434 ) ) Values of $new Array ( [0] =&gt; '3434','3434' [1] =&gt; '1','2' [2] =&gt; 'Lemon','Kidney Beans' [3] =&gt; '20','10' [4] =&gt; '0','0' [5] =&gt; '3434','3434' ) ('3434','3434'), ('1','2'), ('Lemon','Kidney Beans'), ('20','10'), ('0','0'), ('3434','3434') Error: Column count doesn't match value count at row 1 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload