Transcript
Page 1: Day 9. SELECT INSERT UPDATE DELETE » The standard UPDATE statement. UPDATE table SET field1=val1, field2=val2 WHERE condition » Multiple table UPDATE

Day 9

Page 2: Day 9. SELECT INSERT UPDATE DELETE » The standard UPDATE statement. UPDATE table SET field1=val1, field2=val2 WHERE condition » Multiple table UPDATE
Page 3: Day 9. SELECT INSERT UPDATE DELETE » The standard UPDATE statement. UPDATE table SET field1=val1, field2=val2 WHERE condition » Multiple table UPDATE

» 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

Page 4: Day 9. SELECT INSERT UPDATE DELETE » The standard UPDATE statement. UPDATE table SET field1=val1, field2=val2 WHERE condition » Multiple table UPDATE

» 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.

Page 5: Day 9. SELECT INSERT UPDATE DELETE » The standard UPDATE statement. UPDATE table SET field1=val1, field2=val2 WHERE condition » Multiple table UPDATE

» 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>

Page 6: Day 9. SELECT INSERT UPDATE DELETE » The standard UPDATE statement. UPDATE table SET field1=val1, field2=val2 WHERE condition » Multiple table UPDATE

» 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.’;}

}?>

Page 7: Day 9. SELECT INSERT UPDATE DELETE » The standard UPDATE statement. UPDATE table SET field1=val1, field2=val2 WHERE condition » Multiple table UPDATE
Page 8: Day 9. SELECT INSERT UPDATE DELETE » The standard UPDATE statement. UPDATE table SET field1=val1, field2=val2 WHERE condition » Multiple table UPDATE

» Single table DELETE syntax.DELETE FROM table WHERE condition

» Multiple table DELETE syntax.DELETE table1.field, table2.*

FROM table1, table2

WHERE condition

Page 9: Day 9. SELECT INSERT UPDATE DELETE » The standard UPDATE statement. UPDATE table SET field1=val1, field2=val2 WHERE condition » Multiple table UPDATE

» The limit command allows you to limit the amount of records effected to the number after the command.

SELECT ... FROM ... WHERE ... LIMIT 10

Page 10: Day 9. SELECT INSERT UPDATE DELETE » The standard UPDATE statement. UPDATE table SET field1=val1, field2=val2 WHERE condition » Multiple table UPDATE

» 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.


Recommended