[SOLVED] UPDATE not working...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

AliasBDI
Forum Contributor
Posts: 286
Joined: Fri Nov 15, 2002 10:35 am
Location: Spring, TX, USA

[SOLVED] UPDATE not working...

Post by AliasBDI »

My UPDATE does not work, but it is not giving an error. Is there a way to check it? Here is the UPDATE code:

Code: Select all

$query = "UPDATE user_info SET username = '$username', email = '$email', password = '$password', password_decrypted = '$password_decrypted', level = '$level', active = '$active' WHERE id = '$id' "; $sql = mysql_query($query) or die("ERROR");
I commented that out and echoed it in order to check it. It gave me everything correctly. I then queried it against the database in PHPMyAdmin and it worked correctly. So why would the form not work?
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post by tim »

echo the query var out n see what it says. you may need to incorporate some globals from the $_POST
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

What does echo $query; output exactly ? Also might want to do or die(mysql_error()); whilst testing so if there was to be an error then you'll see what it is.

[edit] shooter tim beat me to it! ;)
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post by tim »

Wow mark, i think this is the first time I beat you to a post reply.

Wow wow wow, i got feyd two days ago, now you. Make way cause i'm moving to higher grounds

/ rant :roll:
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

[php_man]mysql_info[/php_man] may help..
AliasBDI
Forum Contributor
Posts: 286
Joined: Fri Nov 15, 2002 10:35 am
Location: Spring, TX, USA

Post by AliasBDI »

I added the "or die(mysql_error());" but it did not show an error.

I echoed the query out and it worked fine:

Code: Select all

UPDATE user_info SET username = 'user', email = 'email@domainname.com', password = 'md5password', password_decrypted = 'password', level = '2', active = 'Y' WHERE id = '1'
*I altered the variable results for privacy*
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

If the query is correct, and there's no error what makes you think it isn't working?
I mean is your PHP code suggesting it isn't working? Or are you checking the database row via phpMyAdmin/whatever immediately after running to query to check if the update happened?
AliasBDI
Forum Contributor
Posts: 286
Joined: Fri Nov 15, 2002 10:35 am
Location: Spring, TX, USA

Post by AliasBDI »

I'm actually querying the database again to display the record. AND checking it in PHPMyAdmin as well.

Here is all of the code. But it all looks fine to me. Does it matter that I'm on a Windows server?

Code: Select all

<?php
//session_start();
include '../executions/connection_db.php';

// Define post fields into simple variables.  These are called CONSTANTS.
$username = $_POST['username'];
$oldusername = $_POST['oldusername'];
$email = $_POST['email'];
$level = $_POST['level'];
$password_decrypted = $_POST['password'];
$password = md5($password_decrypted);
$active = $_POST['active'];
$id = $_POST['id'];

/* Lets strip some slashes in case the user entered
any escaped characters. */

$username = stripslashes($username);
$email = stripslashes($email);
$level = stripslashes($level);
$active = stripslashes($active);

/* Do some error checking on the form posted fields to make sure they are completed */
if((!$username) || (!$email)){
	include 'update.php'; // Show the form again!
	/* End the error checking and if everything is ok, we'll move on to
	 creating the user account */
	exit(); // if the error checking has failed, we'll exit the script!
}

/* Let's do some checking and ensure that the user's email address or username or driver's license
 does not exist in the database */

 $sql_username_check = mysql_query("SELECT username FROM user_info WHERE username='$username' AND domain='$domain'");

// if check username
if ($username == $oldusername) { 
     $username_check = 0; 
} else { 
     $username_check = mysql_num_rows($sql_username_check); 
} // end if check username 

if($username_check > 0){
 	echo "Error1"; //include '_alert_users.php';	
	if($username_check > 0){
 		echo "Error2"; //include '_alert_users.php';
 		unset($username);
 	}
 	echo "Error3";
 	exit();  // exit the script so that we do not create this account!
}

/* Everything has passed both error checks that we have done.
It's time to create the account! */

// Enter info into the Database.
//echo "UPDATE user_info SET username = '$username', email = '$email', password = '$password', password_decrypted = '$password_decrypted', level = '$level', active = '$active' WHERE id = '$id' "; $sql = mysql_query($query) or die("ERROR DUDE"); 
//exit();
$query = "UPDATE user_info SET username = '$username', email = '$email', password = '$password', password_decrypted = '$password_decrypted', level = '$level', active = '$active' WHERE id = '$id' "; $sql = mysql_query($query) or die(mysql_error()); 

if(!$sql){
	echo "There has been an error updating your account. Please contact the webmaster.";
} else {
	
// update completed, go to userindex.
header('Location: index.php');
}

?>
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Weird. Might be worth creating a simple test script that ONLY runs that hardcoded query update, then check the database. If that works then maybe the problem is elsewhere in the code *shrug*
AliasBDI
Forum Contributor
Posts: 286
Joined: Fri Nov 15, 2002 10:35 am
Location: Spring, TX, USA

Post by AliasBDI »

Good idea... I did that and BANG, it worked. I guess that means.... what DOES that mean?

I echoed both the manual query and the variable query and it turned out identical, except that the id was bold on the variable query. It looked like this:
UPDATE user_info SET username = 'rgraph1', email = 'jabshire@resonantgraphics.com', password = '5fcfd41e547a12215b173ff47fdd3739', password_decrypted = 'trustno1', level = '2', active = 'Y' WHERE id = '1'

UPDATE user_info SET username = 'rgraph1', email = 'jabshire@resonantgraphics.com', password = '5fcfd41e547a12215b173ff47fdd3739', password_decrypted = 'trustno1', level = '2', active = 'N' WHERE id = '1'
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

So, another check. On your original code/script put an exit(); right after doing:
mysql_query($query) or die(mysql_error()); and go and check the database to see if the update worked ok.
AliasBDI
Forum Contributor
Posts: 286
Joined: Fri Nov 15, 2002 10:35 am
Location: Spring, TX, USA

Post by AliasBDI »

Tried it and the UPDATE did not work. I did the variable query not the manual one (I figured you were referring to it).
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

this is probably a good time to add sanitizing code for the $_POST variables you are adding.. since you say the variable version showed up as bold'd id, maybe your submitting some slightly mangled data?
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Hmm..even weired. So the final test would be to use the hardcoded update query in your original script, i.e comment out the variable one and put the hardcoded one in. If that works then the query must be at fault, otherwise i have no idea as it makes no sense (to me anyway) :o
AliasBDI
Forum Contributor
Posts: 286
Joined: Fri Nov 15, 2002 10:35 am
Location: Spring, TX, USA

Post by AliasBDI »

I tried this:

Code: Select all

echo "UPDATE user_info SET username = '$_POST['username']', email = '$_POST['email']', password = '$_POST['password']', password_decrypted = '$_POST['password']', level = '$_POST['level']', active = '$_POST['active']' WHERE id = '$_POST['id']' "; 
exit();

I got this error:
Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in c:\domains\resonanthosting.com\wwwroot\econtrol\users\_update_user.php on line 58
Post Reply