Click here to Skip to main content
15,881,027 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a area where admin can change a users details or delete a user, the delete statement is working well, update on the other hand is not updating.

when i echo out the update statement I get :
SQL
UPDATE users SET username='superman', dob='0000-00-00', location='The Daily Planet ', group='2' WHERE id='136'


so I know the data is begin process correctly, its just not updating the data itself.

My update statment
PHP
require 'core/init.php';
$username = mysql_real_escape_string($_POST["username"]);
$dob = mysql_real_escape_string($_POST["dob"]);
$location = mysql_real_escape_string($_POST["location"]);
$group = mysql_real_escape_string($_POST["group"]);
$user_id = (int)$_POST['id'];

    $result = mysql_query("UPDATE users
              SET username='$username', 
                  dob='$dob',
                  location='$location',
                  group='$group'
              WHERE 
              id='$user_id'");

  echo  "UPDATE users
              SET username='$username', 
                  dob='$dob',
                  location='$location',
                  group='$group'
              WHERE 
              id='$user_id'"

    
            
	//header("location:admin.php");


To me this looks fine, but something it preventing the data entering the database.
Posted
Comments
Ron Beyer 21-Jan-14 16:40pm    
Is "id" a string or a number, shouldn't it be something like: id=$user_id without the single quotes?
Maciej Los 21-Jan-14 16:44pm    
Good question!
OriginalGriff 21-Jan-14 16:48pm    
Post it as the solution!
:thumbsup:
Ron Beyer 21-Jan-14 16:50pm    
Done!
Ben Oats 21-Jan-14 17:12pm    
id is a int

As per OG:

I'm not a PHP guy, but it looks like you are trying to set the where of a numeric field using a string constant. I think your query should look something like:

PHP
$result = mysql_query("UPDATE users
              SET username='$username', 
                  dob='$dob',
                  location='$location',
                  group=$group
              WHERE 
              id=$user_id");


Without the quotes on the $user_id (and I also removed them on $group since that looks numeric too).
 
Share this answer
 
Comments
Maciej Los 21-Jan-14 16:51pm    
+5!
Ron Beyer 21-Jan-14 16:51pm    
Thanks!
Ben Oats 21-Jan-14 17:12pm    
thanks for trying, and i think you are write about integers, but its still not updating.
Ron Beyer 21-Jan-14 17:24pm    
Have you tried running the raw query text in a MySql management tool?
Ben Oats 21-Jan-14 17:52pm    
seems that group='$group'; is affecting the query. ive checks the name matches the group in the db and its fine.

Update statement working but data not updating means
1. conditional data is going wrong (here id is int so ur condition should be id=136 not id='136' and 136 value isnot in table)
 
Share this answer
 
v2
back ` fixes problem.

SQL
UPDATE `users`
SET `username` = '$username', `dob` = '$dob',`location` = '$location',`group` = '$group'  
WHERE `id` = $user_id
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900