我有一个包含 3 列的表:id
、food_name
和category
。例如,我有$food_name = "Amaranth grain, cooked"
(有一个逗号)和category = "Cereals"
.
我无法插入值并出现以下错误:
Could not connect: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')
VALUES ('Amaranth grain, cooked', )' at line 1
我的 SQL 查询是:
$results1 = mysql_query("
INSERT INTO " . $table . " (food_en, category)
VALUES ('" . $food_name . "', '" . $category . "')"
)or die(mysql_error());
我的猜测是“Amaranth grain,cooked”中的逗号有问题,但我无法解决。我试过了str_replace(',', '\,', $food_name)
,mysql_real_escape_string()
但没有成功。我可能没有正确使用它们。
提前致谢。
编辑:我确实回显了 $category,它既不是 null 也不是空的(值是'谷物')。
当我打印而不是使用 mysql_query 时,我得到:
INSERT INTO calories (food_en, category)
VALUES ('Amaranth grain, cooked', 'Cereal Grains and Pasta')
INSERT INTO carbohydrates (food_en, category)
VALUES ('Amaranth grain, cooked', 'Cereal Grains and Pasta')
INSERT INTO fats_and_fatty_acids (food_en, category)
VALUES ('Amaranth grain, cooked', 'Cereal Grains and Pasta')
INSERT INTO protein_and_amino_acids (food_en, category)
VALUES ('Amaranth grain, cooked', 'Cereal Grains and Pasta')
INSERT INTO vitamins (food_en, category)
VALUES ('Amaranth grain, cooked', 'Cereal Grains and Pasta')
INSERT INTO minerals (food_en, category)
VALUES ('Amaranth grain, cooked', 'Cereal Grains and Pasta')
INSERT INTO sterols (food_en, category)
VALUES ('Amaranth grain, cooked', 'Cereal Grains and Pasta')
INSERT INTO other (food_en, category)
VALUES ('Amaranth grain, cooked', 'Cereal Grains and Pasta')Could not connect: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')
VALUES ('Amaranth grain, cooked', )' at line 1
(不同的表只是我希望将行插入的不同表)