Você está na página 1de 13

PHP Tutorial for (Record Insertion retrieval deletion and

updating)
In this tutorial u will learn following things

How
How
How
How
How
How

to
to
to
to
to
to

create a form using html and CSS


crate database in phpmyadmin
insert record in database
show record on webpage from database
delete record from database
edit and update record in database

Note :
Create a folder in Xamp/ htdocs/
Save all files in the folder.
Save CSS file in same folder.

How to create a form using html and CSS


First of all you need a tool to work on Like notepad, notepad ++
I created a simple form using html and external CSS.
To attach css in html use following code in the head tag
<link rel="stylesheet" href="style.css" type="text/css" />

Html code for 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 http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body>
<div id="header">
<div id="left">
<label>Register Customer</label>
</div>
<div id="right">
</div>
</div>
<center>

<div id="login-form">
<form method="post"action ="submit.php">
<table align="center" width="35%" border="0">
<tr>
<td><input type="text" name="customer_name" placeholder="customer
Name" required /></td>
</tr>
<tr>
<td><input type="text" name="contact_name" placeholder="Contact
Name" required /></td>
</tr>
<tr>
<td><input type="text" name="city" placeholder="City" required /></td>
</tr>
<tr>
<td><input type="text" name="country" placeholder="Country" required
/></td>
</tr>
<tr>
<td> <button><input type="submit" name="submit" value ="Enter
Record"></button></td>
</tr>
</table>
</form>
</div>
</center>
</body>
</html>
Save this file with register.php

How to create database in phpmyadmin


To create data base you need to have XAMP
Download XAMP from https://www.apachefriends.org/index.html
Install it
Open XAMP

Start apache
Start MySQL
Goto LocalHost/phpmyadmin
Create database with name (raeesdb) here

Goto database and create table with name (customer)


Following are the fields of database
i)
ii)
iii)
iv)
v)

customer_id datatype INT size (10) auto increment primary key


customer_name datatype varchar size (50)
contact_name datatype varchar size (50)
city datatype varchar size (50)
country datatype varchar size (50)

Once data base is create now need to connect to the database


For connecting with database you need to write the following PHP code
<?php
$mysql_hostname = "localhost";
$mysql_user = "root";
$mysql_password = "";
$mysql_database = "raeesdb";
$con = mysqli_connect($mysql_hostname, $mysql_user,
$mysql_password) or die("Could not connect database");
if ($con==true )
{
//echo "true con";
mysqli_select_db( $con,$mysql_database) or die("Could
not select database");
}
else{
echo "abc";
}
?>
Save this file with dbconnection.php

How to insert record in database


Code for record insertion in the database
<!-- insertion of data into database-->

<?php
include("dbconnection.php");
if($_POST['submit'])
{
$customer_name=$_POST['customer_name'];
$contact_name=$_POST['contact_name'];
$city=$_POST['city'];
$country=$_POST['country'];
if ($con==true )
{
//echo "true con";
mysqli_select_db( $con,$mysql_database) or die("Could not
select database");
}
else{
echo "abc";
}
mysqli_query($con,"INSERT INTO customer(customer_name,
contact_name, city, country )
VALUES('$customer_name','$contact_name','$city','$country')");
header("location: show.php");
mysqli_close($con);
}
?>
Save this file with submit.php

How to show record on webpage from database


Code to show the record from database on webpage
<!--Retrieval of data from Database and showing it on webpage -->
<!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 http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<link rel="stylesheet" href="style.css" type="text/css" />
</head>

<body>
<?php
include("dbconnection.php");
$selectSQL = 'SELECT * FROM customer';
if( !( $selectRes = mysqli_query($con, $selectSQL) ) ){
echo 'Retrieval of data from Database Failed - #'.mysql_errno().': '.mysql_error();
}else{}
if( mysqli_num_rows( $selectRes )==0 ){
// echo '<tr><td colspan="4">No Rows Returned</td></tr>';
}else{
}
mysqli_close($con);
?>
<div id="header">
<div id="left">
<label>View Customer Detail</label>
</div>
<div id="right">
</div>
</div>
<table align="center" width="75%" border="1">
<thead>
<tr>
<th>Customer ID</th>
<th>Customer Name</th>
<th>Contact Name</th>

<th>City</th>
<th>Country</th>
<th> Delete user<th>
</tr><?php
while( $row = mysqli_fetch_array( $selectRes ) ){
?>
<tr>
<td><?php echo $row['customer_id']; ?></td></td>
<td><?php echo $row['customer_name']; ?></td>
<td><?php echo $row['contact_name'];?></td>
<td><?php echo $row['city']; ?></td>
<td><?php echo $row['country'];?></td>
<td><a href='delete.php?cusid=<?php echo $row["customer_id"]; ?
>'>Delete</a></td>
<td><a href='edit.php?cusid=<?php echo $row["customer_id"]; ?
>'>Edit</a></td>
</tr>
<?php } ?>
</thead>
<tbody>
</tbody>
</table>
</body>
</html>
Your record will be look like this
Note: in this table record is already added

Save this file with name show.php

How to delete record from database


Code for record deletion from database
<!-- Deletion of data from database-->
<?php
include("dbconnection.php");
$id= $_GET['cusid'];

$query="DELETE FROM customer WHERE customer_id='$id' ";


$exequery=mysqli_query($con,$query);
if($exequery)
{
header("location: show.php");
}
mysqli_close($con);
?>
Add delete link on show.php page
<td><a href='delete.php?cusid=<?php echo $row["customer_id"]; ?
>'>Delete</a></td>

Save this file with name delete.php

How to edit and update record in database


To edit and update record you need to create new page where data will be
displayed
First you need to create page with 5 fields containing

(1) customer id (type hidden)


(2) customer name
(3) contact name
(4) city
(5) country
complete code for fetching record from database and show it in text boxes
<?php
include("dbconnection.php");
if(isset($_GET['cusid']) && $_GET['cusid'] !=""){
$id = $_GET['cusid'];
$q = "SELECT * FROM customer WHERE customer_id= '$id'";
$r= mysqli_query($con,$q);
$user= mysqli_fetch_assoc($r);
}
mysqli_close($con);
?>
<!-- Form for updating the record-->
<!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 http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body>
<div id="header">
<div id="left">
<label>Register Customer</label>
</div>
<div id="right">
</div>
</div>
<center>
<div id="login-form">
<form method="POST" name = "update record" action ="update.php">
<table align="center" width="35%" border="0">
<tr>
<td> <input type="hidden" name="customer_id" value ="<?php echo $id ?
>">
</tr>
<tr>
<td><input type="text" name="customer_name" value ="<?php echo
$user['customer_name'] ?>"/></td>
</tr>
<tr>
<td><input type="text" name="contact_name" value ="<?php echo
$user['contact_name'] ?>"/></td>
</tr>

<tr>
<td><input type="text" name="city" value ="<?php echo $user['city'] ?
>"></td>
</tr>
<tr>
<td><input type="text" name="country" value ="<?php echo
$user['country'] ?>"/></td>
</tr>
<tr>
<td><input id="button" type="submit" name="submit" value ="Update
Record"></td>
</tr>
</table>
</form>
</div>
</center>
</body>
</html>
Add edit link on show.php page
<td><a href='delete.php?cusid=<?php echo $row["customer_id"]; ?
>'>Delete</a></td>

Save this file with name edit.php


Now need to update the record in the database
Complete code for updating record in database
<?php
include("dbconnection.php");
$id= $_POST['customer_id'];
$customer_name=$_POST['customer_name'];
$contact_name=$_POST['contact_name'];
$city=$_POST['city'];
$country=$_POST['country'];
$query= "UPDATE customer SET customer_name=
'$customer_name', contact_name='$contact_name', city='$city',
country='$country' WHERE customer_id='$id'";
$exequery=mysqli_query($con,$query);
if($exequery)
{
header("location: show.php");;
}
mysqli_close($con);
?>

Você também pode gostar