简体   繁体   中英

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. "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)"

<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

 $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

`check` tinyint(1) NOT NULL

check is a reserved word and you are using it inside the query as one of the column.

List of all reserved words

You need to use backticks ` to escape reserved words in 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)";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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