Wednesday, May 13, 2009

Exercise 08

1. Start with a simple table in the database:

mysql> SELECT * FROM employees;

A database named my database is created in my Apache Server for testing this Exercise and the corresponding table employees with the following structures are created.

The above is copied from the websites phpMyAdmin installed in my server for management of database.


2. Create a web page with the following PHP:

The program codes are modified to suit the version of PHP, MySQL and Apache Server installed in my desktop:

$host=”localhost”;
$user=”root”;
$password=”″;
$db=”mydatabase”;
$cxn = mysqli_connect($host,$user,$password,$db) or die (”Could not connect to Server”);
$sql=”SELECT * FROM employees”;
$result = mysqli_query($cxn,$sql)
or die(”Couldn’t execute select query.”);
$row = mysqli_fetch_assoc($result);
echo “First Name: “, $row["First"], “
”;
echo “Last Name: “, $row["Last"], “
”;
echo “Address: “, $row["Address"], “
”;
echo “Position: “, $row["Position"], “
”;
?>

3. This is how we can add a record and is part of a file to create called add_record.html:


The output form of the browser is as follows:


4. The corresponding PHP file is add_record.php used with the POST method:


The PHP file is to add new employee record, modified as follows:

$host=”localhost”;
$user=”root”;
$password=”″;
$db=”mydatabase”;
$cxn = mysqli_connect($host,$user,$password,$db) or die (”Could not connect to Server”);
$first=$_POST['first'];
$last=$_POST['lsat'];
$address=$_POST['address'];
$position=$_POST['position'];
$sql=”INSERT INTO employees (First, Last, Address, Position)
VALUES (’$first’,'$last’,'$address’,'$position’)”;
$result = mysqli_query($cxn,$sql)
or die(”Couldn’t execute select query.”);
if ($result == 1)
{ echo “Thank you! Your information has been entered.”;}
else {echo “Sorry, there’s a problem”;}
?>
The output of the PHP programme after successful adding employee’s record is as follows:
Thank you! Your information has been entered.
mysql_select_db(“mydatabase",$db);
$result = mysql_query("INERT INTO employees (first,last,address,position) VALUES ('$first','$last','$address','$position')");
if ($result == 1) { echo "Thank you! Your information has been entered.";
} else {
echo "Sorry, there's a problem";
}
?>


5. The last code example shows how to get multiple records:
The html file with the following codes is created for initiating actions to list all the employee’s records:


The form would be displayed as follows:

The corresponding PHP file “List_employee.php” with the following program codes is created:

$host=”localhost”;
$user=”root”;
$password=”″;
$db=”mydatabase”;

No comments:

Post a Comment