问题是将多个数据插入到db中

时间:2017-09-12 18:25:01

标签: php mysql mysqli

我在获取记录以插入多个数据时遇到问题。它只想插入最后一项。我在while循环中quantityitem_nameamountitem number。因此,如果有3个项目,我需要将所有3个项目与用户信息lanIdemployee_namedepartmentcost_center一起插入。

action.php的

<form action="test.php" method="post">';      

                      $uid = $_SESSION["uid"];
                      $sql = "SELECT * FROM cart WHERE user_id = '$uid'";
                      $run_query = mysqli_query($con,$sql);
                      while($row=mysqli_fetch_array($run_query)){
                          $x++;
                     echo  

                     '<br>'.'<input type="text" name="item_name" value="'.$row["product_title"].'">
                      <input type="text" name="quantity" value="'.$row["qty"].'">
                      <input type="text" name="amount" value="'.$row["price"].'">
                      <input type="text" name="item_number" value="'.$x.'">';
                    }


                      echo"<br>
                                 <label>Lan ID</label>
                                    <input type='text' name='lanId' id='lanId'  autocomplete='off'  class='form-control' >
                                <label>Employee Name</label>
                                    <input type='text' name='employee_name' id='name'   autocomplete='off'  class='form-control'>
                                <label>Department</label>
                                    <select name='department' id='department'  class='form-control'>
                                        <option value =''>Select Department...</option>
                                        <option value ='OTHER'> OTHER</option>

                                    </select>

                            <label>Cost Center</label>
                            <input type='text' class='form-control' name='cost_center' value=''>
                            <label>Total amount</label>
                            <input type='text' class='form-control' name='total_amt' value='$total_amt'>
                            <br><br><br>
                            <input type='submit' class='btn btn-primary' value='Submit'>

                        </form>";

test.php的

    <?php
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
include('db.php');

$item_name = $_POST["item_name"];
$amount = $_POST["amount"];
$quantity = $_POST['quantity'];
$lanId = $_POST["lanId"];
$employee_name = $_POST["employee_name"];
$department = $_POST['department'];
$cost_center = $_POST['cost_center'];
$total_amt = $_POST['total_amt'];

$sql = "INSERT INTO `order` 
        ( `item_name`, `amount`, `quantity`, 
        `lanId`, `employee_name` , `department`, `cost_center`, `total_amt`) 
        VALUES ('$item_name', '$amount', '$quantity', 
        '$lanId', '$employee_name', '$department', '$cost_center','$total_amt')";
        $run_query = mysqli_query($con,$sql);
        if($run_query){
            echo "
                <div class='alert alert-success'>
                    <a href='http://a0319p528/project2/profile.php' class='close' data-dismiss='alert' aria-label='close'>&times;</a>
                <b>data inserted successfully..!</b>
                </div>
            ";
        }

        ?>

1 个答案:

答案 0 :(得分:2)

第一个问题是您只从表单中获取最后一个输入,因为您在循环的每次迭代中都会覆盖它们。您可以通过name="amount[]"将它们视为&#34; HTML数组&#34;。这将使$_POST['amount']成为一个数组,而不是一个字符串,然后您可以在PHP中提交和处理表单时循环。

<input type="text" name="item_name[]" value="'.$row["product_title"].'">
<input type="text" name="quantity[]" value="'.$row["qty"].'">
<input type="text" name="amount[]" value="'.$row["price"].'">
<input type="text" name="item_number[]" value="'.$x.'">';

此外,您应该使用预准备语句而不是常规查询,原因有两个

  1. 它将在内部处理所有引用问题,因此您不必担心它。这反过来又会阻止SQL注入(安全性改进)
  2. 您将能够使用不同的值多次执行相同的查询
  3. 使用准备好的声明,您可以将查询归结为下面显示的内容。这将循环遍历具有多个值的元素(如上所示)并为每次迭代运行查询。

    $lanId = $_POST["lanId"];
    $employee_name = $_POST["employee_name"];
    $department = $_POST['department'];
    $cost_center = $_POST['cost_center'];
    $total_amt = $_POST['total_amt'];
    
    $stmt = $con->prepare("INSERT INTO `order` (`item_name`, `amount`, `quantity`, `lanId`, `employee_name` , `department`, `cost_center`, `total_amt`)
                                                VALUES (?, ?, ?, ?, ?, ?, ?, ?)");
    foreach ($_POST['item_name'] as $key=>$item_name) {
        $amount = $_POST['amount'][$key];
        $quantity = $_POST['quantity'][$key];
    
        $stmt->bind_param("ssssssss", $item_name, $amount, $quantity, $lanId, $employee_name, $department, $cost_center, $total_amt);
        $stmt->execute();
    }
    if ($stmt->affected_rows) {
        echo "
                <div class='alert alert-success'>
                    <a href='http://a0319p528/project2/profile.php' class='close' data-dismiss='alert' aria-label='close'>&times;</a>
                <b>data inserted successfully..!</b>
                </div>
            ";
    }
    $stmt->close();