-1

Update query is not reflected into database . This is related to PHP and PDO method of connection. The query I have written is ,

try {
  $conn = new PDO('mysql:host=localhost;dbname=***; charset=utf8','root','****[enter image description here][1]');
  $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  $sql = "SELECT * FROM providers WHERE password = '$oldpass'";
  $statement = $conn->prepare($sql);  
  $statement->execute();  
  $providers = $statement->fetchAll();
  
  if(($providers[0]["password"]=="$oldpass") and ("$pass"=="$cpass")){
    
    $statement = $conn->prepare('update providers set password="$cpass" where password="$oldpass"');  
    $condition=$statement->execute();
    
    echo '<script>alert("Password changed")</script>';
  }
  else{
    echo '<script>alert("Incorrect old password");</script>';
  }
        }
        catch(PDOException $e) {
            echo "Connection failed: " . $e->getMessage();
        }
            $conn = null;
        }

EDIT The message on entering corresponding credentials is Password changed.

Is there anything wrong with the query?? Because it does not show the updated password in database (phpMyAdmin)

Mureinik
  • 297,002
  • 52
  • 306
  • 350
Sara
  • 3
  • 6
  • 3
    Please note that the way you're building your queries is unsafe. You're open to [SQL injection](https://stackoverflow.com/questions/332365/how-does-the-sql-injection-from-the-bobby-tables-xkcd-comic-work). You're defeating the purpose of preparing by directly inserting the parameters. Since you're already using PDO, it's easy for you to parametrize your queries. – El_Vanja Nov 28 '20 at 17:43
  • Are there any errors? If you don't see them, here's a thorough [guide](https://stackoverflow.com/a/22662582/4205384) on debugging db related problems. – El_Vanja Nov 28 '20 at 17:45

1 Answers1

2

The changes you've made will only be visible inside the same transactions, and would be implicitly rolled back when you close the connection. In order to persist them in the database, you need to commit them:

$conn->commit();
Mureinik
  • 297,002
  • 52
  • 306
  • 350