user837168
user837168

Reputation: 51

PHP Array insert into MySQL table as individual rows

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 "Error: Column count doesn't match value count at row 1" I donot know where am i going wrong. my codes are as below: (The number of values i get vary according to user input)

    $docno1=array();
    $serialno = array();
    $acc_name = array();
    $debit = array();
    $credit = array();

    for ($i=1;$i<=$rowcount;$i++)
    {
    //echo 'Accountname'.$i.' :'.($_GET['accname'.$i]).'<br>';
    $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<br>';
    $multi = array(
    ($docno1),
    ($serialno), //Array for a row of fields
    ($acc_name),
    ($debit),
    ($credit),
    ($docno1)

    );

    print_r($multi);

    $new = array();
    foreach($multi as $key=>$value) {
    $new[] = "'".implode("','", $value)."'";

    }
    echo '<br>Values of $new <br>';
    print_r($new);

    $query = "(".implode("), (",$new).")";
    echo $query.'<br>';


    mysql_query("INSERT INTO docitems (`docno`,`itemno`,`accountname`,`debit`,`credit`, `picrefno`) VALUES ".$query.";") or die('Error: ' . mysql_error());


    echo "Inserted successfully";
    die;

The results i get are :

      values of $multi
      Array
      (
      [0] => Array
      (
      [1] => 3434
      [2] => 3434
      )

      [1] => Array
      (
      [1] => 1
      [2] => 2
      )

      [2] => Array
      (
      [1] => Lemon
      [2] => Kidney Beans
      )

      [3] => Array
      (
      [1] => 20
      [2] => 10
      )

      [4] => Array
      (
      [1] => 0
      [2] => 0
      )

      [5] => Array
      (
      [1] => 3434
      [2] => 3434
      )

      )

      Values of $new 
      Array
      (
      [0] => '3434','3434'
      [1] => '1','2'
      [2] => 'Lemon','Kidney Beans'
      [3] => '20','10'
      [4] => '0','0'
      [5] => '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

Upvotes: 1

Views: 2339

Answers (3)

Spudley
Spudley

Reputation: 168695

It looks to me as if you are mapping your array the wrong way round. You're trying to add two records with six fields each, but what you're actually putting into the SQL statement are six records with two fields each.

This is why MySQL is complaining -- because you've told it you want to update six fields, but in each of the records you've given it, you've only specified two fields.

You need to build your array differently.

I assume that $docno1, $serialno, $acc_name, $debit and $credit will always all have the same number of array elements (it appears from your code that you are assuming this, so I'll follow you in your assumption).

In that case, you need to build your array something like this:

$multi = array();
foreach($docno1 as $key=>value) {
    $multi[] = array(
        $docno1[$key],
        $serialno[$key], //Array for a row of fields
        $acc_name[$key],
        $debit[$key],
        $credit[$key],
        $docno1[$key])
}

Replace the block in your code where you set $multi with this, and your program should work.

Look at what print_r($multi) looks like now, and you'll see the difference.

(note, there are more efficient ways of writing your whole program than this, but I've focused on giving you a drop-in replacement for this specific bit, to help show you where you were going wrong, rather than simply rewriting the whole program for you)

Hope this helps.

Upvotes: 0

Garrett Smallwood
Garrett Smallwood

Reputation: 389

If the error is occurring when trying to insert a row to your table, try specifying the list of fields, in the insert query -- this way, the number of data in the values clause will match the number of expected columns.

Else, MySQL expects six columns : it expects the specific inserts -- for which you didn't specify a value.

Upvotes: 0

Nanne
Nanne

Reputation: 64409

 mysql_query("INSERT INTO docitems (`docno`,`itemno`,`accountname`,`debit`,`credit`, `picrefno`) VALUES ".$query.";") or die('Error: ' . mysql_error());

You are trying to insert something into 6 fields, so that $query string must have 6 values in it, or you get this error.

You have a lot of $query's that are 2 values. And that's not 6

Upvotes: 5

Related Questions