简体   繁体   English

带有html复选框值的SQL语法中的错误

[英]error in SQL syntax with html checkbox value

Surely it is a really stupid issue, but I can not find the error. 当然这是一个非常愚蠢的问题,但是我找不到错误。

From a form I get the value of a checkbox (0 or 1), but the query generates an error. 从表单中,我得到一个复选框的值(0或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 'check)" “您的SQL语法有误;请检查与您的MySQL服务器版本相对应的手册,以找到在'check'附近使用的正确语法”

<form action="processCategory.php?action=add" method="post" enctype="multipart/form-data" name="frmCategory" id="frmCategory">
  <input type="hidden" name="check" value="0" />
  <input type="checkbox" name="check" value="1" />
</form>

PHP 的PHP

 $checkboxValue = (isset($_POST['check'])) ? intval($_POST['check']) : 0; // returns 0 or 1


$sql   = "INSERT INTO tbl_category (cat_parent_id, cat_name, cat_description, check) 
          VALUES ($parentId, '$name', '$description', $checkboxValue)";
$result = dbQuery($sql) or die('Cannot add category' . mysql_error());

mysql field: check mysql字段:检查

`check` tinyint(1) NOT NULL

check is a reserved word and you are using it inside the query as one of the column. check是保留字,您正在查询中将其用作该列之一。

List of all reserved words 所有保留字的列表

You need to use backticks ` to escape reserved words in MySQL: 您需要使用反引号`来转义MySQL中的保留字:

INSERT INTO tbl_category (cat_parent_id, cat_name, cat_description, `check`) 

change: 更改:

$sql   = "INSERT INTO tbl_category (cat_parent_id, cat_name, cat_description, check) 
      VALUES ($parentId, '$name', '$description', $checkboxValue)";

to: 至:

$sql   = "INSERT INTO tbl_category (cat_parent_id, cat_name, cat_description, check) 
      VALUES ($parentId, $name, $description, $checkboxValue)";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM