MySQL多个查询不起作用

MySQL多个查询不起作用

问题描述:

Problem has been solved

I have created a form that processes the changing of user information from the admin side e.g. the admin changes a user's username and/or email. I am having trouble processing multiple queries.

For example, if the admin changes the username, the query works. If the admin changes the email address, the query works. But if the admin changes the username and email at the same time through the form then only the username changes.

Any ideas? I will submit my code but I will change variables for security reasons etc. Also, anything in capitals has been changed for security reasons. The code is all correct for each individual function because as I said, if I ONLY change the email, it works and actually changes. But if I change the username AND email, only the username will change despite the fact the email query runs and it echo's the email has been changed!

Also, it is worth noting that all of the fields e.g. username field and email field are part of one form that submits to one page.

if (isset($_POST['SUBMIT_BUTTON_PRESSED'])) {
//Gather all inputs from the form and sanitise it.
//REMOVED FOR SECURITY REASONS.

if($USERNAME_NEW != "") {
    if($USERNAME_NEW == $CURRENT_USERNAME) {
        echo "You have entered the username you are already using. Please enter a different username.";
    } else {
    $CHECK_USERNAME = "SELECT USERNAME_ROW FROM USERS_TABLE WHERE username='$USERNAME_NEW'";
    $RUN_QUERY = mysqli_query($CONNECTION INFO, $CHECK_USERNAME);
    $RESULT = mysqli_num_rows($RUN_QUERY);
    if($RESULT > 0) {
    echo "That username already exists. You cannot use that username again. Please enter another username.";    
    } else {
    $editing_username = true;
    $USERNAME = $NEW_USERNAME; //NOT NEEDED BUT IT STILL WORKS
    $THE_SQL_QUERY = "UPDATE USER_TABLE SET username='$USERNAME' WHERE username='$ORIGINAL USERNAME'";
    $RUN_THIS_QUERY= mysqli_query($CONNECTION INFO, $THE_SQL_QUERY);
    echo "The user's username has been changed to:  ". $USERNAME;
        }
    }
}
     if($EMAIL != "") {
        if($EMAIL == $CURRENT_EMAIL) {
            echo "You have entered the same email address to the one you are already using. Please enter a different email address.";
        } else {
            $CHECK_EMAIL = "SELECT USERS_EMAIL FROM USER_TABLE WHERE username='$USER'";
            $CHECK_EMAIL_QUERY = mysqli_query($CONNECTION_INFO, $CHECK_EMAIL);
            $RESULT = mysqli_num_rows($CHECK_EMAIL_QUERY);
            if($RESULT > 0) {
            echo "That email already exists. You cannot use that username again. Please enter another username.";   
            } else {
                $editing_email = true;
                $THE_NEW_EMAIL = $FINAL_EMAIL_THING; // AGAIN NOT NEEDED BUT STILL WORKS
                $THE_SQL= "UPDATE USER_TABLE SET USER_EMAIL='$EMAIL' WHERE username='$USER' LIMIT 1"; // REMOVED THE LIMIT 1, STILL DOESN'T WORK
                $RUN_THIS_QUERY = mysqli_query($CONNECTION, $THE_SQL);
                if($RUN_THIS_QUERY) {
                echo "The user's email has been changed."; // EVEN WHEN BOTH FIELDS ARE SUBMITTED THIS WORKS SO THE QUERY IS RUNNING BUT THE EMAIL DOESN'T CHANGE
                    }
                }
            }
        }

Thanks for the help! Also, no un-witty remarks about how my question is structured etc. because I don't care to be honest. I just want this code working to be honest because I've been working on it for a while. This may be something simple or I might be using the wrong approach for this type of form submission.

Remember: THIS CODE DOES WORK WHEN I SUBMIT EACH FIELD SEPARATELY!

Its very hard to figure out as you are not producing the real code.

I think you have missed something here. As you are using USER_NAME as key in the SQL's, make sure that you are using the updated username in the second sets of SQL (to update the email) as they are already replaced by the first SQL.

And there is no security risk while showing your codes snippets to someone else. Hide only the username/passwords or Identities. :)