sql php显示错误,php- SQL语法错误

我试图将表单数据插入数据库中的表之一.要插入的数据是名称,电子邮件,当前日期和用户兴趣.

这是代码.

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

解决方法:

将’添加到值,因为其中一些是字符串

$sqll="insert into subscriptions (name,email,subdate,intrests)

values ('$name','$email',CURRENT_DATE, '$interests')";

实际上,直接将参数写入sql是一个不好的主意,最好使用prepared-statements来执行,避免使用SQL Injection

标签:mysqli,mariadb,sql,mysql,php

来源: https://codeday.me/bug/20191108/2010042.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值