How to create CRUD in php with mysql?
In This tutorial, we learn how to create CRUD (create,Read,Update and Delete) operation in php with mysql database.
Create Database Table :- users
Table Name : users
CREATE TABLE `tblusers` (
`id` int(10) NOT NULL AUTO_INCREMENT PRIMARY KEY,
`first_Name` varchar(200) DEFAULT NULL,
`last_Name` varchar(200) DEFAULT NULL,
`mobile_Number` bigint(10) DEFAULT NULL,
`email` varchar(200) DEFAULT NULL,
`address` mediumtext DEFAULT NULL,
`created_at` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
Create Database connection
File Name : dbconnection.php
<?php
$host = "localhost";
$username ="ittutorial";
$password = "sana*mahtab";
$db_name = "itechxpert";
$conn = mysqli_connect($host,$username,$password)or die(mysqli_error());
mysqli_select_db($conn,$db_name)or die(mysqli_error($conn));
// $conn = mysqli_connect("localhost", "root", "", "itechxpert");
// $conn = mysqli_connect($host,$username,$password,$db_name)or die(mysqli_error());
#################### OR ##############################
define ( 'DB_HOST', 'localhost' );
define ( 'DB_USER', 'gmax' );
define ( 'DB_PASSWORD', 'gmaxpass' );
define ( 'DB_NAME', 'gmaxdb' );
$conn = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD) or die('Could not connect to database server.');
mysqli_select_db($conn,DB_NAME) or die('Could not select database.');
// // $conn = mysqli_connect("localhost", "root", "", "itechxpert");
// $conn = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD,DB_NAME) or die('Could not connect to database server.');
?>
Create Html Application Form for user data submit and save into the users table into database.
File Name : user-register.php
<html>
<head>
<title> User Registration Form ittutorial.in</title>
</head>
<body>
<form method="POST" name="frm" action="user-register-action.php">
<h2> User Registration Form ittutorial.in </h2>
<div class="form-group">
<div class="row">
<div class="col"><input type="text" class="form-control" name="fname" placeholder="First Name" required="true"></div>
<div class="col"><input type="text" class="form-control" name="lname" placeholder="Last Name" required="true"></div>
</div>
</div>
<div class="form-group">
<input type="text" class="form-control" name="contactno" placeholder="Enter your Mobile Number" required="true" maxlength="10" pattern="[0-9]+">
</div>
<div class="form-group">
<input type="email" class="form-control" name="email" placeholder="Enter your Email id" required="true">
</div>
<div class="form-group">
<textarea class="form-control" name="address" placeholder="Enter Your Address" required="true"></textarea>
</div>
<div class="form-group">
<button type="submit" class="btn btn-success btn-lg btn-block" name="submit">Submit</button>
</div>
</form>
</body>
</html>
Insert Data
Now we write PHP Code for data insertion.
Once the user filled application form and click on the submit button then data will be saved in the database users table.
File Name : user-register-action.php
<?php
include 'dbconnection.php';
// include('dbconnection.php');
if(isset($_POST['submit']))
{
//first getting the inputed post values
$fname=$_POST['fname'];
$lname=$_POST['lname'];
$contno=$_POST['contactno'];
$email=$_POST['email'];
$add=$_POST['address'];
// Query for data insertion into database
$qry=mysqli_query($conn, "insert into users(first_name,last_name, mobile_number, email, address) value('$fname','$lname', '$contno', '$email', '$add' )");
if ($qry) {
echo "<script>alert('You have successfully submitted');</script>";
echo "<script > document.location ='user-list.php'; </script>";
}
else
{
echo "<script>alert('Something Went Wrong. Please try again');</script>";
echo "<script > document.location ='user-register.php'; </script>";
}
}
?>
Fetch / Show the user list
Fetch the record from the database and show the user-list.php page
File Name : user-list.php
<?php include 'dbconnection.php'; ?>
<html>
<head>
<title> Fetch / Show the user list of itechxpert.in </title>
</head>
<body>
<table class="table table-striped table-hover">
<thead>
<tr>
<th>#</th>
<th>Name</th>
<th>Email</th>
<th>Mobile Number</th>
<th>Created Date</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php
$result = mysqli_query($conn,"select * from users");
$cnt=1;
$total_row = mysqli_num_rows($result);
if($total_row>0){
while ($row=mysqli_fetch_array($result)) {
?>
<!--Fetch the Records -->
<tr>
<td><?php echo $cnt;?></td>
<td><?php echo $row['first_name'];?> <?php echo $row['last_name'];?></td>
<td><?php echo $row['mobile_number'];?></td>
<td><?php echo $row['Email'];?></td>
<td><?php echo $row['address'];?></td>
<td> <?php echo $row['created_at'];?></td>
<td>
<a href="user-details.php?user_id=<?php echo htmlentities ($row['id']);?>" class="view" title="View" data-toggle="tooltip"><i class="material-icons"></i></a>
<a href="user-edit.php?user_id=<?php echo htmlentities ($row['id']);?>" class="edit" title="Edit" data-toggle="tooltip"><i class="material-icons"></i></a>
<a href="user-delete.php?user_id=<?php echo ($row['id']);?>" class="delete" title="Delete" data-toggle="tooltip" onclick="return confirm('Do you really want to Delete ?');"><i class="material-icons"></i></a>
</td>
</tr>
<?php
$cnt=$cnt+1;
} } else {?>
<tr>
<th style="text-align:center; color:red;" colspan="6">No Record Found</th>
</tr>
<?php } ?>
</tbody>
</table>
</body>
</html>
View the single record details:
create user-details.php file. For fetching a record we have to get the row id of that record and store in $vid. We access the $_GET[?viewid?] variable to do it.
File Name : user-details.php
<?php include 'dbconnection.php'; ?>
<html>
<head>
<title> Fetch / Show the user details of itechxpert.in </title>
</head>
<body>
<table cellpadding="0" cellspacing="0" border="0" class="display table table-bordered" id="hidden-table-info">
<tbody>
<?php
$uid=$_GET['user_id'];
$result=mysqli_query($conn,"select * from users where id =$uid");
$cnt=1;
while ($row=mysqli_fetch_array($result)) {
?>
<tr>
<th>First Name</th>
<td><?php echo $row['first_name'];?></td>
<th>Last Name</th>
<td><?php echo $row['last_name'];?></td>
</tr>
<tr>
<th>Email</th>
<td><?php echo $row['email'];?></td>
<th>Mobile Number</th>
<td><?php echo $row['mobile_number'];?></td>
</tr>
<tr>
<th>Address</th>
<td><?php echo $row['address'];?></td>
<th>Creation Date</th>
<td><?php echo $row['created_at'];?></td>
</tr>
<?php
$cnt=$cnt+1;
}?>
</tbody>
</table>
</body>
</html>
Edit/ Update the particular record
create user-edit.php file For updating a record. we have to get the row id of that record and store in $uid. We access the $_GET[?user_id?] variable to do it.
File Name : user-edit.php
<?php include 'dbconnection.php'; ?>
<html>
<head>
<title> update the user details of itechxpert.in </title>
</head>
<body>
<form method="POST" name="frm" action="edit-action.php">
<?php
$uid=$_GET['user_id'];
$result = mysqli_query($con,"select * from users where id='$uid'");
while ($row=mysqli_fetch_array($result)) {
?>
<h2>Update user details</h2>
<div class="form-group">
<div class="row">
<div class="col"><input type="text" class="form-control" name="fname" value="<?php echo $row['first_name'];?>" required="true"></div>
<div class="col"><input type="text" class="form-control" name="lname" value="<?php echo $row['last_name'];?>" required="true"></div>
</div>
</div>
<div class="form-group">
<input type="text" class="form-control" name="contactno" value="<?php echo $row['mobile_number'];?>" required="true" maxlength="10" pattern="[0-9]+">
</div>
<div class="form-group">
<input type="email" class="form-control" name="email" value="<?php echo $row['email'];?>" required="true">
</div>
<div class="form-group">
<textarea class="form-control" name="address" required="true"><?php echo $row['address'];?></textarea>
</div>
<?php
}?>
<div class="form-group">
<button type="submit" class="btn btn-success btn-lg btn-block" name="submit">Update</button>
</div>
</form>
</body>
</html>
edit-action
File Name : edit-action.php
<?php
include('dbconnection.php');
if(isset($_POST['submit']))
{
$uid=$_GET['user_id'];
//Getting Post Values
$fname=$_POST['fname'];
$lname=$_POST['lname'];
$contno=$_POST['contactno'];
$email=$_POST['email'];
$add=$_POST['address'];
//Query for data updation
$qry=mysqli_query($conn, "update users set first_name='$fname',last_name='$lname', mobile_number='$contno', email='$email', address='$add' where id='$uid'");
if ($query) {
echo "<script>alert('You have successfully updated user details');</script>";
echo "<script > document.location ='index.php'; </script>";
}
else
{
echo "<script>alert('Something Went Wrong. Please try again');</script>";
}
}
?>
Delete Record
delete recotd from the database table.
File Name : user-delete.php
<?php
include('dbconnection.php');
//Code for deletion
if(isset($_GET['user_id']))
{
$uid=intval($_GET['user_id']);
$sql = mysqli_query($conn,"delete from users where id=$uid");
echo "<script>alert('Data deleted');</script>";
echo "<script>window.location.href = 'user-list.php.php'</script>";
}
?>
Previous
Next