php-sql语法错误

标签 php mysql sql mysqli mariadb

我正在尝试将表单数据插入到我的数据库中的一个表中。要插入的数据是姓名、电子邮件、当前日期和用户兴趣。 这是代码。

if (isset($_POST['name'])) {
   $name = $_POST['name'];
   $email = $_POST['email'];
   $intrests = $_POST['intrests'];
   $default_intrests = array("mob","pcs","scm","oth");
   $interests = "";
   if (count($intrests) == 0) {
      $interests = implode(",", $default_intrests);
   }
   else {
          $interests = implode(",", $intrests);
   }

   $sqll="insert into subscriptions (name,email,subdate,intrests) values ($name,$email,CURRENT_DATE, $interests)";
   $insert = mysqli_query($link, $sqll);
   if (!$insert) {
      echo mysqli_error($link);
   }
}

在提交表单时,显示以下错误:

You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'dsa,asdf@qwer.com,CURRENT_DATE, mob)' at line 1

最佳答案

' 添加到值中,因为其中一些是 string

 $sqll="insert into subscriptions (name,email,subdate,intrests)
     values ('$name','$email',CURRENT_DATE, '$interests')";

事实上,直接将参数写入sql是个坏主意,最好使用prepared-statements。这样做并避免SQL Injection

关于php-sql语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51238608/

相关文章:

php - preg_replace_callback 错误中的正则表达式。 PHP

php - 为什么 mysql_real_escape_string 对 $_POST 不起作用?

MySQL 自动增量 InnoDB 类似 MyISAM

java - 在没有 Visual 的情况下在 java 中打开链接

mysql - SQL从两个表更新余额

php - 使用 POST/GET 将数组从 Objective C 发送到 PHP

php - 需要有关执行 mysql 插入查询的一些逻辑的帮助吗?

php - HTML 选择不显示所有选项

mysql - 加入查询也返回空

php ctype_alpha 不适用于 mysql 数据库数据