Connecting to a Database with PHP

page 1: Introduction
page 2: Connecting to the Database
page 3: Displaying Tables
page 4: Inserting New Records
page 5: Updating Records
page 6: Deleting Records

Updating Records

Being that we're humans, and not perfect like these beautiful pets, we'll make mistakes while adding our records. Fortunately, there is an SQL statement to cover that, UPDATE, which tells the server which table to update. Then we add SET to indicate which field needs updating, and what value we're inserting instead. We also need to give the ID number of the record we want to alter, and we do that by using WHERE. (For now, look in your database in phpMyAdmin to identify the ID of the record you would like to change. In later tutorials, when we set up interactive forms, we'll talk about passing URL parameters.)

<?php
$connectID = mysql_connect("127.0.0.1", "root", "dancer");
mysql_select_db("animal_rescue", $connectID);
$result = mysql_query("UPDATE adoption_list
SET name='Allegra' WHERE ID=7", $connectID);
if ($result == TRUE)
{
print "The record was updated successfully.<p>";
}
else
{
print "The record could not be updated.<p>";
}
$resultID = mysql_query("SELECT species, breed, name, age,
personality FROM adoption_list", $connectID);
print "<table border=1><tr><th>Species</th><th>Breed</th>
<th>Name</th><th>Age</th><th>Personality</th></tr>";
while ($row=mysql_fetch_row($resultID))
{
print "<tr>";
foreach ($row as $field)
{
print "<td>$field</td>";
}
print "</tr>";
}
print "</table>";
mysql_close($connectID);
?>

Finally, we will need to be able to delete records once pets get adopted, or otherwise leave the rescue center. Let's explore the delete statement-->>

Back to Unix Tips Page-->