Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6212779
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T06:34:44+00:00 2026-05-24T06:34:44+00:00

I am trying to insert multiple rows in a MySQL table from PHP arrays.

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T06:34:45+00:00Added an answer on May 24, 2026 at 6:34 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to insert multiple rows using SqlCommand from C# to SQL Server. I'm
So I am trying to insert multiples rows of data from one table to
I want to insert mutiple rows in mysql table using php. Data is in
I am trying to INSERT INTO a table using the input from another table.
I'm an SQL newbie and trying to figure out how to insert multiple table
I am trying to insert multiple records into a table where each record has
I am trying to select some fields from one table and insert them into
I am trying to insert 2 rows into the same table. The first will
I'm using an INSERT .. SELECT to 'duplicate' rows from a table (with a
I'm trying to insert multiple rows but I continue getting an sqlerror and cannot

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.