简体   繁体   English

使用php在mysql中插入多个记录

[英]Insertion of multiple records in mysql using php

I have been trying to insert more than one records into my database table, but all i get is zeros(0) inserted into the fields. 我一直试图在数据库表中插入多个记录,但是我得到的只是插入字段中的zeros(0)。 Below is what I tried. 下面是我尝试过的。 I tried using the foreach loop for insertion, but its not working for me. 我尝试使用foreach循环进行插入,但对我而言不起作用。 There any easy way to do it.Thanks 有任何简单的方法可以做到。

The html HTML

<form method="post" action="data_handlers/purchase_entry_process.php">
    <table>
    <thead>
    <tr class="titlerow">
    <th>Item name</th>
    <th>Quantity</th>
    <th>Purchase Price</th>
    <th>Amount</th>
    <th>Description</th>
    </tr>
    </thead>
        <tr>
        <td class="col-md-3"><select name="item_code[]">
        <option></option>
        <?php
        $tempholder = array();
        $query = $link->query("SELECT * FROM items");
        $nr = mysqli_num_rows($query);
        for($i=0; $i<$nr; $i++) {
        $row = mysqli_fetch_array($query);
        if(!in_array($row['item_code'],$tempholder)) 
        {
        echo"<option value=\"{$row['item_code']}\" >{$row['item_name']}</option>";
        }
        }  
        ?>
        </select></td>
        <td><input type="text" name="qty"></td>
        td><input type="text" name="purchase_price"></td>
        <td><input type="text" name="sub_total"></td>
        <td><input type="text" name="description"></td>                     
        </tr>
        <tr>
        <td class="col-md-3"><select name="item_code[]">
        <option></option>
        <?php
        $tempholder = array();
        $query = $link->query("SELECT * FROM items");
        $nr = mysqli_num_rows($query);
        for($i=0; $i<$nr; $i++) {
        $row = mysqli_fetch_array($query);
        if(!in_array($row['item_code'],$tempholder)) 
        {
        echo"<option value=\"{$row['item_code']}\" >{$row['item_name']}</option>";
        }
        }  
        ?>
        </select></td>
        <td><input type="text" name="qty"></td>
        td><input type="text" name="purchase_price"></td>
        <td><input type="text" name="sub_total"></td>
        <td><input type="text" name="description"></td>                     
        </tr>
    </table>
    </form>

php PHP

$item_code = $_POST['item_code'];
$qty = $_POST['qty'];
$pur_price = $_POST['purchase_price'];
$sub_total = $_POST['sub_total'];
$desc = $_POST['description'];

foreach($item_code as $item_codes);
$insert_inventory = $link->query("INSERT INTO inventory VALUES ('NULL','$item_codes','$qty','$pur_price','$qty','$desc','$sub_total')");

To get the quantity, price, sub_total and description from all the fields, you should make them arrays like you did with item_code. 要从所有字段中获取数量,价格,小计和描述,应像使用item_code一样将它们设置为数组。

<td><input type="text" name="qty[]"></td>
<td><input type="text" name="purchase_price[]"></td>
<td><input type="text" name="sub_total[]"></td>
<td><input type="text" name="description[]"></td>  

For processing the PHP and make it ready for the query you could do something akin to 为了处理PHP并使其准备好进行查询,您可以执行类似于

<?php
    // Prepare the query
    $query = "INSERT INTO inventory VALUES ";

    // Go through all the entries
    foreach ($_POST['item_code'] as $key => $value) {

        // If an item has been selected from the drop-down
        // we process it for the query.
        if (!empty($value) && $value != "") {
            $query .= ($key > 0 ? ", " : "");
            $query .= "(NULL, '" . $value . "', '" . $_POST['qty'][$key] . "', '" . $_POST['purchase_price'][$key] . "', '" . $_POST['sub_total'][$key] . "', '" . $_POST['description'][$key] . "')";
        }
    }

    // Example output from $query
    // INSERT INTO inventory VALUES (NULL, '1', '1', '11', '111', '1111'), (NULL, '2', '2', '22', '222', '2222')
?>

I would recommend throwing in some escaping of the variables as well, to prevent SQL injections. 我建议也对变量进行一些转义 ,以防止SQL注入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM