使用php表单在数据库中插入多行

时间:2014-12-04 01:04:06

标签: php mysql forms

我创建了一个表单,可以在input field的帮助下为product_name的{​​{1}}和quantity添加更多product,这就是 demo 您可以在表单中添加更多输入字段。

问题是,当我提交表单时,只有最后jquery将提交到我的数据库中,其余产品将不会提交。

这是我的查询

product

我的表格

<?php
  if(isset($_POST['submit'])){
     //process the form

     $date = $_POST["date"];
     $customer_name = $_POST["customer_name"];
     $product_description = $_POST["product_description"];
     $quantity = $_POST["quantity"];
     $status = $_POST["status"];

     $query  = "
     INSERT INTO orders (
     date, customer_name, product_description, quantity, status   
     ) VALUES (
     '$date', '$customer_name', '$product_description',$quantity,$status
     )";

     $order_set = mysqli_query($connection, $query);
     if($order_set){
       redirect_to("index.php");
     }

  } else {
    // failed

  }


?>

任何机构都知道如何提交<form action="order.php" method="post"> <div class="newOrder"> <p><span>Date</span><input type="date" value="2014-12-01" name="date" /></p> <p><span>Name</span> <select name="customer_name"> <?php while($customer = mysqli_fetch_assoc($customers_set)){ ?> <option><?php echo $customer['customer_name']; ?></option> <?php } ?> <?php mysqli_free_result($customers_set); ?> </select> </p> <div id="input_fields"> <p><span>Product Description</span> <select name="product_description"> <?php while($product = mysqli_fetch_assoc($product_set)){ ?> <option><?php echo $product['product_description']; ?></option> <?php } ?> <?php mysqli_free_result($product_set); ?> </select> <input value="0" type="text" name="quantity" /> </p> </div> <a href="#" class="more">Add More Product</a> <p class="radio"> <input type="radio" name="status" value="0" checked />For delivery&nbsp;&nbsp; <input type="radio" name="status" value="1" />For payment confirmation&nbsp;&nbsp; <input type="radio" name="status" value="2" />Reserved items&nbsp;&nbsp; </p> <input type="submit" name="submit" value="Create Order" /> </div> </form> 中的所有productquantity输入将保存在数据库中。

4 个答案:

答案 0 :(得分:0)

将您的值包装在数据库连接中。从我的一个旧课程中考虑这一点。注意是一个不同的代码,但工作正常。

 $first_name = $_POST['firstname'];
 $last_name = $_POST['lastname'];
 $when_it_happened = $_POST['whenithappened'];
 $how_long = $_POST['howlong'];
 $how_many = $_POST['howmany'];
 $alien_description = $_POST['aliendescription'];
 $what_they_did = $_POST['whattheydid'];
 $fang_spotted = $_POST['fangspotted'];
 $email = $_POST['email'];
 $other = $_POST['other'];

 $dbc = mysqli_connect('data.aliensabductedme.com', 'owen', 'aliensrool', 'aliendatabase')
  or die('Error connecting to MySQL server.');

 $query = "INSERT INTO aliens_abduction (first_name, last_name, when_it_happened, how_long, " .
"how_many, alien_description, what_they_did, fang_spotted, other, email) " .
"VALUES ('$first_name', '$last_name', '$when_it_happened', '$how_long', '$how_many', " .
"'$alien_description', '$what_they_did', '$fang_spotted', '$other', '$email')";

$result = mysqli_query($dbc, $query)
or die('Error querying database.');

mysqli_close($ DBC);

答案 1 :(得分:0)

输入字段是否具有相同的名称?所以我猜这就是为什么只插入最后一个。

您必须循环添加的INSERT查询foreach产品,包括数量。

在将输入值插入查询之前,您应该对输入值进行清理。

答案 2 :(得分:0)

你需要使用foreach

foreach ($_POST['quantity'] as $quantity) {
//insert code
}

答案 3 :(得分:0)

当您插入多个查询时,您不应该从php循环中执行此操作。效率不高,因为您正在执行多个查询而不是一个或两个。您可以循环从表单发送的结果,清理它并准备数据库插入,然后根据该结果构建查询。在这里查看一次将多行插入数据库:

Insert multiple rows with one query MySQL