Day 9
» The standard UPDATE statement.UPDATE table
SET field1=‘val1’, field2=val2
WHERE condition
» Multiple table UPDATE.UPDATE table1, table2
SET table1.field1=‘val1’, table2.field2=val2
WHERE condition
» Can also use the mysql_affected_rows() function to retrieve number of records updated.
» With UPDATE, mysql_affected_rows() will return 0 if no changes were made to the records. This does not mean the query didn’t work, only that the new value was the same as the old value.
» Form Page<form action=“process.php” method=“post”>
<input type=“text” name=“email”
value=“<?php echo $email; ?>”>
<input type=“hidden” name=“did_update” value=“1”>
<input type=“hidden” name=“user_id” value=“<?php echo $user_id; ?>”>
<input type=“submit” value=“Change Email”>
</form>
» Processing Page<?php if ($_REQUEST[‘did_update’] == 1){
$email = $_REQUEST[‘email’];$user_id = $_REQUEST[‘user_id’];$query = “UPDATE users SET email = ‘$email’
WHERE user_id = $user_id LIMIT 1”;$result = mysql_query($query);if (mysql_affected_rows($result) == 1){
echo ‘Email updated’;}else{
echo ‘Email not updated.’;}
}?>
» Single table DELETE syntax.DELETE FROM table WHERE condition
» Multiple table DELETE syntax.DELETE table1.field, table2.*
FROM table1, table2
WHERE condition
» The limit command allows you to limit the amount of records effected to the number after the command.
SELECT ... FROM ... WHERE ... LIMIT 10
» Now it’s your turn.
1. Create a Blog using your understanding of PHP and MySQL.
2. Save the files in a Blog folder.3. Confirm the pages works in a browser.4. Revel in your programming glory.