2

I'm trying to insert a new record in a MySQL database from PHP, which I've done a million times before, but for some reason, I can't get it to work this time, and it really bugs me.

Inserting strings into all the varchar collumns are going great, but when I get to inserting a value into the int column, I get an error telling me that I have a syntax error.

Basically, the first query works just fine, but the second one returns the error, and as you can see, I've made damn sure it really is an integer I'm trying to insert.

I hope somebody can help. I'm really starting to develop a headache over this :/

$groupId2 = 5;
$groupId = (int)$groupId2;
if(!mysqli_query($link, "INSERT INTO contestants (firstName, lastname, email) VALUES ('$firstName', '$lastName', '$email')"))
  echo "First: " . mysqli_error($link);
if(!mysqli_query($link, "INSERT INTO contestants (firstName, lastname, email, group) VALUES ('$firstName', '$lastName', '$email', '$groupId')"))
  echo "Second: " . mysqli_error($link);
4

2 に答える 2

7

groupは mysql キーワードであり、前後に逆引用符を使用します

"INSERT INTO contestants (firstName, lastname, email, `group`) 
 VALUES ('$firstName', '$lastName', '$email', '$groupId')"
于 2012-07-06T15:28:51.663 に答える
3

エラーは、int を ' ' で囲んだためです。アポストロフィを取り除く必要があり、問題なく動作します。

if(!mysqli_query($link, 
   "INSERT INTO contestants
   (firstName, lastname, email, group) VALUES
   ('$firstName', '$lastName', '$email', $groupId)"))
                                         ^^^^^^^^^

明確にするために、数値フィールドを挿入する場合、それらは必要ありません。

pst によると、これは間違っていますが、一重引用符が必要ないという事実は依然として正しいです。

于 2012-07-05T22:27:46.967 に答える