douhei8633 2011-07-31 20:04
浏览 38
已采纳

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

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
  • 写回答

3条回答 默认 最新

  • doubi7346 2011-07-31 20:38
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序
  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入
  • ¥20 XP系统在重新启动后进不去桌面,一直黑屏。
  • ¥15 opencv图像处理,需要四个处理结果图
  • ¥15 无线移动边缘计算系统中的系统模型
  • ¥15 深度学习中的画图问题
  • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条