Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
591 views
in Technique[技术] by (71.8m points)

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

$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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

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

For example:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...