I'm attempting to make a form for a charity that allows staff members to view and update victim records that are stored in the SQL Database via the website. I have managed to display the records and create a form that allows the alteration of the records, however when I enter variables I receive a SYNTAX error.
$updateSQL="UPDATE Victims SET victimFName=".$victimFN." WHERE victimId=".$id."";
$exeupdateSQL= mysql_query($updateSQL) or die (mysql_error());
echo "The Record has been updated";
I receive the error: 'You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE victimId=' at line 1'
I then researched SQL Injections and changed the code:
$updateSQL="UPDATE Victims SET victimFName=".mysql_real_escape_string($_POST['victimFName']).";
WHERE victimId=".mysql_real_escape_string($_POST['victimId'])."";
exeupdateSQL= mysql_query($updateSQL) or die (mysql_error());
echo "The Record has been updated";
This still didnt work.
I have attempted to replace my variables $VictimFN and $id by entering data into the SQL query and the code works, updating the record. Such as:
$updateSQL="UPDATE Victims SET victimFName='Mary Smith' WHERE victimId='1'";
$exeupdateSQL= mysql_query($updateSQL) or die (mysql_error());
echo "The Record has been updated";
I am fairly new to programming and was wondering how I could fix this as the issue is to do with my variables.
Thanks to your help I have established the problem with the code and have now fixed it to:
$updateSQL="UPDATE Victims SET victimFName='".mysql_real_escape_string($_POST['victimFName'])."' WHERE victimId='".mysql_real_escape_string($_POST['victimId'])."'";
$exeupdateSQL=mysql_query($updateSQL) or die (mysql_error());
echo "The Record has been updated";
echo "<br><a href=ViewVictimRequest.php>View Updated Record</a>";
There are now no errors, however the records state they are updated when they are not. Is there anyway to fix this?
Thank you in advance for your response and sorry for the inconvenience!