View Full Version : why wont my name update?
Flumps 10-08-2008, 10:56 AM im trying to update a firstName in a database here is query..
$update = mysql_query("UPDATE phoneList SET firstName = $_POST[NewFirstName] WHERE firstName = $_POST[OldFirstName]");
if (!$update)
{
die('Could not update database: ' . mysql_error());
}
mysql_close($con);
the error I get is:
Could not update database: 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 '' at line 1
any ideas?
basically im trying to update the name from form. think ive just wrote the wrong code completely so if anyone could help me out that would be great thanks.
webcertain 10-08-2008, 11:09 AM you arent escaping your fields with '' ?
arbet 10-08-2008, 11:26 AM $update = mysql_query("UPDATE phoneList SET firstName = $_POST['NewFirstName'] WHERE firstName = $_POST['OldFirstName']");
You need to add single quotes to the items inside brackets. As a tip, if your query is not working, try executing it directly in phpmyadmin, and change it as needed.
ThatScriptGuy 10-08-2008, 11:32 AM First of all, your phoneList table should have a Primary Key - usually something like 'id'. Second of all, you should be escaping your data, at a minimum, with mysql_real_escape_string. So, if you had a primary key, and were escaping your data, your code might look something like this:
$id=(int)mysql_real_escape_string($_POST['id']);
$firstName=mysql_real_escape_string($_POST['NewFirstName'];
$update=mysql_query("UPDATE `phoneList` SET `firstName`='$firstName' WHERE `id`='$id'");
larwilliams 10-08-2008, 12:08 PM im trying to update a firstName in a database here is query..
$update = mysql_query("UPDATE phoneList SET firstName = $_POST[NewFirstName] WHERE firstName = $_POST[OldFirstName]");
if (!$update)
{
die('Could not update database: ' . mysql_error());
}
mysql_close($con);
the error I get is:
any ideas?
basically im trying to update the name from form. think ive just wrote the wrong code completely so if anyone could help me out that would be great thanks.
Assuming that the data is indeed in post and the names are correct, here is the code you would use:
$NewFirstName = mysql_real_escape_string($_POST['NewFirstName']);
$OldFirstName = mysql_real_escape_string($_POST['OldFirstName']);
$update = mysql_query("UPDATE phoneList SET firstName = '$NewFirstName' WHERE firstName = '$OldFirstName'");
if (!$update){
die('Could not update database: ' . mysql_error());
}
mysql_close($con);
Flumps 10-08-2008, 12:26 PM Assuming that the data is indeed in post and the names are correct, here is the code you would use:
$NewFirstName = mysql_real_escape_string($_POST['NewFirstName']);
$OldFirstName = mysql_real_escape_string($_POST['OldFirstName']);
$update = mysql_query("UPDATE phoneList SET firstName = '$NewFirstName' WHERE firstName = '$OldFirstName'");
if (!$update){
die('Could not update database: ' . mysql_error());
}
mysql_close($con);
the above runs through without errors but for some reason it doesnt update the the database with the new value for example I had a name: test and I wanted to change it to testing but it stayed at test.
ThatScriptGuy 10-08-2008, 12:30 PM Are you positive that 'OldFirstName' is actually a variable? More than likely, you're forgetting to create a hidden form element in your form that posts to this page. The hidden element would be called 'OldFirstName' and would have a value of the user's current first name. Perhaps try
echo $_POST['NewFirstName'] . '<br />' . $_POST['OldFirstName'];
exit;
at the top of your processing script to verify that the OldFirstName is even being sent.
PS - You really should be using a Primary, auto-incrementing key for this sort of thing.
larwilliams 10-08-2008, 12:40 PM Are you positive that 'OldFirstName' is actually a variable? More than likely, you're forgetting to create a hidden form element in your form that posts to this page. The hidden element would be called 'OldFirstName' and would have a value of the user's current first name. Perhaps try
echo $_POST['NewFirstName'] . '<br />' . $_POST['OldFirstName'];
exit;
at the top of your processing script to verify that the OldFirstName is even being sent.
PS - You really should be using a Primary, auto-incrementing key for this sort of thing.
I agree, especially since it is possible multiple people could have the same first name and the OP's query would change all of them, not just the one he intends it to.
vibrokatana 10-08-2008, 03:36 PM I agree, especially since it is possible multiple people could have the same first name and the OP's query would change all of them, not just the one he intends it to.
When doing update it is best to use the unique primary key. Otherwise you often end up with results that are going to be a bit funny (even if you don't catch it immediately).
Flumps 10-08-2008, 04:50 PM When doing update it is best to use the unique primary key. Otherwise you often end up with results that are going to be a bit funny (even if you don't catch it immediately).
i do have an id field and it set to primary... if this helps?
Flumps 10-08-2008, 04:57 PM First of all, your phoneList table should have a Primary Key - usually something like 'id'. Second of all, you should be escaping your data, at a minimum, with mysql_real_escape_string. So, if you had a primary key, and were escaping your data, your code might look something like this:
$id=(int)mysql_real_escape_string($_POST['id']);
$firstName=mysql_real_escape_string($_POST['NewFirstName'];
$update=mysql_query("UPDATE `phoneList` SET `firstName`='$firstName' WHERE `id`='$id'");
ah this worked. thank you :)
ThatScriptGuy 10-08-2008, 05:47 PM Good to hear. Always always use your unique key to update your records (ID in this case) or you will end up eventually modifying or deleting multiple records.
Flumps 10-08-2008, 05:49 PM Good to hear. Always always use your unique key to update your records (ID in this case) or you will end up eventually modifying or deleting multiple records.
yep noted :)
thank you. learning php at a nice steady pase :)
|