dqx24298 2014-12-16 13:44
浏览 58
已采纳

SQL和PHP问题 - 错误:您的sql语法中有错误[关闭]

Error:

INSERT INTO demorequests 
         (firstname,lastname,email,phone,company,jobtitle,numberofstaff) 
VALUES 
        (Hank,Janssens,contact@hankjanssens.com,5193174848,Imaginadev,,1)

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 '@hankjanssens.com,5193174848,Imaginadev,,1)' at line 2

Database table structure

CREATE TABLE IF NOT EXISTS `demorequests` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `firstname` varchar(30) NOT NULL,
  `lastname` varchar(30) NOT NULL,
  `email` varchar(60) NOT NULL,
  `phone` varchar(12) NOT NULL,
  `company` varchar(60) NOT NULL,
  `jobtitle` varchar(30) NOT NULL,
  `numberofstaff` varchar(12) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

PHP //assign $_POST to variables

 $firstname = $_POST['firstname'];
    $lastname = $_POST['lastname'];
    $email = $_POST['email'];
    $phone = $_POST['phone'];
    $company = $_POST['company'];
    $jobtitle = $_POST['jobtitle'];
    $numberofstaff = $_POST['numberofstaff'];

    $sql = "INSERT INTO demorequests    
    (firstname,lastname,email,phone,company,jobtitle,numberofstaff)
    VALUES ($firstname,$lastname,$email,$phone,$company,$jobtitle,$numberofstaff)";
  • 写回答

4条回答 默认 最新

  • doushang9172 2014-12-16 13:47
    关注

    Jonathan - When dealing with strings, VALUES need to be quoted.

    ('$firstname','$lastname','$email','$phone','$company','$jobtitle','$numberofstaff')
    

    Nota: If it's an int, then quotes are not required. However, all of your columns are VARCHAR except for the id column being an AI.

    Do sanitize your inputs also with mysqli_real_escape_string() since your present code is open to SQL injection.

    You may always want to use stripslashes() in conjunction with mysqli_real_escape_string(), should a name contain an apostrophe.

    For example: (assuming you're using mysqli_ as your connecting API).

    $firstname = stripslashes($_POST['firstname']);
    $firstname = mysqli_real_escape_string($con, $_POST['firstname']);
    

    and do the same for the rest.

    Or better yet:

    Use mysqli with prepared statements, or PDO with prepared statements, they're much safer.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?