0

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!

EmmaNora
  • 1
  • 2

2 Answers2

0

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);
?>
coDe murDerer
  • 1,858
  • 4
  • 20
  • 28
0

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);
?>
Navneet Garg
  • 1,364
  • 12
  • 29