谁能解释 MariaDB 的这种奇怪行为?
Can anyone explain this weird behaviour of MariaDB?
我正在创建一个具有基本功能的博客,其中之一是向数据库添加帖子。
这是我编写的代码,它在用户单击提交时将数据插入数据库:
if(isset($_POST['submit'])){
//Assign the variables
$title = mysqli_real_escape_string($db->link, $_POST['title']);
$category = mysqli_real_escape_string($db->link, $_POST['category']);
$body = mysqli_real_escape_string($db->link, $_POST['body']);
$author = mysqli_real_escape_string($db->link, $_POST['author']);
$tags = mysqli_real_escape_string($db->link, $_POST['tags']);
// //Simlpe Validation
if($title == '' || $category='' ||$body == '' || $author == ''){
//Set Error
$error = 'Please fill out all the required fields.';
}
else{
$query = "insert into posts (title, body, author, tags, category) values ('$title','$body', '$author', '$tags',$category)";
$insert_row = $db->insert($query);
}
}
错误说:
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 ')' at line 1
奇怪的部分来了。
当我排除 if 和 else 语句并直接 运行 if 和 else 语句之外的查询时,如下所示:
$query = "insert into posts (title, body, author, tags, category) values ('$title','$body', '$author', '$tags',$category)";
$insert_row = $db->insert($query);
// Simlpe Validation
// if($title == '' || $category='' ||$body == '' || $author == ''){
// //Set Error
// $error = 'Please fill out all the required fields.';
// }
// else{
// }
但是使用上面的代码查询 运行s 完美并且数据库得到更新。
谁能解释一下?
我很难理解为什么它会这样。
顺便说一下,这里是数据库 class 中用于插入的方法:
/*
* Insert
*/
public function insert($query){
$insert_row = $this->link->query($query) or die($this->link->error);
//Validate insert
if($insert_row){
header("Location: index.php?msg=".urlencode('Record Added'));
exit();
}
else{
die('Error: ('.$this->link->errno.') '.$this->link->error);
}
}
编辑:
有人问 'category'。好吧,$_POST['category'] 是一个整数,帖子的列类别 table 显然也是一个整数。这就是为什么我没有在查询中对 $category 保留任何引号。
感谢@FDavidov 建议我开始在这两种情况下登录。我发现在 if 语句中,而不是这个:
$constant == ''
我写了这个:
$constant =''
它覆盖了 $category
的值,因此在查询中有一个前导 ,
(额外的逗号)导致语法错误。
我将其更正为 $constant == ''
。现在一切都很好。
我正在创建一个具有基本功能的博客,其中之一是向数据库添加帖子。
这是我编写的代码,它在用户单击提交时将数据插入数据库:
if(isset($_POST['submit'])){
//Assign the variables
$title = mysqli_real_escape_string($db->link, $_POST['title']);
$category = mysqli_real_escape_string($db->link, $_POST['category']);
$body = mysqli_real_escape_string($db->link, $_POST['body']);
$author = mysqli_real_escape_string($db->link, $_POST['author']);
$tags = mysqli_real_escape_string($db->link, $_POST['tags']);
// //Simlpe Validation
if($title == '' || $category='' ||$body == '' || $author == ''){
//Set Error
$error = 'Please fill out all the required fields.';
}
else{
$query = "insert into posts (title, body, author, tags, category) values ('$title','$body', '$author', '$tags',$category)";
$insert_row = $db->insert($query);
}
}
错误说:
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 ')' at line 1
奇怪的部分来了。
当我排除 if 和 else 语句并直接 运行 if 和 else 语句之外的查询时,如下所示:
$query = "insert into posts (title, body, author, tags, category) values ('$title','$body', '$author', '$tags',$category)";
$insert_row = $db->insert($query);
// Simlpe Validation
// if($title == '' || $category='' ||$body == '' || $author == ''){
// //Set Error
// $error = 'Please fill out all the required fields.';
// }
// else{
// }
但是使用上面的代码查询 运行s 完美并且数据库得到更新。
谁能解释一下?
我很难理解为什么它会这样。
顺便说一下,这里是数据库 class 中用于插入的方法:
/*
* Insert
*/
public function insert($query){
$insert_row = $this->link->query($query) or die($this->link->error);
//Validate insert
if($insert_row){
header("Location: index.php?msg=".urlencode('Record Added'));
exit();
}
else{
die('Error: ('.$this->link->errno.') '.$this->link->error);
}
}
编辑: 有人问 'category'。好吧,$_POST['category'] 是一个整数,帖子的列类别 table 显然也是一个整数。这就是为什么我没有在查询中对 $category 保留任何引号。
感谢@FDavidov 建议我开始在这两种情况下登录。我发现在 if 语句中,而不是这个:
$constant == ''
我写了这个:
$constant =''
它覆盖了 $category
的值,因此在查询中有一个前导 ,
(额外的逗号)导致语法错误。
我将其更正为 $constant == ''
。现在一切都很好。