2016-10-17 1 views
-2

私はあなたが注文したい量を入力することができますスパチュラの詳細のテーブルを持ってこのhtmlフォームを持っています。詳細を記入して提出すると、customerdetails、staffがテーブルオーダーに挿入され、auto_incrementオーダーIDが作成されます。他のテーブルはorderidとspatulaidを外部キーとして持つorderlistと呼ばれ、量もあります。どうすれば私の注文リストテーブルに挿入するために、スパチュラID、注文IDと数量を取得するPHPを書くだろうか?私はスパチュラIDと注文IDに関して特定の量を記入する必要があります。どのようにhtmlからの入力を取得し、PHPに送信し、IDに関連してテーブルに挿入

<html> 
<head><title>Orders</title></head> 
<body> 
<h1> Order </h1> 
<form method='post' action ="insert.php" > 
<p> 
Customer Details <br> 
<textarea rows="4" cols="50" name="customerdetails"> 
</textarea> 
</p> 
<p> 
Responsible Staff Member: 
<input type="text" name="staff"> 
</p> 
<table border="1"> 
    <tr> 
    <th> Spatula ID </th> 
    <th> Name </th> 
    <th> Type </th> 
    <th> Size </th> 
    <th> Colour </th> 
    <th> Price </th> 
    <th> Quantity currently in stock </th> 
    <th> Order Quantity </th> 
    </tr> 
    <?php 


    $con = mysqli_connect("localhost","blah","blah","blah"); 

    // Check connection 
    if (mysqli_connect_errno()) { 
    echo "Could not connect to MySQL for the following reason: " .  mysqli_connect_error(); 
    } 
    $sql = "Select * FROM Spatula"; 
    $result = mysqli_query($con,$sql); 

    while($row=mysqli_fetch_array($result)){ 
    echo "<tr><td>".$row['idSpatula']."</td><td>".$row['ProductName']."</td> <td>". 
     $row['`Type`']."</td><td>".$row['Size']."</td><td>".$row['Colour']. 
     "</td><td>".$row['Price']."</td><td>".$row['QuantityInStock']."</td><td>" 
     .'<input type="text" name="quantity"</td></tr>'; 
    } 
    mysqli_close($con); 
    ?> 
</table> 
</p> 
<input type='submit' value='Submit' /> 
</form> 
</body> 
</html> 

これは私のinsert.php

<?php 

    $con = mysqli_connect("localhost","blah","blah","blah"); 

    // Check connection 
    if (mysqli_connect_errno()) { 
    echo "Could not connect to MySQL for the following reason: " . mysqli_connect_error(); 
    } 
    $customerdetails = $_POST['customerdetails']; 
    $staff = $_POST['staff']; 
    $date = date("Y-m-d H:i:s"); 
    $quantity = $_POST['quantity']; 
    $sql_string = "INSERT INTO  `Order`(RequestedTime,ResponsibleStaffMember,CustomerDetails) 
    VALUES ('$date','$staff','$customerdetails')"; 
    $result = mysqli_query($con,$sql_string); 
    } 
    if($result) 
    { 
    echo 'Data inserted successfully'; 
    } 
    else{ 
    echo 'Data insertion failed'; 
    } 
    mysqli_close($con); 
    ?> 

答えて

1

使用です:http://php.net/manual/en/mysqli.insert-id.php

mysqli_insert_id($your_connection_variable) 

がautoの参考のために最後のクエリ

で使用されるIDを生成し返します。

テーブルから最後のインサートIDを取得し、そのIDを別のテーブルに挿入することができます。

関連する問題