2010-12-10 176 views
0

我有一個配方表和配​​料表,兩個表的主鍵都是自動增量,配方的主鍵是配料中的外鍵。我將數據從html發佈到php。請注意,我的成分文本框是動態生成的,併成功將數據發佈到php腳本。發佈的數據是正確的,當我插入這個數據表我的查詢工作正常,但數據不會添加到MySQL表。我的代碼和輸出是插入查詢不會插入數據到mysql數據庫表

$sql = "insert into recipe (rec_id, Name, Overview,category, Time, Image) values ('', '$name','$overview','$category','$time','$TARGET_PATH')"; 
    $result = mysql_query($sql) or die ("Could not insert data into DB: " . mysql_error()); 
     $rec_id = mysql_insert_id();

和成分

$ingredient = $_POST['ingredient']; 
$amount = $_POST['amount']; 
$integer = 0; 
while (count($ingredient)>$integer) { 
if (($ingredient[$integer] <> "") && ($amount[$integer] <> "")){ 
$sql = "INSERT INTO `cafe`.`ingredients` (`ingredient_id`, `ingredient_name`, `ammount`, `rec_id`,) 
    VALUES ('', '".$ingredient[$integer]."', '".$amount[$integer]."', '$rec_id')"; 
mysql_query($sql); 
echo $sql."
"; } else{ echo "ingredient number ".($integer+1)." is missing values and cannot be inserted."; } $integer = ($integer + 1); }

當我附和了把查詢是

nsert into recipe (rec_id, Name, Overview,category, Time, Image) values ('', 'demo recipe','no overview','meal','10/12/10 : 13:02:33','http://www.localhost/cafe/pics/demo.gif') 
INSERT INTO cafe.ingredients (ingredient_id, ingredient_name, ammount, rec_id,) VALUES ('', 'ingredient one', '3gm', '29') 
INSERT INTO cafe.ingredients (ingredient_id, ingredient_name, ammount, rec_id,) VALUES ('', 'ingredient two', '3gm', '29') 
INSERT INTO cafe.ingredients (ingredient_id, ingredient_name, ammount, rec_id,) VALUES ('', 'ingredient three', '3gm', '29')

,但是當我從成分看mysql表或retriew數據有成分中沒有數據。

+0

刪除cafe你可以請張貼的配方和成分的定義表? – 2010-12-10 08:24:50

+0

`amount`拼寫`amount` – 2010-12-10 08:46:58

回答

0

似乎是在代碼中的語法錯誤:

if (($ingredient[$integer] "") && ($amount[$integer] "")) 
          ^^       ^^ 

看起來你缺少一個比較操作。

+0

乾草這不是這樣實際上它是:如果(($成分[$整數] <> 「」)&&($量[$整數] <> 「」)) – hunter 2010-12-10 08:20:28

0

當插入犯規罰球異常和犯規插入數據有我想幾個選項

1)你的地方使用事務和回滾它 2)您選擇的查詢是壞的,數據是存在的,但你不選擇它

2

你有rec_id後,一個額外的,

刪除它,所以它看起來像

INSERT INTO cafe.ingredients (ingredient_id, ingredient_name, ammount, rec_id) VALUES ('', 'ingredient one', '3gm', '29') 

而你,也就OK

0

從查詢

$sql = "INSERT INTO ingredients (`ingredient_id`, `ingredient_name`, `ammount`, `rec_id`,) 
    VALUES ('', '".$ingredient[$integer]."', '".$amount[$integer]."', '$rec_id')";