What is CRUD?
C is Create
R is Remove
U is Update
D is Display
In this video I have an example want to show you with
my web hosting using GoDaddy
I have domain name want2learn.info and database name is ´test_customer_db´ and table name is ´tbl_customers´.
I have 5 fields in this table
- customerid
- firstname
- lastname
- address
- contact
And then write php codes
Create file config.php to connect with database
<?php
$mysql_hostname = "localhost";
$mysql_user = "test2015";
$mysql_password = "test2015";
$mysql_database = "test_customer_db";
$bd = mysql_connect($mysql_hostname, $mysql_user, $mysql_password) or die("Opps some thing went wrong");
mysql_select_db($mysql_database, $bd) or die("Opps some thing went wrong");
?>
Create file index.php to display customers
<?php
include('includes/config.php');
$sql = "SELECT * FROM tbl_customers ORDER BY firstname";
$rsd = mysql_query($sql);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Display Customers</title>
</head>
<body>
<a href="new_customer.php" title="Add New Customer">Add New Customer</a><br/><br/>
<table border="1" cellpadding="3" cellspacing="3">
<tr>
<th width="30">No. </th>
<th width="120">Customer Id</th>
<th width="120">First Name</th>
<th width="120">Last Name</th>
<th width="120">Address</th>
<th width="100">Contact</th>
<th width="25"> </th>
<th width="25"> </th>
</tr>
<?php
$n = 0;
while($row = mysql_fetch_array($rsd))
{
$n = $n + 1;
$f1 = $row['customerid'];
$f2 = $row['firstname'];
$f3 = $row['lastname'];
$f4 = $row['address'];
$f5 = $row['contact'];
?>
<tr>
<td><?php echo $n; ?></td>
<td><?php echo $f1; ?></td>
<td><?php echo $f2; ?></td>
<td><?php echo $f3; ?></td>
<td><?php echo $f4; ?></td>
<td><?php echo $f5; ?></td>
<td><a href="update_customer.php?id=<?php echo $f1 ?>">Edit</a></td>
<td><a href="delete_customer_data.php?id=<?php echo $f1 ?>">Delete</a></td>
</tr>
<?php
} //while
?>
</table>
</body>
</html>
Create file new_customer.php to add new customer form
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Add New Customer</title>
</head>
<body>
<form method="post" action="save_customer_data.php">
Customer Id<br/>
<input type="text" name="f1"/><br/>
First Name<br/>
<input type="text" name="f2"/><br/>
Last Name<br/>
<input type="text" name="f3"/><br/>
Address<br/>
<textarea rows="3" name="f4"></textarea><br/>
Contact<br/>
<input type="text" name="f5"/><br/>
<input type="submit" value="Save" title="Save"/>
</form>
</body>
</html>
Create file save_customer_data.php to save customer data into database
<?php
include('includes/config.php');
$f1 = $_POST['f1']; //Customer id
$f2 = $_POST['f2']; //First Name
$f3 = $_POST['f3']; //Last Name
$f4 = $_POST['f4']; //Address
$f5 = $_POST['f5']; //Contact
mysql_query("INSERT INTO tbl_customers (customerid, firstname, lastname, address, contact) VALUES ('". $f1 ."', '". $f2 ."', '". $f3 ."', '". $f4 ."', '". $f5 ."')");
mysql_close($bd);
header("location: http://www.want2learn.info/website/");
?>
Create file delete_customer_data.php to delete customer from database
<?php
include('includes/config.php');
$id = $_GET['id'];
mysql_query("DELETE FROM tbl_customers WHERE customerid='". $id ."'");
mysql_close($bd);
ob_start();
header("location: http://www.want2learn.info/website/");
?>
Create file update_customer.php to update customer
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Update Customer</title>
</head>
<body>
<form action="save_update_customer_data.php" method="post">
<?php
include('includes/config.php');
$id = $_GET['id'];
$result = mysql_query("SELECT * FROM tbl_customers WHERE customerid='". $id ."'") or die($result."<br/><br/>".mysql_error());
while($row = mysql_fetch_array($result, MYSQL_BOTH))
{
echo "<input name='f1' value='". $id ."' type='hidden'/>";
echo "First Name<br/>";
echo "<input name='f2' value='". $row['firstname'] ."' type='text'/><br/>";
echo "Last Name<br/>";
echo "<input name='f3' value='". $row['lastname'] ."' type='text'/><br/>";
echo "Address<br/>";
echo "<textarea name='f4' rows='3'>". $row['address'] ."</textarea><br/>";
echo "Contact<br/>";
echo "<input name='f5' value='". $row['contact'] ."' type='text'/><br/>";
}
?>
<br/>
<input type="submit" title="Update" value="Update"/>
</form>
</body>
</html>
Create file save_update_customer_data.php to save update customer into database
<?php
include('includes/config.php');
if(isset($_POST['f1'], $_POST['f2'], $_POST['f3'], $_POST['f4'], $_POST['f5'])){
$f1 = $_POST['f1']; //Customer id
$f2 = $_POST['f2']; //First Name
$f3 = $_POST['f3']; //Last Name
$f4 = $_POST['f4']; //Address
$f5 = $_POST['f5']; //Contact
mysql_query("UPDATE tbl_customers SET firstname='". $f2 ."', lastname='". $f3 ."', address='". $f4 ."', contact='". $f5 ."' WHERE customerid='". $f1 ."'");
mysql_close($bd);
header("location: http://www.want2learn.info/website/");
}
?>
