simple crud(Create,Read,Update,Delete) operation in PHP MySQL

How to perform  CRUD operation in PHP.

First, understand what crud operation is?

CRUD operation or CRUD Application in PHP is a Reading, Updating and deleting simple operations in the PHP MySQL database.

Here you easily understand because all PHP crud operation step by step explained.

Step: 1
Start your xampp server and Open any browser.

Step: 2
In the URL bar, type http://localhost/phpmyadmin/ and create database crudp.

Step: 3
After, create the table crud and insert data.

Step: 4
Open any text editor like notepad, notepade++, etc.

Create file config.php 

Step: 
Save files in C:\xampp\htdocs/your folder

Step: 6
Thus, make an index.php to insert data.

Make select.php file for fetch data into MySQL database.

Make update.php file for Update data into MySQL database.

And last make a delete.php file to delete data 


Create Database named
crudp

CREATE TABLE IF NOT EXISTS `crud` (
  `id` int(10) NOT NULL AUTO_INCREMENT,
  `name` varchar(110) NOT NULL,
  `email` varchar(20) NOT NULL,
  PRIMARY KEY (`id`) 
) ENGINE=InnoDB DEFAULT CHARSET=latin1                      AUTO_INCREMENT=1 ;                                                          


Database Connection File 
config.php


<?php
$host='localhost';
$user='root';
$pass='';
$data='crudp';

$conn=mysqli_connect($host,$user,$pass,$data);

if(!$conn)
{
die(mysqli_error());
}

?>


Main Form file 
index.php
simple crud operation in php

You may also like



<?php
include'config.php';
if(isset($_POST['insert']))
{
$name=$_POST['name'];
$email=$_POST['email'];

$query=mysqli_query($conn,"insert into crud(name,email)values('$name','$email')");

}
?>

<html>
<head><title>crud operaration</title>
</head>
<body>
<form action="index.php"method="post">
Name:-<input type="text"name="name"placeholder="Your Name"required><br>
Email:-<input type="email"name="email"placeholder="Your Email"required><br>
<p>&emsp;&emsp;&emsp;<input type="submit"name="insert"value="INSERT">
</form>
<a href='select.php'>Display</a>
</body>
</html>


Fetch Data in Mysql Database 
select.php
simple crud operation in php select data



<?php
include'config.php';

$query=mysqli_query($conn,"select *from crud order by id desc");

echo"<table border='1';><tr>
<th>ID</th>
<th>NAME</th>
<th>EMAIL</th>
<th>Edit</th>
<th>Delete</th></tr>";

while($row=mysqli_fetch_array($query))
{ 
echo"<tr>";
echo"<td>".$row['id']."</td>";
echo"<td>".$row['name']."</td>";
echo"<td>".$row['email']."</td>";
echo"<td><a href='update.php?id=$row[id]'>Edit</a></td>";
echo"<td><a href='delete.php?id=$row[id]'>Delete</a></td>";
echo"</tr>";
}
echo"</table><p>";
echo"<a href='index.php'>New Record Insert</a>";
?>


Update Data using 
update.php 
simple crud operation in php update data



<?php
include'config.php';
if(isset($_POST['update']))
{
 
 $id=$_POST['id'];
 $name=$_POST['name'];
 $email=$_POST['email'];
$result=mysqli_query($conn,"update crud set name='$name',email='$email'where id=$id");
header('location:select.php');
}

?>

<?php
$id=$_GET['id'];

$query=mysqli_query($conn,"select *from crud");
while($row=mysqli_fetch_array($query))
{
 $name=$row['name'];
 $email=$row['email'];
}

?>

<html>
<head><title>untitle</title></head>
<body>
<form action="update.php"method="post">
Name:-<input type="text"name="name"placeholder="Update Name"value="<?php echo $name;?>"required><br>
Email:-<input type="email"name="email"placeholder="Update Email"value="<?php echo $email;?>"required><br>
<input type="hidden"name="id"value="<?php echo $_GET['id'];?>"><br>
&emsp;&emsp;&emsp;<input type="submit"name="update"value="Update">
</body>
</html>


Delete The Data
delete.php
<?php
include'config.php';
$id=$_GET['id'];
mysqli_query($conn,"delete from crud where id=$id");
header('location:select.php');
?>



Comments

  1. Hello friend,
    I'm beginner in PHP, MySQL please help.

    I'm performing crud operation create, read, delete this 3 operation is performing well, but when I update data then error generates

    Warning: mysql_fetch_array () expects parameter 1 to be resource, boolean given in D:\xampp\htdocs\crudo\edit.php on line 36

    A lot of trying but not I'm unable to find the error.

    edit.php file here



    When I click on edit button, then show a warning

    Warning: mysql_fetch_array () expects parameter 1 to be resource, boolean given in D:\xampp\htdocs\crudo\edit.php on line 36

    Please help to solve this warning

    ReplyDelete

Post a Comment