One of the important MySQL queries that need to be done in PHP is UPDATE. As the command suggests, it enables you to update records in the MySQL table, without necessarily inserting a row again. For example, you have the following data table below:
Assuming Peter Patter would like to change his password from "logan" to "weaponx," the MySQL query using UPDATE would be: <?php //Step 1 Connect to database $username = "yourusername"; $password = "yoursqlpassword"; $hostname = "localhost"; $table = "usertable"; $database = "userdatabase"; $dbhandle = mysql_connect($hostname, $username, $password) or die("Unable to connect to MySQL"); $selected = mysql_select_db($database,$dbhandle) or die("Could not select $database"); //Initial variables $username = 'wolverine'; $fullname = 'Peter Patter'; //Set new password for Peter Patter $newpassword = 'weaponx'; //sanitize $newpassword = mysql_real_escape_string(stripslashes($newpassword)); $username = mysql_real_escape_string(stripslashes($username)); $fullname = mysql_real_escape_string(stripslashes($fullname)); //Update records in MySQL database mysql_query("UPDATE usertable SET password = '$newpassword' WHERE username = '$username' AND fullname = '$fullname'") or die(mysql_error()); echo 'The database has been updated. Thank you'; ?> The update script above will change the password of Peter Patter from "logan" to "weaponx." The script above is just a raw example; you can make the above PHP script work with HTML forms for more interactive updating of data.
blog comments powered by Disqus |
|
|
|
|
|
|
|