带有 html 复选框值的 SQL 语法错误 [英] error in SQL syntax with html checkbox value

查看:25
本文介绍了带有 html 复选框值的 SQL 语法错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这确实是一个非常愚蠢的问题,但我找不到错误.

Surely it is a really stupid issue, but I can not find the error.

我从表单中获取复选框的值(0 或 1),但查询生成错误.您的 SQL 语法有错误;请检查与您的 MySQL 服务器版本相对应的手册,以在检查"附近使用正确的语法"

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 字段:检查

mysql field: check

`check` tinyint(1) NOT NULL

推荐答案

check 是保留字,您在查询中将其用作列之一.

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

所有保留字列表

您需要使用反引号 ` 来转义 MySQL 中的保留字:

You need to use backticks ` to escape reserved words in MySQL:

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

这篇关于带有 html 复选框值的 SQL 语法错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆