【问题标题】:Why I'm getting syntax error in SQL statement for a variable containing email address? [duplicate]为什么我在包含电子邮件地址的变量的 SQL 语句中出现语法错误? [复制]
【发布时间】:2015-03-22 13:05:06
【问题描述】:

我正在使用 PHP、MySQL 和 PHPMyAdmin

以下是 PHP 文件中的代码:

$sql = "INSERT INTO user_login (user_id, email, token)
  VALUES ($user_id, $email, $token)";

上述查询出现以下错误:

Error: INSERT INTO user_login (user_id, email, token)
  VALUES (303, mrp7590@kastate.edu, 68e1f6cbea3b7a0b77a28395f4a8fef8449c23b1b00a392aa43451a0bfd5ff0e)<br>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 '@kastate.edu, ise152e16e36393037933be10ac8a6f6de9e257a5d85e008bbd379bae20b5f535b)' at line 2

有人可以纠正我在上述插入查询中犯的错误吗?

谢谢。

【问题讨论】:

  • SQL 期望字符串值被引用......但你不应该将值直接注入 SQL 语句,而是使用绑定变量
  • 有什么理由不使用准备好的语句?

标签: php mysql sql syntax-error sql-insert


【解决方案1】:

$sql = "INSERT INTO user_login (user_id, email, token) VALUES ($user_id, '$email', '$token')";

【讨论】:

    【解决方案2】:

    你必须用单引号括起来

    $sql = "INSERT INTO user_login (user_id, email, token)
    VALUES ($user_id, '$email', '$token')";
    

    喜欢这个

    INSERT INTO user_login (user_id, email, token)
    VALUES (303,'mrp7590@kastate.edu','68e1f6cbea3b7a0b77a28395f4a8fef8449c23b1b00a392aa43451a0bfd5ff0e')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-29
      • 1970-01-01
      • 1970-01-01
      • 2021-11-09
      • 1970-01-01
      相关资源
      最近更新 更多