Day 17: Database Handling With Oops

In database there are 4 basic functions that we have to perform i.e. Display, Save, Update and Delete. Earlier we have done this without OOPS, now we will do this with OOPS.

There are 3 files used in this assignment:

  1. db1.php : “emp” class and member functions are defined in this file.
  2. display.php : used to display the records from tbemp table. “display” function of “emp” class is used in this file.
  3. save.php : used to save the records in tbemp table. “save” function of “emp” class is used in this file.

Program 49: Class to Display and Save the Records (db1.php)

<?php
class emp {
var $con = “”;
var $name;
var $salary;
var $city;
function __construct() {
$this->con = mysql_connect(‘localhost’,’root’,”);
mysql_select_db(‘mydb’,$this->con);
}
function display() {
$query = “select * from tbemp”;
$data = mysql_query($query);
return $data;
}
function save() {
$query = “insert into tbemp (name,salary,city) values (‘$this->name’,’$this->salary’,’$this->city’)”;
if(mysql_query($query)) {
return true;
} else {
return false;
}
}
}
?>

Program 50: Code to display the record (display.php)

<?php
require_once “db1.php”;
$empObj = new emp();
$data = $empObj->display();
?>
<html>
<head>
<title>Display Records</title>
</head>
<table cellpadding=”10″ border=”1″>
<th>Name</th><th>Salary</th><th>City</th>
<?php while($rec = mysql_fetch_object($data)) { ?>
<tr>
<td><?php echo $rec->name; ?></td>
<td><?php echo $rec->salary; ?></td>
<td><?php echo $rec->city; ?></td>
</tr>
<?php } ?>
</table>
<br/>
<a href=”save.php”>Add Record</a>
</html>

Program 51: Code to save the record (save.php)

<?php
require_once “db1.php”;
$empObj = new emp();
$msg = “”;
if(isset($_REQUEST[‘btnSubmit’])) {
if(trim($_REQUEST[‘txtName’])!=””) {
$empObj->name = $_REQUEST[‘txtName’];
} else {
$msg = “*Name Required”;
}
if(trim($_REQUEST[‘txtSalary’])!=””) {
$empObj->salary = $_REQUEST[‘txtSalary’];
} else {
$msg.= “<br/>*Salary Required”;
}
if(trim($_REQUEST[‘txtCity’])!=””) {
$empObj->city = $_REQUEST[‘txtCity’];
} else {
$msg.= “<br/>*City Required”;
}
if($msg == “”) {
$result = $empObj->save();
if($result) {
$msg = “Record Saved”;
} else {
$msg = “Unable To Save”;
}
}
}
?>
<html>
<head>
<title>Save Records</title>
</head>
<div id=”msg” style=”color:red”>
<?php echo $msg; ?>
</div>
<form method=”POST”>
<table cellspacing=”10″>
<tr>
<th>Name* : </th> <td> <input type=”text” name=”txtName” id=”txtName” value=”<?php echo $_POST[‘txtName’];?>”/> </td>
</tr>
<tr>
<th>Salary* : </th> <td> <input type=”text” name=”txtSalary” id=”txtSalary” value=”<?php echo $_POST[‘txtSalary’];?>”/> </td>
</tr>
<tr>
<th>City* : </th> <td> <input type=”text” name=”txtCity” id=”txtCity” value=”<?php echo $_POST[‘txtCity’];?>”/> </td>
</tr>
<tr>
<th><input type=”reset” value=”Reset”/></th>
<td><input type=”submit” value=”Save” name=”btnSubmit” id=”btnSubmit”/></td>
</tr>
</table>
</form>
<br/>
<a href=”display.php”>Display Records</a>
</html>

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.