简体   繁体   English

非常简单的MySQL查询无法正常工作

[英]Very simple MySQL query not working

I'm trying to execute this: 我正在尝试执行此操作:

$result = mysql_query("INSERT INTO timesheet (project_no,user,cust_name,notes,duration) VALUES("'".$_POST['project']."', '".$_POST['user']."', '".$_POST['cust']."', '".$_POST['notes']."', '".$_POST['duration']."'")") or die(mysql_error());

I'm aware of SQL injection. 我知道SQL注入。 But for now can anyone spot the issues with apostrophes, speech marks etc?? 但是现在任何人都可以发现撇号,语音标记等问题吗?

使用准备好的声明http://www.petefreitag.com/item/356.cfm

The apostrophes were not correct. 撇号不正确。

$result = mysql_query("INSERT INTO timesheet (project_no,user,cust_name,notes,duration) VALUES('".$_POST['project']."', '".$_POST['user']."', '".$_POST['cust']."', '".$_POST['notes']."', '".$_POST['duration']."')") or die(mysql_error());

The mistake was in the beginning in "values" and on the closing bracket inside the query string. 错误是在“值”的开头和查询字符串内的右括号中。 Use an editor with syntax highlighting, that would've already showed the problem. 使用带有语法高亮显示的编辑器,这已经表明了问题所在。

Unending lines filled with parts of strings and variables concatenated with dots is one of the most horrible habits of too many PHP programmers. 无休止的由字符串组成的行和由点连接的变量组成的行是太多PHP程序员最可怕的习惯之一。 Do your future self a favor and write readable code. 帮自己将来一个忙,写可读的代码。 Sticking with this example (ignoring all its other problems) use other variables to hold the values (which in the real world you'll need anyway since you're not going to directly use $_POST I hope) and write something like this: 坚持这个示例(忽略其所有其他问题),使用其他变量来保存值(在现实世界中,无论如何,由于您不想直接使用$_POST您仍然需要它们),并编写类似以下内容的代码:

$sql = "INSERT INTO timesheet (project_no,user,cust_name,notes,duration)
        VALUES('$project', '$user', '$cust', '$notes', '$duration')";

$result = mysql_query($sql) or die(mysql_error());

no more quotes open/close madness, no more 239 chars line, way more readable and maintainable. 不再有引号引起的开/关疯狂,不再有239个字符的行,更易于阅读和维护。

I think double quote and closing apostrophes issue. 我认为双引号和右撇号问题。 Try this: 尝试这个:

$result = mysql_query("INSERT INTO timesheet (project_no,user,cust_name,notes,duration) VALUES('".$_POST['project']."', '".$_POST['user']."', '".$_POST['cust']."', '".$_POST['notes']."', '".$_POST['duration']."')") or die(mysql_error());

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

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