2012-05-02 12 views
0

私の現在のフォームは2つの異なるテーブルにデータを送信し、1つのテーブルのauto_incremented値も2番目のテーブルに保存します。フォームの提出後、別のテーブルへのエントリのIDを取得

<form method="POST" action="addcocktail.php" > 
        Cocktail Name: <input type="text" name="cocktailname" /> 
        How To: <input type="text" name="howto" /> 
        <br> 
        <select id="selectingred1" name="selectingred1"> 
         <?php 
         $sql = "SELECT ingredientID, name FROM tblIngredient ". 
         "ORDER BY name"; 

         $rs = mysql_query($sql); 

         while($row = mysql_fetch_array($rs)) 
         { 
         echo "<option value=\"".$row['ingredientID']."\">".$row['name']."</option>\n "; 
         } 
         ?> 
        </select> 
        <select id="quantity1" name="quantity1"> 
         <option></option> 
         <option>1</option> 
         <option>2</option> 
         <option>3</option> 
         <option>4</option> 
        </select> 
        <br> 
<input type="submit" value="add" /> 
       </form> 

addcocktail.php:

<?php include("databasecon.php"); ?> 

<?php 
mysql_select_db("mwheywood", $con); 

//insert cocktail details 
$sql="INSERT INTO tblCocktail (name, howto) 
VALUES 
('$_POST[cocktailname]','$_POST[howto]')"; 

$sql2="INSERT INTO tblRecipe (ingredientID, quantity) 
VALUES 
('$_POST[selectingred1]','$_POST[quantity1]'), 
('$_POST[selectingred2]','$_POST[quantity2]'), 
('$_POST[selectingred3]','$_POST[quantity3]'), 
('$_POST[selectingred4]','$_POST[quantity4]')"; 



if (!mysql_query($sql,$con)) 
    { 
    die('Error: you fail at life' . mysql_error()); 
    } 
echo "cocktail added"; 

if (!mysql_query($sql2,$con)) 
    { 
    die('Error: you fail at life' . mysql_error()); 
    } 
echo "ingredients added"; 

mysql_close($con); 

?> 

私は自分のフォームを送信するときに、単純にそれを置くために。投稿クエリの実行後に投稿データの "cocktailID"を "tblRecipe"に保存するには

答えて

2

mysql_insert_id()機能で成功すれば挿入IDを取得できます。

+0

hmm Insert文の後に各選択ボックスのエントリにcocktailIDを挿入する方法がわからないのですか? – MattHeywood

+0

それについて考えてみてください。あなたはすべての機能を持っている、それらを一緒に置くだけです。 –

関連する問題