Tuesday, June 10, 2014

Phonebook Application in PHP

Lets create a simple phone book application in php which covers all the basic database operations like insert,retrieve,update,delete. Our app takes 4 fields from user: Contact name,number,email,city.Our main aim here is to see all the 4 basic operations in work. Thats the reason why iam building this application. The application which include these four can also be called as CRUD application(C-Create, R-Retrieve,U-Update,D-Delete).
our app consists of following files

  • index.php
  • insert.php
  • retrieve.php
  • update.php
  • delete.php


index.php

<!DOCTYPE html>

<html>
    <head>
        <meta charset="UTF-8">
        <title>PhoneBook</title>
    </head>
    <body style="font-size: 15pt">
        <h1 align='center'>PhoneBook Application</h1>
        <table align='center' border='1'>
            <form method="post" action="insert.php">
                <tr><td>Name</td><td><input type="text" name="t1"/></td></tr>
                <tr><td>Number</td><td><input type="text" name="t2"/></td></tr>
                <tr><td>Email</td><td><input type="email" name="t3"/></td></tr>
                <tr><td>City</td><td><input type="text" name="t4"/></td></tr>
                <tr><td colspan="2"><input type="submit" value="Save"/>
                <input type="submit" value="View" formaction="retrieve.php"/>
                <input type="submit" value="Update" formaction="update.php"/>  
                <input type="submit" value="Delete" formaction="delete.php"/></td></tr>
            </form>              
        </table>
        <label>Note:</label>
        <ul>
            <li>
                Insert all fields to save contact.
            </li>
            <li>
                Click view to see saved contacts.
            </li>
            <li>
                Insert number field to delete contact.
            </li>
            <li>
                Insert name,number,email fields to update contact number.
            </li>
        </ul>
    </body>
</html>
insert.php
<?php

$name = $_POST["t1"];
$number = $_POST["t2"];
$email = $_POST["t3"];
$city = $_POST["t4"];

$server = "localhost";
$user = "root";
$password = "mysql";
$database = "phone";

$con = mysql_connect($server, $user, $password);
mysql_select_db($database, $con);
$query = "insert into contacts values('$name','$number','$email','$city')";
mysql_query($query);
$rows_affected = mysql_affected_rows();
if ($rows_affected > 0) {
    echo "<h1 align='center'>Contact saved!</h1>"
    . "</br><div align='center'><a href='index.php'>[back]</a></div>";
} else if ($rows_affected == 0) {
    echo "<h1 align='center'>Details provided are invalid</h1>"
    . "</br><div align='center'><a href='index.php'>[back]</a></div>";
} else {
    echo "<h1 align='center'>Error--->" . mysql_error() . "</h1>"
    . "</br><div align='center'><a href='index.php'>[back]</a></div>";
}
mysql_close();
?>
retrieve.php
<?php

error_reporting(0);
$server = "localhost";
$user = "root";
$password = "mysql";
$database = "phone";
$con = mysql_connect($server, $user, $password);
mysql_select_db($database, $con);
$query = "select * from contacts";
$result = mysql_query($query);
echo "<table align='center' border='1'><thead><tr><td>Name</td><td>Number</td><td>Email</td><td>City</td></tr></thead>";
echo "<tbody>";
while ($row = mysql_fetch_array($result)) {
    echo "<tr><td>" . $row["nm"] . "</td>";
    echo "<td>" . $row["num"] . "</td>";
    echo "<td>" . $row["em"] . "</td>";
    echo "<td>" . $row["ci"] . "</td></tr>";
}
echo "</tbody></table>"
 . "</br><div align='center'><a href='index.php'>[back]</a></div>";
mysql_close();
?>
update.php
<?php

$name = $_POST["t1"];
$num = $_POST["t2"];
$email = $_POST["t3"];

$server = "localhost";
$user = "root";
$password = "mysql";
$database = "phone";

$con = mysql_connect($server, $user, $password);
mysql_select_db($database, $con);
$query = "update contacts set num='$num' where nm='$name' and em='$email'";
mysql_query($query);
$rows_affected = mysql_affected_rows();
if ($rows_affected > 0) {
    echo "<h1 align='center'>Contact updated!</h1>"
    . "</br><div align='center'><a href='index.php'>[back]</a></div>";
} else if ($rows_affected == 0) {
    echo "<h1 align='center'>Details provided are invalid</h1>"
    . "</br><div align='center'><a href='index.php'>[back]</a></div>";
} else {
    echo "<h1 align='center'>Error--->" . mysql_error() . "</h1>"
    . "</br><div align='center'><a href='index.php'>[back]</a></div>";
}
mysql_close();
?>
delete.php
<?php

$num = $_POST["t2"];

$server = "localhost";
$user = "root";
$password = "mysql";
$database = "phone";

$con = mysql_connect($server, $user, $password);
mysql_select_db($database, $con);
$query = "delete from contacts where num='$num'";
mysql_query($query);
$rows_affected = mysql_affected_rows();
if ($rows_affected > 0) {
    echo "<h1 align='center'>Contact deleted!</h1>"
    . "</br><div align='center'><a href='index.php'>[back]</a></div>";
} else if ($rows_affected == 0) {
    echo "<h1 align='center'>Details provided are invalid</h1>"
    . "</br><div align='center'><a href='index.php'>[back]</a></div>";
} else {
    echo "<h1 align='center'>Error--->" . mysql_error() . "</h1>"
    . "</br><div align='center'><a href='index.php'>[back]</a></div>";
}
mysql_close();
?>
SCREENSHOTS

Insert


Retrieve


















Update


















Delete




No comments :

Post a Comment

Designed By Seo Blogger Templates