dptdb84606 2016-01-09 23:18 采纳率: 0%
浏览 54

如何使用php将多条记录插入到mysql中

i am fetching different record from the database but using single quantity field trying to insert multiple records into MYSQL but every time for loop or any other loop overriding single value in all rows(inserting single first value in every field), badly stuck. kindly suggest what kind of appropriate steps should be done.

<input type="text" name="qty[]" id="qtyid" style="width:40px;">

<?php   
if(isset($_POST["update"])) {
  $usersCount=count($_POST['qty']);

  $qtys=implode(",",$_POST['qty']);
  for($i=0;$i<$usersCount;$i++) {
    $query="UPDATE cart set qty='".$qtys."' WHERE prodid='$cartid'";
    $dbh->query($query);
    echo $qtys; 
  }
}
$total=$total*$qtys;
?>

database tuple image

  • 写回答

1条回答 默认 最新

  • du131642 2016-01-10 00:25
    关注

    Your "question" is unclear. I read it couple times nad i still dont know what is your problem.

    Anyway. I'll try to explain what your code do in case it would help you to understand where the problem is.

    //if form was submited and input of name "update" was in
    if(isset($_POST["update"])) {
    
      // count array created by inputs of name="qty[]"
      $usersCount=count($_POST['qty']);
    
      // make a string from that array, separating the keys by a "," char
      $qtys=implode(",",$_POST['qty']);
    
      // loop as many times as number of elements in that array = <input name="qty[]"> you had in submited form
      for($i=0;$i<$usersCount;$i++) {
    
        // and finally, do always the same for each loop repeat
        $query="UPDATE cart set qty='".$qtys."' WHERE prodid='$cartid'";
        $dbh->query($query);
        echo $qtys; 
      }
    }
    

    So in conclusion your loop is doing always the same - every each repeat:

    `$query="UPDATE cart set qty='".$qtys."' WHERE prodid='$cartid'";`
    

    Basically to do that you would't need to have loop. Because in that query you change value of column qty wherever column prodid is equal to something

    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法