Jump to content

One field not updating after UPDATE query


Go to solution Solved by Jacques1,

Recommended Posts

Hi there.  I'm totally new (about a week!) with php and mysql and am encountering a problem that perhaps someone can help me with?

I've looked through to see if a similar problem has appeared or been solved, but without success, so apologies if I am repeating something. 

 

In php I am trying to update 7 fields from a form from which a user has edited/modified any of the fields in a chosen record (except id).

 

Here is the code:

 

$id=$_GET['id'];
 
$task=$_POST['task'];
$category=$_POST['category'];
$created=$_POST['created'];
$state=$_POST['state'];
$due=$_POST['due'];
$repeat=$_POST['repeat'];
$completed=$_POST['completed'];
 
mysql_select_db($database) or die( "Unable to select database for updating");
 
mysql_query("UPDATE tasks SET Task='$task',Category='$category', Status='$state', Created='$created', Due='$due',Completed='$completed', Repeat='$repeat' WHERE id = '$id'");
 
mysql_close();
 
No updating of any field occurs, so after playing around a bit  I found that if I removed the Repeat section everything (except repeat of course) was updated successfully :
 
mysql_query("UPDATE tasks SET Task='$task',Category='$category', Status='$state', Created='$created', Due='$due',Completed='$completed' WHERE id = '$id'");
 
I also added:
 
echo "$repeat";
 
at the end and that shows that the variable $repeat does contain the new value from the form.
Adding another update query:
 
mysql_query("UPDATE tasks SET Repeat='$repeat' WHERE id = '$id'");
 
resulted in the repeat field not being updated.
 
 
Any thoughts?
 

"Repeat" is a mysql reserved keyword.  You need to wrap your table column names in `backticks` (the key to the left of the 1 on the keyboard).  Backticks tell the query that what's between them is not to be considered a reserve word.

  • Solution

Please don't use backticks. We've discussed this multiple times, and the bottom line is: Backticks are harmful.

 

In addition to that, you'd have to add them every single time you access the column, and if you ever forget it, you'll again wonder what the hell is wrong.

 

The solution is to not use reserved words as identifiers.

Edited by Jacques1
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.