IChaps Posted April 6, 2016 Share Posted April 6, 2016 Hi, I'm currently experiencing difficulties updating the database. I'm currently creating a 'Forgotten Password' feature. I've finished most of it, however when I attempt to update the database with a new password, nothing happens and the old password remains in the database. I've followed W3Schools example but I'm stuck. --------- The new password is generated by numbers 4 in total, ie $new_pass = (rand(1000,9999); and from the form I've used $user_email= $_POST["retrieve_email"]; then I use the line sql = "UPDATE registration SET Password=$new_pass WHERE Email=$user_email"; I can't get it to work, and I don't know why. Could anyone please write the code I need to make it work, or let me know how to proceed? Thank You. ps. I thought about deleting the entry and re-inserting it. Would that work (out of interest)? Link to comment Share on other sites More sharing options...
justsomeguy Posted April 6, 2016 Share Posted April 6, 2016 You have an error in your SQL query but it sounds like you're not checking for SQL errors. If you're just learning about PHP, I recommend that you skip the tutorials that teach you to try and put data into the query and instead study prepared statements and PDO: http://www.w3schools.com/php/php_mysql_prepared_statements.asp Then your code would look something like this: $query = $pdo->prepare('UPDATE registration SET Password=:pass WHERE Email=:email'); $query->execute([ ':pass' => $new_pass, ':email' => $user_email ]); Link to comment Share on other sites More sharing options...
IChaps Posted April 7, 2016 Author Share Posted April 7, 2016 Hello. Thank you justsomeguy for your reply. I've attached a specimen of mySQLi / php code (minus the database and login details). It appears to read from the database, display the queried email and old password. Then generate a new pass number and display this. But I can't get it to write back to / update the database. I have thought about deleting the entry and re-inserting it. I don't know enough about PDO at present. As I unable to figure out how to do this, Could I request that the required update line be written for me please? Thank You. Link to comment Share on other sites More sharing options...
justsomeguy Posted April 7, 2016 Share Posted April 7, 2016 Writing code for you isn't going to teach you how to program. Your code is not executing the query. You build a query in a string but you don't actually run the query. There are several examples of running update queries here, although these examples are not specific to update queries, they could be any kind of query: http://www.w3schools.com/php/php_mysql_update.asp Regardless of whether or not you're using mysqli or PDO, you need to use prepared statements. This page has some examples: http://www.w3schools.com/php/php_mysql_prepared_statements.asp For the code you have now, first you should print that query out and see what you're telling the database to do. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now