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

时间:2015-02-14 14:07:12

标签: javascript php mysql ajax

我一直在尝试将多个记录插入到我的数据库表中,但我得到的只是在字段中插入零(0)。以下是我的尝试。 我尝试使用foreach循环进行插入,但它不适用于我。有任何简单的方法可以做到。谢谢

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

$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')");

1 个答案:

答案 0 :(得分:1)

要从所有字段中获取数量,价格,sub_total和描述,您应该像使用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>  

为了处理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')
?>

我建议同时使用escaping of the variables,以防止SQL注入。