2014-12-04 1 views
2

를 사용하여 데이터베이스에서 여러 행을 삽입 내가 product_namejquery의 도움으로 productquantity에 대한 input field 더 추가 할 수있는 형태를 만들어,이 양식에서 더 많은 입력 필드를 추가 할 수 있습니다 demo입니다.PHP 양식

문제는 양식을 제출하면 product은 내 데이터베이스에 제출되며 나머지 제품은 제출되지 않습니다.

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

어떤 몸은 내 쿼리

<?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 

    } 


?> 

내 양식 input field의 모든 productquantity 입력이 데이터베이스에 저장됩니다 제출하는 방법을 어떤 생각했다입니다.

+1

내가 그냥 루프에서 값 목록을 구축하고 구축하고 전체 쿼리 – Strawberry

+0

감사의 위해를 실행할 필요가 있다고 생각 : 데이터베이스에 한 번에 삽입 여러 행을 위해 여기 봐 당신의 제안,하지만 내 PHP 학습이 너무 제한되어 있기 때문에 어디서부터 시작 해야할지 모르겠다 ..하지만 어쨌든 고마워요 – jhunlio

답변

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 쿼리를 반복해야합니다. 여기에는 수량이 포함됩니다.

입력 값을 쿼리에 삽입하기 전에 입력 값을 모두 소거해야합니다.

+0

삽입 루프를하지; 단지 값 – Strawberry

0

당신은 여러 개의 쿼리를 삽입 할 때, 당신은 PHP 루프에서 그렇게 안

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

foreach 문을 사용해야합니다. 하나 또는 두 개의 쿼리 대신 여러 개의 쿼리를 실행하기 때문에 효율적이지 않습니다. 양식에서 보낸 결과를 반복하고, 정리하고, 데이터베이스 삽입을 준비한 다음, 결과를 기반으로 쿼리를 작성할 수 있습니다.

(Insert multiple rows with one query MySQL)