如何将多行插入数据库PHP MYSQL

时间:2018-01-24 11:38:59

标签: javascript php arrays mysqli

我的发票输入需要帮助

我需要一些帮助,使用php将不同值的多行插入数据库。我已经有按钮插入新行

这是我的插入查询:

  $insert     = "INSERT INTO subtrans (idproduk,namaproduk,harga,qty,sub) 
  VALUES";
      for ($i=0; $i < $count; $i++) {
        $insert .= "('{$idproduk[$i]}','{$namaproduk[$i]}','{$harga[$i]}','{$qty[$i]}','{$sub[$i]}')";
        $insert .= ",";
      }
    $insert = rtrim($insert,",");
    $sql = $con->query($insert);

继承我的HTML代码:

<tbody>
                  <tr>
                    <td><input class="case" type="checkbox"/></td>
                    <td><input type="text" data-type="idproduk" name="itemNo[]" id="itemNo_1" class="form-control form-control-sm autocomplete_txt" autocomplete="off"></td>
                    <td><input type="text" data-type="namaproduk" name="itemName[]" id="itemName_1" class="form-control form-control-sm autocomplete_txt" autocomplete="off"></td>
                    <td><input type="number" name="price[]" id="price_1" class="form-control form-control-sm changesNo" autocomplete="off" onkeypress="return IsNumeric(event);" ondrop="return false;" onpaste="return false;"></td>
                    <td><input type="number" name="quantity[]" id="quantity_1" class="form-control form-control-sm changesNo" autocomplete="off" onkeypress="return IsNumeric(event);" ondrop="return false;" onpaste="return false;"></td>
                    <td><input type="number" name="total[]" id="total_1" class="form-control form-control-sm totalLinePrice" autocomplete="off" onkeypress="return IsNumeric(event);" ondrop="return false;" onpaste="return false;"></td>
                  </tr>
                </tbody>

这是我的js代码:

//adds extra table rows
var i=$('table tr').length;
$(".addmore").on('click',function(){
 html = '<tr>';
 html += '<td><input class="case" type="checkbox"/></td>';
 html += '<td><input type="text" data-type="idproduk" name="itemNo[]" id="itemNo_'+i+'" class="form-control form-control-sm autocomplete_txt" autocomplete="off"></td>';
 html += '<td><input type="text" data-type="namaproduk" name="itemName[]" id="itemName_'+i+'" class="form-control form-control-sm autocomplete_txt" autocomplete="off"></td>';
 html += '<td><input type="text" name="price[]" id="price_'+i+'" class="form-control form-control-sm changesNo" autocomplete="off" onkeypress="return IsNumeric(event);" ondrop="return false;"onpaste="return false;"></td>';
 html += '<td><input type="text" name="quantity[]" id="quantity_'+i+'" class="form-control form-control-sm changesNo" autocomplete="off" onkeypress="return IsNumeric(event);" ondrop="return false;"  onpaste="return false;"></td>';
 html += '<td><input type="text" name="total[]" id="total_'+i+'" class="form-control form-control-sm totalLinePrice" autocomplete="off" onkeypress="return IsNumeric(event);" ondrop="return false;"  onpaste="return false;"></td>';
 html += '</tr>';
 $('table').append(html);
 i++;
 });

1 个答案:

答案 0 :(得分:0)

仅仅为了记录我不相信这是解决这个特定问题的最佳方法 - 更好的方法是使用一次准备好的prepared statement然后在循环中执行多次使用不同的每次都是值。但是,以下生成可行的sql(无论如何都在测试中)

$insert = "insert into `subtrans` ( `idproduk`, `namaproduk`, `harga`, `qty`, `sub` ) values ";
$params = array();

foreach( $_POST['itemName'] as $i => $value ){
    $params[]="'". implode( "','", 
        array( 
                $_POST['itemNo'][ $i ],
                $_POST['itemName'][ $i ],
                $_POST['price'][ $i ],
                $_POST['quantity'][ $i ],
                $_POST['total'][ $i ]
            )
        ) ."'";
}
$sql = sprintf("%s ( %s )", $insert, implode( '),(', $params ) );

echo $sql;
$result = $con->query( $sql );

如我所提到的,更好的方法是多次执行prepared statement

$sql='insert into `subtrans` ( `idproduk`, `namaproduk`, `harga`, `qty`, `sub` ) values (?,?,?,?,?)';
$stmt=$con->prepare( $sql );
if( $stmt ){
    $stmt->bind_param('ssddd',$id,$name,$price,$qty,$total);

    foreach( $_POST['itemName'] as $i => $value ){
        $id=$_POST['itemNo'][ $i ];
        $name=$_POST['itemName'][ $i ];
        $price=$_POST['price'][ $i ];
        $qty=$_POST['quantity'][ $i ];
        $total=$_POST['total'][ $i ];

        $stmt->execute()
    }
}