Monday, January 18, 2010

Telephone Directory System(PHP/My SQl)

This blog is about a basic Telephone Management System designed by PHP and MySQl with Wamp Server .This Tutorial I think will help the Be gainers.The most interesting feature of this software is that everything ie. insertion,Deletion,Updation and searching everything is carried out in a single page.I am just providing the code.Copy it and paste it in www folder of your WAMP server and then create a database/Table in the MY Sql Section to find out the full functionability of the software.

First the connection page-
I have named it as "connection.php"

$link=mysql_connect('127.0.0.1','root','');
if(!$link)
{
die('Error Connecting to Server. '.mysql_error());
}
$select_db=mysql_select_db('telephone',$link);
if(!$select_db)
{
die('Error Connecting to Database '.mysql_error());
}


// clear the cache
header('Cache-Control: no-cache, must-revalidate');
header('Pragma: no-cache');
$ExpStr = "Expires: " . gmdate("D, d M Y H:i: s ", time() ) . " GMT";
header($ExpStr);
header('Cache-Control: public, no-cache');
?>


Now the main Page.
I have named it as "resultname.php"

include("connection.php");
$searchname = $_REQUEST['searchname'];
if($_REQUEST["ADD"])
{
$id = $_REQUEST['id'];
$name = $_REQUEST['name'];
$mobile1 = $_REQUEST['mobile1'];
$mobile2 = $_REQUEST['mobile2'];
$mobile3 = $_REQUEST['mobile3'];
$lan1 = $_REQUEST['lan1'];
$lan2 = $_REQUEST['lan2'];
$address = $_REQUEST['address'];
$email = $_REQUEST['email'];

mysql_query("insert into record
(name,mobile1,mobile2,mobile3,lan1,lan2,address,email)
values
('$name','$mobile1','$mobile2','$mobile3','$lan1','$lan2','$address','$email')");
if(mysql_affected_rows() > 0)
{
$msg = "Records Inserted Successfully";
}
else
{
$msg = "Error! record not inserted";
}

}
if($_REQUEST['mode']=="add")
{?>









































Insert New Record
Name
Mobile1
Mobile2
Mobile3
LAN 1
LAN 2
Address
Email











}
if($_REQUEST["Update"])
{
$id = $_REQUEST['id'];
$name = $_REQUEST['name'];
$mobile1 = $_REQUEST['mobile1'];
$mobile2 = $_REQUEST['mobile2'];
$mobile3 = $_REQUEST['mobile3'];
$lan1 = $_REQUEST['lan1'];
$lan2 = $_REQUEST['lan2'];
$address = $_REQUEST['address'];
$email = $_REQUEST['email'];

$sql = "UPDATE record SET
name = '$name' , mobile1 = '$mobile1' , mobile2 = '$mobile2' , mobile3 = '$mobile3' , lan1 = '$lan1' , lan2 = '$lan2' , address = '$address' , email = '$email' WHERE id='$id'";
$result = mysql_query($sql);
if(mysql_affected_rows()>0)
{
$msg = "Updation Completed Successfully";
}
else
{
$msg = "Updation failed";
}

}
if($_REQUEST['mode']=="edit")
{

$result = mysql_query("select * from record where id=".$_GET['id']);
$myrow = mysql_fetch_array($result);?>


">








































Update Record
Name ">
Mobile1 ">
Mobile2 ">
Mobile3 ">
LAN 1 ">
LAN 2 ">
Address
Email ">


" onClick="return confirm('Are You Sure ?)">







}
if($_REQUEST['mode']=="delete")
{
mysql_query("delete from record where id=".$_GET['id']);
$msg = "Record Deleted Successfully.";
}

?>





Edit Content










Welcome To the Telephone Directory Management System













Searching By Name

Enter Name or PH. No to Search



























Record
ADD





";}
else
{echo $msg."
";} ?>














$total = mysql_query("SELECT count(*) as cnt FROM record where name like '%$searchname%' or mobile1 like '%$searchname%' or mobile2 like '%$searchname%' or mobile3 like '%$searchname%' or lan1 like '%$searchname%' or lan2 like '%$searchname%'");
$a = mysql_fetch_object($total);
$total_items = $a->cnt;

//echo "Total Number of records in Database: ".$total_items;
$limit= 10;
$page= $_GET['page'];
echo '' . $set_limit . '';
if((!$limit) || (is_numeric($limit) == false)
|| ($limit <> 50))
{
$limit = 10; //default
}

//Set default if: $page is empty, non numerical,
//less than zero, greater than total available

if((!$page) || (is_numeric($page) == false)
|| ($page <> $total_items))
{
$page = 1; //default
}
$total_pages = ceil($total_items / $limit);
$set_limit = ($page * $limit) - $limit;


$sql="SELECT * FROM record where name like '%$searchname%' or mobile1 like '%$searchname%' or mobile2 like '%$searchname%' or mobile3 like '%$searchname%' or lan1 like '%$searchname%' or lan2 like '%$searchname%' order by name LIMIT $set_limit, $limit";
$sql_query=mysql_query($sql);

while($row=mysql_fetch_array($sql_query))
{
?>













}

$prev_page = $page - 1;

if($prev_page >= 1) {
echo("
Prev ");
// echo $prev_page;
}
for($a = 1; $a <= $total_pages; $a++)
{
if($a == $page) {
echo(" $a | "); //no link
}
else {
echo("
$a
| ");
}
}

//Next page: **EDIT THIS LINK PATH**

$next_page = $page + 1;
if($next_page <= $total_pages) {

echo("
Next");
// echo $next_page;
}



?>
Name Mobile1 Mobile2 Mobile3 Lan1 Lan2 Email Address

&name="
onClick="return confirm('Are You Sure you Want to edit: ?')">EDIT
" onClick="return confirm('Are You Sure you Want to delete: ?')">DELETE






Now the MYSQl Portion

first create a database named-"telephone"

Now the table structure.I have named the table as "record"

now copy paste this command

CREATE TABLE IF NOT EXISTS `record` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(50) NOT NULL,
`mobile1` varchar(15) NOT NULL,
`mobile2` varchar(15) NOT NULL,
`mobile3` varchar(15) NOT NULL,
`lan1` varchar(15) NOT NULL,
`lan2` varchar(15) NOT NULL,
`address` varchar(100) NOT NULL,
`email` varchar(50) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `name` (`name`)
)

I think that will be all.If you find any problem feel free to contact me @
dasgupta.rony@gmail.com

This is basically for the begainers and doesnot cointain any complexity.

Hope to meet you all very soon.