简体   繁体   English

MYSQL插入失败-语法正确吗?

[英]MYSQL insert failing - Syntax correct?

Does anyone see anything wrong with this INSERT statement? 有人认为此INSERT语句有什么问题吗? This fails for me when used in PHP code. 在PHP代码中使用时,这对我来说失败了。 Im using MySql 5.6. 我正在使用MySql 5.6。 dept_id is the primary key.I dont see anything wrong with the syntax but it is failing just the same. dept_id是主键。我没有看到语法有什么问题,但同样失败了。 Any help would be awesome! 任何帮助都是极好的!

    $add_sql = @mysql_query("INSERT INTO `vf_department` (`dept_id`,`descr`,`sub_dept`)VALUES('','$new_desc','$new_sub')") or die("Can not add item.");

is your primary key an auto increment? 您的主键是自动增量吗? If so, you do not need to have it as part of the insert statement. 如果是这样,则不需要将其作为插入语句的一部分。

I agree with ebizzity... if the one column is an auto-increment, leave it out of the insert command, MySQL will generate that for you. 我同意ebizzity ...如果其中一栏是自动递增的,请将其保留在insert命令之外,MySQL将为您生成该增量。 As for all the other comments, I'm surprised nobody mentioned your exposure to SQL injection. 至于所有其他评论,令我惊讶的是,没有人提到您遭受SQL注入。

if the values for your $new_desc and/or $new_sub contain single quote, that would offset the balance of your command and make it choke... not to mention would COULD happen with sql injection. 如果$ new_desc和/或$ new_sub的值包含单引号,那将抵消您命令的余额并使其窒息...更不用说SQL注入会发生这种情况。 Look into cleansing incoming data and parameterize your calls. 研究清理传入的数据并参数化您的呼叫。

try this 尝试这个

$add_sql = mysql_query("INSERT INTO `vf_department` (`descr`,`sub_dept`)
          VALUES('$new_desc','$new_sub')") or die(mysql_error());

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

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