2

I am trying to make an update query to update a user to have a password. The update statement is extremely easy and it has been baffling me for about 12 hours now.

I have read everything having to do with this on 3.5 pages of google searches. but for some reason, none of the suggestions work for me!

Here is the UPDATE query in its 'original' form:

$sql_update = "UPDATE users_sensitive SET password = '$password_hash', ch_password = '$password_hash' WHERE email_hash = '$email_hash'";
$result_update = mysql_query($sql_update) or die(mysql_error());

When I do this update Query, I get no errors or anything back. It also just does not update.

Here's another rendition of the same code:

$sql_update = "UPDATE users_sensitive SET password = '" . $password_hash . "', ch_password = '" . $password_hash. "' WHERE email_hash = '" . $email_hash . "'";
$result_update = mysql_query($sql_update) or die(mysql_error());

Again, nothing happens.

When I put the actual numbers in here instead of the php variables:

$sql_update = "UPDATE users_sensitive SET password = '700b5b23b511d974fe9eeb17ad350b33', ch_password = '700b5b23b511d974fe9eeb17ad350b33' WHERE email_hash = 'ac24dab060a172d8c0b3679d8ae61cac'";
$result_update = mysql_query($sql_update) or die(mysql_error());

(don't worry, it's not sensitive info) It does actually update...

So, I'm assuming my syntax is wrong? I know these are Strings instead of just numbers, so I need the single quotes around them. I have the two variables I need echoed and they are both showing exactly what they should be. I have even tried to use backticks around the column name but that didn't do anything?

I did a var_dump and it came back "true".

When I do a print on my $sql_update, I get:

UPDATE users_sensitive SET password = '700b5b23b511d974fe9eeb17ad350b33', ch_password = '700b5b23b511d974fe9eeb17ad350b33' WHERE email_hash = 'ac24dab060a172d8c0b3679d8ae61cac'

There is no whitespace here. When I print the $result_update, it comes up with: 1

ANSWER Thank you to bemace, James Anderson, VolkerK, alex and the others!

The problem was that there was a strange line break in the code creating whitespace that was not the same as on the database.

After backtracking, I noticed at the very beginning of my code on this page, I used a GET to get a hash number from the URL. While making a variable, during testing, I added a line break to the variable for testing purposes only. I (stupidly) left the line break in there. When using that variable in a hidden form field, it included the line break.

After taking the line break out of the first line, everything matched up and all is good.

If you are reading this and plan on posting later, Posting the entire code is probably worth it. This problem would have been fixed earlier if I had.

Again, Thank you to the quick responses and trouble shooting!

8
  • You have used mysql_real_escape_string() on those variables before interpolating them? Commented Jun 29, 2011 at 5:43
  • 1
    Did you check the variables in the where condition to fit your expectations ? Commented Jun 29, 2011 at 5:43
  • What it says var_dump($result_update)? Commented Jun 29, 2011 at 5:43
  • Did you actually print out $sql_update and not just the variables? What does $result_update contain (true?)? What does mysql_affected_rows() say? Commented Jun 29, 2011 at 5:44
  • Try a var_dump($sql_update) perhaps? Commented Jun 29, 2011 at 5:44

2 Answers 2

3

No errors and no updates tells me that your where clause isn't matching anything. Make sure $email_hash doesn't have any leading or trailing whitespace and isn't being truncated.

A less likely possibility is that the update is part of a transaction that is being rolled back.

Another less likely possibility: are you connected to the right server?

Sign up to request clarification or add additional context in comments.

16 Comments

I did a var_dump and it came back "true". When I do a print on my $sql_update, I get: UPDATE users_sensitive SET password = '700b5b23b511d974fe9eeb17ad350b33', ch_password = '700b5b23b511d974fe9eeb17ad350b33' WHERE email_hash = 'ac24dab060a172d8c0b3679d8ae61cac' - There is no whitespace here. When I print the $result_update, it comes up with: 1
what happens with select * from users_sensitive where email_hash='$email_hash'?
From my quick tests, Nothing happens... My code is: $sql_select = "SELECT * FROM users_sensitive WHERE email_hash='$email_hash'"; $result_select = mysql_query($sql_select); echo "test1"; while ($row = mysql_fetch_array($result_select)) { echo $user_sensitive_id = $row['user_sensitive_id']; echo $user_id = $row['user_id']; echo "test"; } and I only receive the "test1" as though it's not seeing email_hash. I have checked MULTIPLE times and the email hash exists in the database!
@Nathan - so we're back to the where clause not matching anything. Are you sure phpMyAdmin or whatever you're using to inspect the table is connected to the same server and database as your php code?
Also, yes - I include a database connect outside script. Every page connects exactly the same and all pages are working correctly. After checking DB names, Everything is exactly the same.
|
0

Thank you to bemace, James Anderson, VolkerK, alex and the others!

The problem was that there was a strange line break in the code creating whitespace that was not the same as on the database.

After backtracking, I noticed at the very beginning of my code on this page, I used a GET to get a hash number from the URL. While making a variable, during testing, I added a line break to the variable for testing purposes only. I (stupidly) left the line break in there. When using that variable in a hidden form field, it included the line break.

After taking the line break out of the first line, everything matched up and all is good.

If you are reading this and plan on posting later, Posting the entire code is probably worth it. This problem would have been fixed earlier if I had.

Again, Thank you to the quick responses and trouble shooting!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.