id
、food_name
、およびの 3 つの列を持つテーブルがあり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());
私の推測では、「アマランス粒、調理済み」のコンマが問題を引き起こしていると思いますが、解決できません。私は試してみましたが、成功しませんでしたstr_replace(',', '\,', $food_name)
。mysql_real_escape_string()
私はおそらくそれらを正しく使用していませんでした。
前もって感謝します。
編集: $category をエコーしましたが、null でも空でもありません (値は「Cereals」です)。
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
(異なるテーブルは、行を挿入したい単なる異なるテーブルです)