【问题标题】:Syntax error in SQL syntaxSQL 语法中的语法错误
【发布时间】:2016-02-18 08:49:06
【问题描述】:

这是错误信息:

SQLSTATE[42000]:语法错误或访问冲突:1064 您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 1 行的“$userID”附近使用正确的语法

这是我的代码:

<?php
require('includes/config.php');
//if logged in redirect to members page
require('layout/header.php');
if (isset($_POST['create'])) {
    try {
        $userID=$_SESSION['memberID'];
        //insert into database with a prepared statement  
        $stmt = $db->prepare('INSERT INTO business (bus_id,bus_name,bus_description,bus_phone,bus_email,bus_website,bus_category,bus_address,bus_hours,memberID) VALUES (NULL, :name, :description, :phone, :email, :website, :category, :address, :hours), $userID');  
        $stmt->execute(array(':name' => $_POST['bname'],':description' => $_POST['bdescription'],':phone' => $_POST['bphone'],':email' => $_POST['bemail'],':website' => $_POST['bwebsite'],':category' => $_POST['bcategory'],':address' => $_POST['baddress'],':hours' => $_POST['bhours']));  
        $id = $db->lastInsertId('bus_id');
        //redirect to index page
        header('Location: memberpage.php?action=joined');
        exit;
    }
    //else catch the exception and show the error.
    catch(PDOException $e) {
        $error[] = $e->getMessage();
    }
}
?>

【问题讨论】:

  • 你设置bus_id主键了吗
  • 你有一个)这里不应该是:hours)
  • 您以:hours), $userID' 结束查询,我猜, $userID 的部分必须删除

标签: javascript php mysql web-services mysqli


【解决方案1】:

SQL INSERT INTO statement 的格式如下:

INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);

正如错误消息所述,您的语句中 $userID 附近存在语法错误。因此,如果我们将其与上述定义进行比较,您应该将) 移到:hours 附近$userID 之后:

$stmt = $db->prepare('INSERT INTO business (bus_id,bus_name,bus_description,bus_phone,bus_email,bus_website,bus_category,bus_address,bus_hours,memberID) VALUES (NULL, :name, :description, :phone, :email, :website, :category, :address, :hours, $userID)');  

【讨论】:

  • 如果这一点得到确认,该问题应作为拼写错误关闭。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-14
  • 2015-01-31
  • 2015-03-22
相关资源
最近更新 更多