PHP 数组作为单独的行插入 MySQL 表中

发布于 2024-11-27 11:13:23 字数 2587 浏览 2 评论 0原文

我正在尝试从 PHP 数组插入 MySQL 表中的多行。我在其他成员的帮助下设法在一对括号中获取一组值,但是当我尝试插入此值时,我收到“错误:列计数与第 1 行的值计数不匹配”不知道我哪里错了。我的代码如下:(我得到的值的数量根据用户输入而变化)

    $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;

我得到的结果是:

      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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

最后的乘客 2024-12-04 11:13:23
 mysql_query("INSERT INTO docitems (`docno`,`itemno`,`accountname`,`debit`,`credit`, `picrefno`) VALUES ".$query.";") or die('Error: ' . mysql_error());

您尝试将某些内容插入 6 个字段,因此 $query 字符串中必须有 6 个值,否则您会收到此错误。

您有很多包含 2 个值的 $query。这不是 6

 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

情话难免假 2024-12-04 11:13:23

在我看来,好像您正在以错误的方式映射数组。您尝试添加两条记录,每条记录有 6 个字段,但实际放入 SQL 语句中的是 6 条记录,每条记录有两个字段。

这就是 MySQL 抱怨的原因——因为你告诉它你想要更新六个字段,但在你给它的每条记录中,你只指定了两个字段。

您需要以不同的方式构建阵列。

我假设 $docno1$serialno$acc_name$debit$credit 将始终具有相同数量的数组元素(从您的代码看来您正在假设这一点,所以我将按照您的假设进行操作)。

在这种情况下,您需要构建如下所示的数组:

$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])
}

用此替换代码中设置 $multi 的块,您的程序应该可以工作。

看看 print_r($multi) 现在的样子,您就会发现其中的差异。

(注意,有比这更有效的方法来编写整个程序,但我专注于为您提供这个特定位的直接替代品,以帮助您向您展示哪里出了问题,而不是简单地重写整个程序适合您的计划)

希望这有帮助。

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.

人│生佛魔见 2024-12-04 11:13:23

如果在尝试向表中插入行时发生错误,请尝试在插入查询中指定字段列表 - 这样,values 子句中的数据数量将与该数量匹配预期列数。

否则,MySQL 需要六列:它需要特定的插入——您没有为其指定值。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文