0

I have searched online for possible reasons of why a MYSQL update statement won't work but none of the solutions work on my case. What could be wrong in these lines of code?

     public function forgotPassword($email, $newpassword, $salt){
     $result = mysqli_query($this->db->con,"UPDATE users SET user_password = '$newpassword',salt = '$salt'
                      WHERE email = '$email'");

I have tried,

"UPDATE 'users' SET 'user_password' = '$newpassword','salt' = '$salt'
                      WHERE 'email' = '$email'"

I have also tried,

"UPDATE 'DBNAME'.'users' SET 'user_password' = '$newpassword','salt' = '$salt'
                      WHERE 'users'.'email' = '$email'"

The forgotPassword function works properly, only the UPDATE statement is not executed.

Here is the complete function

    public function forgotPassword($email, $newpassword, $salt){
$result = mysqli_query($this->db->con,"UPDATE `melobook_customers`.`users` SET `user_password` ='$newpassword',`salt` ='$salt'
                      WHERE `users`.`email` ='$email'");

     if ($result) {
      return true;
      }
   else
     {
     return false;
     }

     }
8
  • Are you getting an error message? Commented May 29, 2016 at 16:39
  • 1
    You're confusing backticks with inverted commas Commented May 29, 2016 at 16:40
  • @Strawberry good point, but his original query should still work as it contains no mysql reserved keywords. Commented May 29, 2016 at 16:41
  • @izzEps no I am not getting any errors. Commented May 29, 2016 at 16:42
  • have you confirmed that the email address for the user you are trying to update actually exists in the database? Commented May 29, 2016 at 16:43

1 Answer 1

1

I think the problem is within your query, you used table name within single quotes which should not be used, here you can see: 'users'. Instead of the single quotes you've to use `users` or you can leave that like this users. So your query will look something like this,

"UPDATE `users` SET `user_password` = '$newpassword',`salt` = '$salt' WHERE `email` = '$email'"

//I have also tried, Your second query will look something like this

"UPDATE `DBNAME`.`users` SET `users`.`user_password` = '$newpassword',`users`.`salt` = '$salt' WHERE `users`.`email` = '$email'"

Updated Code Based on your requirement

<?php
        class check_update{

            private $con;

            public function __construct(){
                $this->con = mysqli_connect("db_host", "db_user", "db_password", "db_name");
            }

            public function forgotPassword($email, $newpassword, $salt){
                $sql = "UPDATE `db_name`.`table_name` SET `table_name`.`password` ='$newpassword',`table_name`.`salt` = '$salt' WHERE `table_name`.`email` ='$email'";
                $result = mysqli_query($this->con, $sql);
                if ($result) {
                    return true;
                }else{
                    return false;
                }
            }
        }
        $check_update = new check_update();
        $check_update->forgotPassword("your_email", "new_password", "salt");
?>

Hope this will fix your problem.

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

3 Comments

Actually I've forgot to mention that you cant use single quotes anywhere in your query for table_name, database_name, column_names etc. You can use single quotes only for strings. Like below is the sample query for you. > UPDATE stackoverflow_question.test SET password = '$newpassword', salt = '$salt' WHERE email = '$email'
I have tried your solution, it hasnt worked. Could the problem be in the rest of the function? Check my edit
what is the value of $this->db->con property, have you seen that using var_dump($this->db->con); please check what it contains?

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.