Professional Documents
Culture Documents
Iwp Exp10 18bce0172
Iwp Exp10 18bce0172
Iwp Exp10 18bce0172
10. Design a web application using PHP to perform all the Database operations.
i. Create an address book web application that allows the user to store and
retrieve several mailing lists from MySQL database. The address book contains
first name, designation, address1, address2, City, State, emailid. User can add,
update and delete all address information’s in the database. Implement search
operation with emailid, when emailed is given it return an HTML page with all
the complete address information.
CODE:
view_contacts.php
<?php
require_once './config.php';
include './header.php';
try {
$sql = "SELECT * FROM tbl_contacts WHERE 1 AND contact_id = :cid";
$stmt = $DB->prepare($sql);
$stmt->bindValue(":cid", intval($_GET["cid"]));
$stmt->execute();
$results = $stmt->fetchAll();
} catch (Exception $ex) {
echo $ex->getMessage();
}
?>
<div class="row">
<ul class="breadcrumb">
<li><a href="index.php">Home</a></li>
<li class="active">View Contacts</li>
</ul>
</div>
<div class="row">
<div class="panel panel-primary">
<div class="panel-heading">
<h3 class="panel-title">View Contact</h3>
</div>
<div class="panel-body">
<form class="form-
horizontal" name="contact_form" id="contact_form" enctype="multipart/form-
data" method="post" action="process_form.php">
<fieldset>
<div class="form-group">
<label class="col-lg-4 control-
label" for="first_name"><span class="required">*</span>First Name:</label>
<div class="col-lg-5">
<input type="text" readonly="" placeholder="First Name" value="<?php echo $re
sults[0]["first_name"] ?>" id="first_name" class="form-
control" name="first_name"><span id="first_name_err" class="error"></span>
</div>
</div>
<div class="form-group">
<label class="col-lg-4 control-label" for="Last_name">Designation:</label>
<div class="col-lg-5">
<input type="text" readonly="" value="<?php echo $results[0]["Last_name"] ?>"
placeholder="Designation" id="Last_name" class="form-control" name="Last_name">
</div>
</div>
<div class="form-group">
<label class="col-lg-4 control-
label" for="last_name"><span class="required">*</span>Designation:</label>
<div class="col-lg-5">
<input type="text" readonly="" value="<?php echo $results[0]["last_name"] ?>"
placeholder="Designation" id="last_name" class="form-
control" name="last_name"><span id="last_name_err" class="error"></span>
</div>
</div>
<div class="form-group">
<label class="col-lg-4 control-
label" for="profile_pic">Profile picture:</label>
<div class="col-lg-5">
<?php $pic = ($results[0]["profile_pic"] <> "" ) ? $results[0]["profile_pic"]
: "no_avatar.png" ?>
<a href="profile_pics/<?php echo $pic ?>" target="_blank"><img src="profile_p
ics/<?php echo $pic ?>" alt="" width="100" height="100" class="thumbnail" ></a>
</div>
</div>
<div class="form-group">
<label class="col-lg-4 control-
label" for="email_id"><span class="required">*</span>Email ID:</label>
<div class="col-lg-5">
<input type="text" readonly="" value="<?php echo $results[0]["email_address"]
?>" placeholder="Email ID" id="email_id" class="form-
control" name="email_id"><span id="email_id_err" class="error"></span>
</div>
</div>
<div class="form-group">
<label class="col-lg-4 control-
label" for="contact_no1"><span class="required">*</span>Contact No #1:</label>
<div class="col-lg-5">
<input type="text" readonly="" value="<?php echo $results[0]["contact_no1"] ?
>" placeholder="Contact Number" id="contact_no1" class="form-
control" name="contact_no1"><span id="contact_no1_err" class="error"></span>
</div>
</div>
<div class="form-group">
<label class="col-lg-4 control-label" for="contact_no2">Contact No #2:</label>
<div class="col-lg-5">
<input type="text" readonly="" value="<?php echo $results[0]["contact_no2"] ?
>" placeholder="Contact Number" id="contact_no2" class="form-
control" name="contact_no2"><span id="contact_no2_err" class="error"></span>
</div>
</div>
<div class="form-group">
<label class="col-lg-4 control-label" for="address">Address:</label>
<div class="col-lg-5">
<textarea id="address" readonly="" name="address" rows="3" class="form-
control"><?php echo $results[0]["address"] ?></textarea>
</div>
</div>
</fieldset>
</form>
</div>
</div>
</div>
<?php
include './footer.php';
?>
process_form.php
<?php
require './config.php';
$mode = $_REQUEST["mode"];
if ($mode == "add_new" ) {
$first_name = trim($_POST['first_name']);
$Last_name = trim($_POST['Last_name']);
$last_name = trim($_POST['last_name']);
$email_id = trim($_POST['email_id']);
$contact_no1 = trim($_POST['contact_no1']);
$contact_no2 = trim($_POST['contact_no2']);
$address = trim($_POST['address']);
$filename = "";
$error = FALSE;
if (is_uploaded_file($_FILES["profile_pic"]["tmp_name"])) {
$filename = time() . '_' . $_FILES["profile_pic"]["name"];
$filepath = 'profile_pics/' . $filename;
if (!move_uploaded_file($_FILES["profile_pic"]["tmp_name"], $filepath)) {
$error = TRUE;
}
}
if (!$error) {
$sql = "INSERT INTO `tbl_contacts` (`first_name`, `Last_name`, `last_name`, `address`, `c
ontact_no1`, `contact_no2`, `email_address`, `profile_pic`) VALUES "
. "( :fname, :mname, :lname, :address, :contact1, :contact2, :email, :pic)";
try {
$stmt = $DB->prepare($sql);
$stmt->bindValue(":fname", $first_name);
$stmt->bindValue(":mname", $Last_name);
$stmt->bindValue(":lname", $last_name);
$stmt->bindValue(":address", $address);
$stmt->bindValue(":contact1", $contact_no1);
$stmt->bindValue(":contact2", $contact_no2);
$stmt->bindValue(":email", $email_id);
$stmt->bindValue(":pic", $filename);
$stmt->execute();
$result = $stmt->rowCount();
if ($result > 0) {
$_SESSION["errorType"] = "success";
$_SESSION["errorMsg"] = "Contact added successfully.";
} else {
$_SESSION["errorType"] = "danger";
$_SESSION["errorMsg"] = "Failed to add contact.";
}
} catch (Exception $ex) {
$_SESSION["errorType"] = "danger";
$_SESSION["errorMsg"] = $ex->getMessage();
}
} else {
$_SESSION["errorType"] = "danger";
$_SESSION["errorMsg"] = "failed to upload image.";
}
header("location:index.php");
} elseif ( $mode == "update_old" ) {
$first_name = trim($_POST['first_name']);
$Last_name = trim($_POST['Last_name']);
$last_name = trim($_POST['last_name']);
$email_id = trim($_POST['email_id']);
$contact_no1 = trim($_POST['contact_no1']);
$contact_no2 = trim($_POST['contact_no2']);
$address = trim($_POST['address']);
$cid = trim($_POST['cid']);
$filename = "";
$error = FALSE;
if (is_uploaded_file($_FILES["profile_pic"]["tmp_name"])) {
$filename = time() . '_' . $_FILES["profile_pic"]["name"];
$filepath = 'profile_pics/' . $filename;
if (!move_uploaded_file($_FILES["profile_pic"]["tmp_name"], $filepath)) {
$error = TRUE;
}
} else {
$filename = $_POST['old_pic'];
}
if (!$error) {
$sql = "UPDATE `tbl_contacts` SET `first_name` = :fname, `Last_name` = :mname, `last_name
` = :lname, `address` = :address, `contact_no1` = :contact1, `contact_no2` = :contact2, `emai
l_address` = :email, `profile_pic` = :pic "
. "WHERE contact_id = :cid ";
try {
$stmt = $DB->prepare($sql);
$stmt->bindValue(":fname", $first_name);
$stmt->bindValue(":mname", $Last_name);
$stmt->bindValue(":lname", $last_name);
$stmt->bindValue(":address", $address);
$stmt->bindValue(":contact1", $contact_no1);
$stmt->bindValue(":contact2", $contact_no2);
$stmt->bindValue(":email", $email_id);
$stmt->bindValue(":pic", $filename);
$stmt->bindValue(":cid", $cid);
$stmt->execute();
$result = $stmt->rowCount();
if ($result > 0) {
$_SESSION["errorType"] = "success";
$_SESSION["errorMsg"] = "Contact updated successfully.";
} else {
$_SESSION["errorType"] = "info";
$_SESSION["errorMsg"] = "No changes made to contact.";
}
} catch (Exception $ex) {
$_SESSION["errorType"] = "danger";
$_SESSION["errorMsg"] = $ex->getMessage();
}
} else {
$_SESSION["errorType"] = "danger";
$_SESSION["errorMsg"] = "Failed to upload image.";
}
header("location:index.php?pagenum=".$_POST['pagenum']);
} elseif ( $mode == "delete" ) {
$cid = intval($_GET['cid']);
$sql = "DELETE FROM `tbl_contacts` WHERE contact_id = :cid";
try {
$stmt = $DB->prepare($sql);
$stmt->bindValue(":cid", $cid);
$stmt->execute();
$res = $stmt->rowCount();
if ($res > 0) {
$_SESSION["errorType"] = "success";
$_SESSION["errorMsg"] = "Contact deleted successfully.";
} else {
$_SESSION["errorType"] = "info";
$_SESSION["errorMsg"] = "Failed to delete contact.";
}
index.php
<?php
require_once './config.php';
include './header.php';
if (!(isset($_GET['pagenum']))) {
$pagenum = 1;
} else {
$pagenum = $_GET['pagenum'];
}
$page_limit = ($_GET["show"] <> "" && is_numeric($_GET["show"]) ) ? $_GET["show"] : 8;
try {
$keyword = trim($_GET["keyword"]);
if ($keyword <> "" ) {
$sql = "SELECT * FROM tbl_contacts WHERE 1 AND "
. " (email_address LIKE :keyword) ORDER BY first_name ";
$stmt = $DB->prepare($sql);
$stmt->bindValue(":keyword", $keyword."%");
} else {
$sql = "SELECT * FROM tbl_contacts WHERE 1 ORDER BY first_name ";
$stmt = $DB->prepare($sql);
}
$stmt->execute();
$total_count = count($stmt->fetchAll());
if ($pagenum < 1) {
$pagenum = 1;
} elseif ($pagenum > $last) {
$pagenum = $last;
}
$lower_limit = ($pagenum - 1) * $page_limit;
$lower_limit = ($lower_limit < 0) ? 0 : $lower_limit;
$sql2 = $sql . " limit " . ($lower_limit) . " , " . ($page_limit) . " ";
$stmt = $DB->prepare($sql2);
$stmt->execute();
$results = $stmt->fetchAll();
} catch (Exception $ex) {
echo $ex->getMessage();
}
?>
<div class="row">
<?php if ($ERROR_MSG <> "") { ?>
<div class="alert alert-dismissable alert-<?php echo $ERROR_TYPE ?>">
<button data-dismiss="alert" class="close" type="button">×</button>
<p><?php echo $ERROR_MSG; ?></p>
</div>
<?php } ?>
Header.php
<!DOCTYPE html>
<html lang="en">
<head>
<title><?php echo PROJECT_NAME; ?></title>
<link href="bootstrap/css/bootstrap.min.css" rel="stylesheet">
<link href="style.css" rel="stylesheet">
<script src="bootstrap/js/jquery-1.9.0.min.js"></script>
</head>
<body style="background-color:powderblue;">
<div class="container mainbody">
<div class="page-header">
<h1 style="text-align:center;color:red">IWP EXPERIMENT - 10</h1>
<h1 style="text-align:center;color:red">18BCE0172 - AYUSH SHARMA</h1>
<h1 style="text-align:center;color:red">ADDRESS BOOK USING PHP AND MYSQL</h1>
</div>
<div class="clearfix"></div>
</body>
Contacts.php
<?php
require_once './config.php';
include './header.php';
try {
$sql = "SELECT * FROM tbl_contacts WHERE 1 AND contact_id = :cid";
$stmt = $DB->prepare($sql);
$stmt->bindValue(":cid", intval($_GET["cid"]));
$stmt->execute();
$results = $stmt->fetchAll();
} catch (Exception $ex) {
echo $ex->getMessage();
}
?>
<div class="row">
<ul class="breadcrumb">
<li><a href="index.php">Home</a></li>
<li class="active"><?php echo ($_GET["m"] == "update") ? "Edit" : "Add"; ?> Contacts</l
i>
</ul>
</div>
<div class="row">
<div class="panel panel-primary">
<div class="panel-heading">
<h3 class="panel-
title"><?php echo ($_GET["m"] == "update") ? "Edit" : "Add"; ?> New Contact</h3>
</div>
<div class="panel-body">
<form class="form-
horizontal" name="contact_form" id="contact_form" enctype="multipart/form-
data" method="post" action="process_form.php">
<input type="hidden" name="mode" value="<?php echo ($_GET["m"] == "update") ? "upda
te_old" : "add_new"; ?>" >
<input type="hidden" name="old_pic" value="<?php echo $results[0]["profile_pic"] ?>
" >
<input type="hidden" name="cid" value="<?php echo intval($results[0]["contact_id"])
; ?>" >
<input type="hidden" name="pagenum" value="<?php echo $_GET["pagenum"]; ?>" >
<fieldset>
<div class="form-group">
<label class="col-lg-4 control-
label" for="first_name"><span class="required">*</span>First Name:</label>
<div class="col-lg-5">
<input type="text" value="<?php echo $results[0]["first_name"] ?>" placeholde
r="First Name" id="first_name" class="form-
control" name="first_name"><span id="first_name_err" class="error"></span>
</div>
</div>
<div class="form-group">
<label class="col-lg-4 control-label" for="Last_name">Designation:</label>
<div class="col-lg-5">
<input type="text" value="<?php echo $results[0]["Last_name"] ?>" placeholder
="Designation" id="Last_name" class="form-control" name="Last_name">
</div>
</div>
<div class="form-group">
<label class="col-lg-4 control-
label" for="last_name"><span class="required">*</span>Designation:</label>
<div class="col-lg-5">
<input type="text" value="<?php echo $results[0]["last_name"] ?>" placeholder
="Designation" id="last_name" class="form-
control" name="last_name"><span id="last_name_err" class="error"></span>
</div>
</div>
<div class="form-group">
<label class="col-lg-4 control-
label" for="email_id"><span class="required">*</span>Email ID:</label>
<div class="col-lg-5">
<input type="text" value="<?php echo $results[0]["email_address"] ?>" placeho
lder="Email ID" id="email_id" class="form-
control" name="email_id"><span id="email_id_err" class="error"></span>
</div>
</div>
<div class="form-group">
<label class="col-lg-4 control-
label" for="contact_no1"><span class="required">*</span>Contact No #1:</label>
<div class="col-lg-5">
<input type="text" value="<?php echo $results[0]["contact_no1"] ?>" placehold
er="Contact Number" id="contact_no1" class="form-
control" name="contact_no1"><span id="contact_no1_err" class="error"></span>
<span class="help-block">Maximum of 10 digits only and only numbers.</span>
</div>
</div>
<div class="form-group">
<label class="col-lg-4 control-label" for="contact_no2">Contact No #2:</label>
<div class="col-lg-5">
<input type="text" value="<?php echo $results[0]["contact_no2"] ?>" placehold
er="Contact Number" id="contact_no2" class="form-
control" name="contact_no2"><span id="contact_no2_err" class="error"></span>
<span class="help-block">Maximum of 10 digits only and only numbers.</span>
</div>
</div>
<div class="form-group">
<label class="col-lg-4 control-
label" for="profile_pic">Profile picture:</label>
<div class="col-lg-5">
<input type="file" id="profile_pic" class="form-
control file" name="profile_pic"><span id="profile_pic_err" class="error"></span>
<span class="help-block">Must me jpg, jpeg, png, gif, bmp image only.</span>
</div>
</div>
<div class="form-group">
<label class="col-lg-4 control-label" for="address">Address:</label>
<div class="col-lg-5">
<textarea id="address" name="address" rows="3" class="form-
control"><?php echo $results[0]["address"] ?></textarea>
</div>
</div>
<div class="form-group">
<div class="col-lg-5 col-lg-offset-4">
<button class="btn btn-primary" type="submit">Submit</button>
</div>
</div>
</fieldset>
</form>
</div>
</div>
</div>
<script type="text/javascript">
$(document).ready(function() {
});
function validateForm() {
var errCnt = 0;
var first_name = $.trim( $("#first_name").val());
var last_name = $.trim( $("#last_name").val());
var email_id = $.trim( $("#email_id").val());
var contact_no1 = $.trim( $("#contact_no1").val());
var contact_no2 = $.trim( $("#contact_no2").val());
// validate name
if (first_name == "" ) {
$("#first_name_err").html("Enter your first name.");
$('#first_name_err').fadeIn("fast");
errCnt++;
} else if (first_name.length <= 2 ) {
$("#first_name_err").html("Enter atleast 3 letter.");
$('#first_name_err').fadeIn("fast");
errCnt++;
}
if (last_name == "" ) {
$("#last_name_err").html("Enter your Designation.");
$('#last_name_err').fadeIn("fast");
errCnt++;
} else if (last_name.length <= 2 ) {
$("#last_name_err").html("Enter atleast 3 letter.");
$('#last_name_err').fadeIn("fast");
errCnt++;
}
if (!isValidEmail(email_id)) {
$("#email_id_err").html("Enter valid email.");
$('#email_id_err').fadeIn("fast");
errCnt++;
}
if (contact_no1 == "" ) {
$("#contact_no1_err").html("Enter first contact number.");
$('#contact_no1_err').fadeIn("fast");
errCnt++;
} else if (contact_no1.length <= 9 || contact_no1.length > 10 ) {
$("#contact_no1_err").html("Enter 10 digits only.");
$('#contact_no1_err').fadeIn("fast");
errCnt++;
} else if ( !$.isNumeric(contact_no1) ) {
$("#contact_no1_err").html("Must be digits only.");
$('#contact_no1_err').fadeIn("fast");
errCnt++;
}
if (contact_no2.length > 0) {
if (contact_no2.length <= 9 || contact_no2.length > 10 ) {
$("#contact_no2_err").html("Enter 10 digits only.");
$('#contact_no2_err').fadeIn("fast");
errCnt++;
} else if ( !$.isNumeric(contact_no2) ) {
$("#contact_no2_err").html("Must be digits only.");
$('#contact_no2_err').fadeIn("fast");
errCnt++;
}
}
if (profile_pic.length > 0) {
var exts = ['jpg','jpeg','png','gif', 'bmp'];
var get_ext = profile_pic.split('.');
get_ext = get_ext.reverse();
}
if(errCnt > 0) return false; else return true;
}
function isValidEmail(email) {
var regex = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
return regex.test(email);
}
</script>
<?php
include './footer.php';
?>
Address_book.sql
create database address_book;
use address_book;
CREATE TABLE IF NOT EXISTS `tbl_contacts` (
`contact_id` int(11) NOT NULL AUTO_INCREMENT,
`first_name` varchar(255) NOT NULL,
`Last_name` varchar(255) NOT NULL,
`last_name` varchar(255) NOT NULL,
`address` text,
`contact_no1` varchar(255) NOT NULL,
`contact_no2` varchar(255) DEFAULT NULL,
`email_address` varchar(255) NOT NULL,
`profile_pic` varchar(255) DEFAULT NULL,
PRIMARY KEY (`contact_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=41 ;
OUTPUT
HOME PAGE
VIEW CONTACT
EDIT CONTACT DETAILS (CHANG CONTACT NO TO 1111111111):-
SEARCH BY EMAIL
BEFORE SEARCH LIST
AFTER SEARCHING EMAIL - jayakumar.k@vit.ac.in SEARCH RESULT: