Thank you pritaeas for prior assistance. I have another problem for someone. My query complains with this message "query was empty" and I can't undetstand why. Need help please see code below.


<?php
$db="freedomone";
$link = mysql_connect("");
if (!$link)
die("Couldn't connect to MySQL");
mysql_select_db($db , $link)
or die("Couldn't open $db: ".mysql_error());

$id=$_POST;
$FName=$_POST;
$LName=$_POST;
$PatientPhone=$_POST;
$FacName=$_POST;
$FacAddress=$_POST;
$PatRoom=$_POST;
$FacCity=$_POST;
$FacState=$_POST;
$FacZip=$_POST;
$FacPhone=$_POST;
$CaregiverName=$_POST;
$CaregiverContactPhone=$_POST;
$MedDispenseTime1=$_POST;
$MedDispenseTime2=$_POST;
$MedDispenseTime3=$_POST;
$MedDispenseTime4=$_POST;
$MedDispenseTime5=$_POST;
$MedDispenseTime6=$_POST;
$Month=$_POST;
$Year=$_POST;

mysql_query(" UPDATE Liberty SET firstname='$FName' , lastname='$LName' , PatientPhone='$PatientPhone' , FacName='$FacName' , FacAddress='$FacAddress' , PatRoom='$PatRoom' , FacCity='$FacCity' , FacState='$FacState' , FacZip='$FacZip' , FacPhone='$FacPhone' , CaregiverName='$CaregiverName' , CaregiverContactPhone='$CaregiverContactPhone' , MedDispenseTime1='$MedDispenseTime1' , MedDispenseTime2='$MedDispenseTime2' , MedDispenseTime3='$MedDispenseTime3' , MedDispenseTime4='$MedDispenseTime4' , MedDispenseTime5='$MedDispenseTime5' , MedDispenseTime6='$MedDispenseTime6' , Month='$Month' , Year='$Year' WHERE PatientID='$id");

/* The line below verifies query and it's returning the error "query was empty" */
$ret = mysql_query($query) or die(mysql_error());

echo "Record Updated";
mysql_close($link);
?>

Dani AI

Generated

As and already pointed out, the immediate cause of the "Query was empty" message is that the code calls mysql_query($query) while $query was never set (the UPDATE was passed directly earlier and/or failed). The script also has other defects that commonly trigger that message: assigning the whole superglobal ($id = $_POST;) instead of a specific key, and a mismatched quote in the WHERE clause (PatientID='$id")) which will make the first query fail and leave you later calling mysql with an empty value. The general pattern is: a failed or undefined query value becomes empty and MySQL returns "Query was empty". (stackoverflow.com)

Correct the input handling and move to parameterized queries. Example (short, safe pattern — not the long UPDATE from the thread):

$fname = filter_input(INPUT_POST, 'FName', FILTER_SANITIZE_STRING);
$id    = filter_input(INPUT_POST, 'PatientID', FILTER_SANITIZE_STRING);

$mysqli = new mysqli('localhost','dbuser','dbpass','freedomone');
$stmt = $mysqli->prepare('UPDATE Liberty SET firstname = ? WHERE PatientID = ?');
$stmt->bind_param('ss', $fname, $id);
$stmt->execute();

This avoids interpolation bugs and SQL injection; use prepared statements for all user data. (php.net)

Quick troubleshooting checklist (apply in order):

  • Dump the SQL or variables before executing: var_dump($query) or error_log($query) to see what you actually send.
  • Fix POST extraction ($_POST['FieldName'] or filter_input) instead of assigning the whole array. (php.net)
  • Look for mismatched quotes/parentheses in your SQL (the WHERE clause in the OP shows that).
  • Stop using ext/mysql: it is deprecated and removed in modern PHP; migrate to mysqli or PDO and use prepared statements. (php.net)

References:

Practical next step: implement the small prepared-statement pattern above, confirm the POST keys match your form names, and remove the extra mysql_query(...) call that uses an undefined variable.

Recommended Answers

All 5 Replies

Where is $query set? The line above is executing the query, without capturing the result. Looks like it should be replaced with:

$query = " UPDATE Liberty SET firstname='$FName' , lastname='$LName' , PatientPhone='$PatientPhone' , FacName='$FacName' , FacAddress='$FacAddress' , PatRoom='$PatRoom' , FacCity='$FacCity' , FacState='$FacState' , FacZip='$FacZip' , FacPhone='$FacPhone' , CaregiverName='$CaregiverName' , CaregiverContactPhone='$CaregiverContactPhone' , MedDispenseTime1='$MedDispenseTime1' , MedDispenseTime2='$MedDispenseTime2' , MedDispenseTime3='$MedDispenseTime3' , MedDispenseTime4='$MedDispenseTime4' , MedDispenseTime5='$MedDispenseTime5' , MedDispenseTime6='$MedDispenseTime6' , Month='$Month' , Year='$Year' WHERE PatientID='$id";

Where is $query set? The line above is executing the query, without capturing the result. Looks like it should be replaced with:

$query = " UPDATE Liberty SET firstname='$FName' , lastname='$LName' , PatientPhone='$PatientPhone' , FacName='$FacName' , FacAddress='$FacAddress' , PatRoom='$PatRoom' , FacCity='$FacCity' , FacState='$FacState' , FacZip='$FacZip' , FacPhone='$FacPhone' , CaregiverName='$CaregiverName' , CaregiverContactPhone='$CaregiverContactPhone' , MedDispenseTime1='$MedDispenseTime1' , MedDispenseTime2='$MedDispenseTime2' , MedDispenseTime3='$MedDispenseTime3' , MedDispenseTime4='$MedDispenseTime4' , MedDispenseTime5='$MedDispenseTime5' , MedDispenseTime6='$MedDispenseTime6' , Month='$Month' , Year='$Year' WHERE PatientID='$id";

I am a newby so I don't understand your statement "Where is $query set? The line above is executing the query, without capturing the result"

If you use code tags in future, I could refer to specific lines.

The following line uses the variable $query. This variable doesn't appear to be set, and is therefore likely the cause of your error.

$ret = mysql_query($query) or die(mysql_error());

Store the mysql query inside $query and it should do the trick.

$query = "UPDATE Liberty SET firstname='$FName' , lastname='$LName' , PatientPhone='$PatientPhone' , FacName='$FacName' , FacAddress='$FacAddress' , PatRoom='$PatRoom' , FacCity='$FacCity' , FacState='$FacState' , FacZip='$FacZip' , FacPhone='$FacPhone' , CaregiverName='$CaregiverName' , CaregiverContactPhone='$CaregiverContactPhone' , MedDispenseTime1='$MedDispenseTime1' , MedDispenseTime2='$MedDispenseTime2' , MedDispenseTime3='$MedDispenseTime3' , MedDispenseTime4='$MedDispenseTime4' , MedDispenseTime5='$MedDispenseTime5' , MedDispenseTime6='$MedDispenseTime6' , Month='$Month' , Year='$Year' WHERE PatientID='$id'";
mysql_query(" UPDATE Liberty

should be

$query=(" UPDATE Liberty
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.