php 为什么我会收到 MySQL 错误“查询为空”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/636032/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 23:20:16  来源:igfitidea点击:

Why do I get the MySQL Error "Query was empty"?

phpmysql

提问by Greg

$id = $_REQUEST['id'];
$Section = $_REQUEST['section'];
$Subject = $_REQUEST['subject'];
$type = $_REQUEST['type'];
$Start_date1 = isset($_REQUEST['startTxt'])?($_REQUEST['startTxt']):"";
$Venue = isset($_REQUEST['venTxt'])?($_REQUEST['venTxt']):"";
$Facilitator = isset($_REQUEST['faciTxt'])?($_REQUEST['faciTxt']):"";
$Level = isset($_REQUEST['lvlLst'])?($_REQUEST['lvlLst']):"";
$Date1 = $_REQUEST['date1'];

if(isset($_REQUEST['EDIT']))
{
    mysql_query("UPDATE service SET Start_date='$Date1', Venue='$Venue', Facilitator='$Faci' WHERE ServiceID ='$id'");
    if (!mysql_query($sql,$con))
    {
        die('Error: ' . mysql_error());
    }

    echo '<script type="text/javascript">';
    echo 'alert("Changes have been save!");';
    echo 'window.location="Admin_RecSchedMapLst.php";';
    echo '</script>';
    mysql_close($con);
}           

When I click save it returns "Error: Query was empty" - why is this?

当我单击保存时,它返回“错误:查询为空”——这是为什么?

回答by Greg

You're calling mysql_query()twice, once with a non-existent $sqlparameter:

您调用了mysql_query()两次,一次使用不存在的$sql参数:

mysql_query("UPDATE service SET Start_date='$Date1', Venue='$Venue', Facilitator='$Faci' WHERE ServiceID ='$id'");
if (!mysql_query($sql,$con))

should be:

应该:

if (!mysql_query("UPDATE service SET Start_date='$Date1', Venue='$Venue', Facilitator='$Faci' WHERE ServiceID ='$id'"))

You're also not escaping your input, leaving you open to SQL injection. You should use bound parameters ideally, or at the very least run your parameters through mysql_real_escape_string().

你也没有逃避你的输入,让你对 SQL 注入持开放态度。理想情况下,您应该使用绑定参数,或者至少通过mysql_real_escape_string().

For example:

例如:

$Date1 = mysql_real_escape_string($Date1, $conn);

回答by Paul Tarjan

Please, for the love of the internet, don't built an SQL query yourself. Use PDO.

出于对互联网的热爱,请不要自己构建 SQL 查询。使用PDO

回答by jonstjohn

You are not setting the $sql variable and calling mysql_query() twice.

您没有设置 $sql 变量并两次调用 mysql_query() 。