2014-12-04 100 views
2

插入多行中的數據庫中,我創建了可以添加product_nameproductjquery的幫助下quantityinput field一種形式,這是demo,您可以在表單中添加更多的輸入字段。使用PHP形式

問題是當我提交表格時,只有最後一個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輸入input field將保存在數據庫中。

+1

我想你只需要構建值列表在一個循環,然後建立並執行整個查詢 – Strawberry 2014-12-04 01:10:11

+0

感謝對你的建議,但我不知道從哪裏開始,因爲我的PHP學習是如此有限..但無論如何,謝謝 – jhunlio 2014-12-04 01:23:45

回答

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);

0

輸入字段具有相同的名稱?所以我想這就是爲什麼只有最後一個插入。

你必須循環INSERT查詢foreach產品你添加,這包括數量。

在將其插入查詢之前,您還應該對輸入值進行清理。

+0

不循環插入;只是價值觀 – Strawberry 2014-12-04 08:06:06

0

你需要使用的foreach

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

當你插入多個查詢,你不應該做的是從PHP循環。效率不高,因爲您正在執行多個查詢,而不是一個或兩個。您可以循環顯示從表單發送的結果,將其清理併爲數據庫插入做好準備,然後根據結果構建查詢。看看這裏的插入多行一次到一個數據庫:

Insert multiple rows with one query MySQL