以下是错误消息:
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();
}
}
?>
答案 0 :(得分:1)
SQL INSERT INTO
statement具有以下格式:
/**
* relation user.id -> (1 to N) favotites.user_id
*
* @return Favorites[]|null
*/
public function getFavorites()
{
return $this->hasMany(Favorite::className(), ['user_id' => 'id' ]);
}
/**
* relation user.id -> (1 to N) { favotites.user_id | favorites.object_id } -> (1 to 1) user.id
*
* @return Users[]|null
*/
public function getFavoriteUsers()
{
return $this->hasMany(self::className(), ['id' => 'object_id'])->via('favorites');
}
public function _getFavoriteUsers()
{
return $this->hasMany(self::className(), ['id' => 'object_id'])
->viaTable(Favorite::tableName(), ['user_id' => 'id']);
}
正如错误消息所示,语句中靠近INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);
附近存在语法错误。因此,如果我们将其与上述定义进行比较,您应该在$userID
后移动)
:hours
附近:
$userID