简体   繁体   English

插入查询不会将数据插入mysql数据库表

[英]Insert query does't insert data to mysql database table

i have a recipe table and ingredient table the primary key of both tables are auto increament and primary key of recipe is foreign key in ingredient. 我有一个食谱表和成分表,两个表的主键是自动增量,配方的主键是成分中的外键。 i post data from html to php.Note that my ingredient textboxes are generated dynamically and successfully post the data to php script. 我将数据从html发布到php.Note我的成分文本框是动态生成的,并成功将数据发布到php脚本。 posted data is correct when i insert this data to table my query working fine but data is not added to mysql table. 发布数据是正确的当我将此数据插入表我的查询工作正常但数据未添加到mysql表。 my code and output is 我的代码和输出是

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

and for ingredient 和成分

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

when i echo the queries the out put is 当我回答询问时,输出是

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

but when i see the mysql table or retriew data from ingredient there is no data in ingredient. 但是,当我看到mysql表或从配料中检索数据时,配料中没有数据。

You have an extra , after rec_id . 你有一个额外的,rec_id

Remove it, so it looks like 删除它,看起来像

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

And you will be OK 你会没事的

You might want to verify if you are using a BEGIN call and not committing after INSERT. 您可能想验证是否正在使用BEGIN调用而不是在INSERT之后提交。 You can refer to http://www.devarticles.com/c/a/MySQL/Using-Transactions-with-MySQL-4.0-and-PHP/ 你可以参考http://www.devarticles.com/c/a/MySQL/Using-Transactions-with-MySQL-4.0-and-PHP/

in that scenario. 在那种情况下。

There seems to be a syntax error in your code: 您的代码中似乎存在语法错误:

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

Looks like you are missing a comparison operator. 好像您缺少比较运算符。

When insert doesnt throw exceptions and doesnt insert data there are I think a couple of options 当插入不会抛出异常并且不插入数据时,我认为有两种选择

1) you use transaction somewhere and rollback it 2) your select query is bad and data is there, but you just dont select it 1)你在某处使用事务并回滚它2)你的选择查询是坏的,数据在那里,但你只是不选择它

remove cafe from the query 从查询中删除cafe

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM