简体   繁体   中英

mysql_query update

I have a script which runs when a user hits the 'reject' button on some terms. When it runs it should revoke the access rights to the particular area, for some reason the script doesn't work- as in it doesn't update the MYSQL DB nor do I get die error.

$username =  $_SESSION["USER"]["Username"]; 

mysql_connect("x", "y", "z") or die(mysql_error()); 
mysql_select_db("database") or die(mysql_error()); 
mysql_query("UPDATE `users` SET `SeceretArea`='Unauth' WHERE `Username`='$username'") or die(mysql_error());

ANSWER UPDATED TO SUM UP DEBUGGING PROCESS.

If there is no error, then there are three possibilities as to why the database entry isn't happening:

  • There is no entry in your table where the USERNAME column is equal to $database .
  • There is no value being passed to $database .
  • There is a value being passed to $database , but that value isn't being replaced into the query.

From my personal experience, mysql_query() doesn't necessarily substitute variable values in before executing the query. Create the query string first into a variable, then pass it to mysql_query() . Your current setup doesn't replace $username with its value. What you can do is this:

...
$sql = "UPDATE `users` SET `SeceretArea`='Unauth' WHERE `Username`='$username'";
mysql_query($sql) or die(mysql_error());

Apart from that, check your table to make sure that there is a row with the value you want in USERNAME . var_dump() your $username to see if it's the value that you intended.

Also, you really should consider switching to either mysqli or PDO . If you don't want to migrate to an object module, you can use mysqli 's procedural functions.

You will want to make sure you call session_start(); at the top of your script. Without it, your $username variable will never get set.

Make sure the $username carries the session value you passed.

echo "$username";

Proceed to query only when you get it correct.

And also, make sure the field name of the table is correct. I noticed SeceretArea which possibly is SecretArea in your table.

Just a few thoughts:

  • Is there an entry in the DB with the exact value for UserName you are WHEREing?
  • Are you inside an uncommitted transaction?
  • Does your SQL statement interfer with another table with the same name?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM