Loading
Hosting Unlimited Indonesia

All Together Now

All Together Now

We'll finish up this lesson by putting everything into a single page that can add, edit, and remove entries
from the database. It's an extension of what we've covered so far and makes for a good review. Let's take
a look.

<html>
<body>
<?php
$db = mysql_connect("localhost", "root");
mysql_select_db("mydb",$db);
if ($submit) {
// here if no ID then adding else we're editing
if ($id) {
$sql = "UPDATE employees SET first='$first',last='$last',address='$address',position='$position'
WHERE id=$id";
} else {
$sql = "INSERT INTO employees (first,last,address,position) VALUES
('$first','$last','$address','$position')";
}
// run SQL against the DB
$result = mysql_query($sql);
echo "Record updated/edited!<p>";
} elseif ($delete) {
// delete a record
$sql = "DELETE FROM employees WHERE id=$id";
$result = mysql_query($sql);
echo "$sql Record deleted!<p>";
} else {
// this part happens if we don't press submit
if (!$id) {
// print the list if there is not editing
$result = mysql_query("SELECT * FROM employees",$db);
while ($myrow = mysql_fetch_array($result)) {
printf("<a href=\"%s?id=%s\">%s %s</a> \n", $PHP_SELF, $myrow["id"], $myrow["first"],
$myrow["last"]);
printf("<a href=\"%s?id=%s&delete=yes\">(DELETE)</a><br>", $PHP_SELF,
$myrow["id"]);
}
}
?>
<P>
<a href="<?php echo $PHP_SELF?>">ADD A RECORD</a>
<P>
<form method="post" action="<?php echo $PHP_SELF?>">
<?php
if ($id) {
// editing so select a record
$sql = "SELECT * FROM employees WHERE id=$id";
$result = mysql_query($sql);
$myrow = mysql_fetch_array($result);
$id = $myrow["id"];
$first = $myrow["first"];
$last = $myrow["last"];
$address = $myrow["address"];
$position = $myrow["position"];
// print the id for editing
?>
<input type=hidden name="id" value="<?php echo $id ?>">
<?php

}
?>
First name:<input type="Text" name="first" value="<?php echo $first ?>"><br>
Last name:<input type="Text" name="last" value="<?php echo $last ?>"><br>
Address:<input type="Text" name="address" value="<?php echo $address ?>"><br>
Position:<input type="Text" name="position" value="<?php echo $position ?>"><br>
<input type="Submit" name="submit" value="Enter information">
</form>
<?php
}
?>
</body>
</html>

This looks complex, but it really isn't. The script is broken up into three parts. The first if() statement
checks to see whether the Submit button has been pressed, and if it has, it checks to see whether the
variable $id exists. If doesn't, then we're adding a record. Otherwise, we're editing a record.

Next we check to see whether the variable $delete exists. If it does, we delete a record. Note that with
the first if() statement we checked for a variable that came through as a POST, and in this one, the
variable would be part of a GET.

Finally, we take the default action that displays the list of employees and the form. Again we check for the
existence of the $id variable. If it exists, we query the database to display the relevant record. Otherwise,
we display a blank form.

We've now put all we've learned into one script. We used while() loops and if() statements, and we ran the
gamut of the basic SQL statements - SELECT, INSERT, UPDATE, and DELETE. Lastly, we've looked at how we can pass information from one page to another using URLs and form input.

In Lesson 3 we'll look at how to make the page more intelligent.

0 Comments:

Post a Comment

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Top WordPress Themes