简体   繁体   中英

Why am I getting this error “Error: Query was empty”

I am trying to update my SQL database using a form through php, but i keep getting the error "Error: Query was empty".

<?php
$sql = "";
$con = mysql_connect("*******","*******");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("*******", $con);
mysql_query($sql, $con);


if (isset($_POST['STUDENT_FNAME'], $_POST['STUDENT_SNAME'],
$_POST['STUDENTNO'] ))
{ 

$sql="UPDATE STUDENT SET STUDENT_FNAME=('$_POST[STUDENT_FNAME]'),
STUDENT_SNAME=('$_POST[STUDENT_SNAME]')
WHERE STUDENTNO=
('$_POST[STUDENTNO]')";
}

if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "1 record updated";

mysql_close($con);
?>

It also won't update my table and I don't know what I've done wrong. All help will be much appreciated. I am new to this as you can probably tell!

Remove mysql_query($sql, $con) query execution after db selection because $sql is empty,

Also put your update sql execution in IF conditions , because if its not true than again $sql will be empty and you will get same error again,...

<?php
$sql = "";
$con = mysql_connect("*******","*******");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("*******", $con);
// mysql_query($sql, $con); // <-- remove this

if (isset($_POST['STUDENT_FNAME'], $_POST['STUDENT_SNAME'],
$_POST['STUDENTNO'] ))
{ 

$sql="UPDATE STUDENT SET STUDENT_FNAME=('$_POST[STUDENT_FNAME]'),
STUDENT_SNAME=('$_POST[STUDENT_SNAME]')
WHERE STUDENTNO=
('$_POST[STUDENTNO]')";

   if (!mysql_query($sql,$con))
   {
    die('Error: ' . mysql_error());
   }
   echo "1 record updated";
}
mysql_close($con);
?>

There is an error in your code first you are calling mysql_query($sql, $con); without any query in your $sql variable your $sql is blank ""

<?php
$sql = "";
$con = mysql_connect("*******","*******");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("*******", $con);


if (isset($_POST['STUDENT_FNAME'], $_POST['STUDENT_SNAME'],
$_POST['STUDENTNO'] ))
{ 

$sql="UPDATE STUDENT SET STUDENT_FNAME=('$_POST[STUDENT_FNAME]'),
STUDENT_SNAME=('$_POST[STUDENT_SNAME]')
WHERE STUDENTNO=
('$_POST[STUDENTNO]')";
}

if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "1 record updated";

mysql_close($con);
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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