php crud

Insert, Show, Update and Delete in PHP & MySQL.


hi friends this blog explains is how to insert, show, update and delete in PHP & MySQL.

INSERT, UPDATE, DELETE and SHOW Record from Database Using PHP and Mysqli.

welcome all viewers thanks for reading and learning by myprograming.com now explain how to create PHP & MySQL in the insert, show, update and delete query. follow me and learn more about how to create. first, open your localhost/PHPMyAdmin and create a new database and create a new table

  1. Create new Database
  2. Create new table
  3. Create connection.php file
  4. Create index.php file
  5. Create show.php file
  6. Create update.php file
  7. Create delete.php file

1.Create new Database

create new database ( myprograming ). myprograming is database name.


2.Create new table

create new table ( reg ). reg is table name.


3.Create connection.php file

now open your code editor software and create a connection.php

<?php 

// Enter your Host, username, password, database below.
// I left password empty because i do not set password on localhost.
$con=mysqli_connect('localhost','root','','myprograming') or die("connection failed : ".mysqli_connect_error());

if ($con) {
	// echo "Connection Successfully";
}
else{
	echo "Sorry Some Mistakes is";
}
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

?>

4.Create index.php file

create an index.php file in your computer and create a form now add PHP in your index.php file and include your connection.php file also.

<!DOCTYPE html>
<html>
<head>
	<title>insert</title>
</head>
<body>
<div class="row text-center">
	<div class="container">
		<h1>Insert DATA</h1>
	<form action="index.php" method="post">
	
	<input type="text" name="firstname" placeholder="firstname"><br><br>
	<input type="text" name="lastname" placeholder="lastname"><br><br>
	<input type="gmail" name="gmail" placeholder="gmail"><br><br>
	<input type="text" name="number" placeholder="number"><br><br>
	<input type="text" name="address" placeholder="address"><br><br>
	<input type="submit" name="submit" value="insert"><br><br>
	
	</form>

<button><a href="show.php">show data</a></button>
	</div>
</div>

</body>
</html>
<?php 
error_reporting(0);
include 'connection.php';

if (isset($_POST['submit'])) {

	$firstname = $_POST['firstname'];
	$lastname = $_POST['lastname'];
	$gmail = $_POST['gmail'];
	$number = $_POST['number'];
	$address = $_POST['address'];


	$sql = "INSERT INTO `reg` VALUES ('$id','$firstname','$lastname','$gmail','$number','$address')";
	
	$data=mysqli_query($con,$sql);

	if ($data) {
		echo "insert";
	}else
	{
		echo "sorry";
	}
}
?>




5.Create show.php file

create a show.php file on your computer and create a Table now add PHP in your show.php file and include your connection.php file also.

<!DOCTYPE html>
<html>
<head>
	<title>show table</title>
</head>
<body>
<?php 

include ('connection.php');
$sql ="select * from reg";
$data =mysqli_query($con,$sql);
$total=mysqli_num_rows($data);


if ($total=mysqli_num_rows($data)) {
	?>
	<table border="2">
<tr>
<th>id</th>
<th>firstname</th>
<th>lastname</th>
<th>gmail</th>
<th>number</th>
<th>address</th>
<th>delete</th>
<th>update</th>
</tr>

	<?php
	
	while ($result = mysqli_fetch_array($data)) {

		echo " 
			<tr>
				<td>".$result['id']."</td>
				<td>".$result['firstname']."</td>
				<td>".$result['lastname']."</td>
				<td>".$result['gmail']."</td>
				<td>".$result['number']."</td>
				<td>".$result['address']."</td>
				<td><a href='update.php?id=$result[id] & firstname=$result[firstname] & lastname=$result[lastname] & gmail=$result[gmail] & number=$result[number] &address=$result[address]'> update </a></td>
				<td><a href='delete.php?id=$result[id] '>delete </a></td>
			</tr>
		";
	}
}
else
{
 	echo "no record found";
}

?>
</table>
</body>
</html>	
 <!--------- echo "<br>".$result['id']."  ".$result['firstname']." ".$result['lastname']." ".$result['gmail']."  ".$result['number']."  ".$result['address']."<br>";_----->

6.Create update.php file

create a update.php file on your computer and include your connection.php file also.

<!DOCTYPE html>
<html>
<head>
	<title>update</title>
</head>
<body>

<form action="" method="get">
	<input type="text" name="id" placeholder="id" value="<?php echo $_GET['id']; ?>"><br><br>
	<input type="text" name="firstname" placeholder="firstname" value="<?php echo $_GET['firstname']; ?>"><br><br>
	<input type="text" name="lastname" placeholder="lastname" value="<?php echo $_GET['lastname']; ?>" ><br><br>
	<input type="gmail" name="gmail" placeholder="gmail" value="<?php echo $_GET['gmail']; ?>"><br><br>
	<input type="text" name="number" placeholder="number" value="<?php echo $_GET['number']; ?>"><br><br>
	<input type="text" name="address" placeholder="address" value="<?php echo $_GET['address']; ?>"><br><br>
	<input type="submit" name="submit" value="update">
	
</form>

<?php 
error_reporting(0);
include ('connection.php');
if ($_GET['submit'])
{
	$id = $_GET['id'];
	$firstname = $_GET['firstname'];
	$lastname = $_GET['lastname'];
	$gmail = $_GET['gmail'];
	$number = $_GET['number'];
	$address = $_GET['address'];



 	$sql="UPDATE reg SET firstname='$firstname' , lastname='$lastname', gmail='$gmail' , number='$number', address='$address'   WHERE id='$id'";
 	
 	$data=mysqli_query($con, $sql);
   	
 	if ($data) {
 		//echo "record update";
 		header('location:show.php');
 	}
 	else{
 		echo "not update";
 	}
}
else
{
	echo "click on  button to save the change";
}
?>

</body>
</html>

7.Create delete.php file

create a delete.php file on your computer and include your connection.php file also.

<?php 

include ('connection.php');

$id = $_GET['id'];
$sql ="DELETE FROM `reg` WHERE id='$id'";

$data = mysqli_query($con,$sql);

if ($data) {
	echo "deleted";
	header('location:show.php');
}else
{
	echo "error";
}

 ?>

Insert, Show, Update and delete in PHP zip.file here Download link.


Related Posts

One thought on “Insert, Show, Update and Delete in PHP & MySQL.

Leave a Reply

Your email address will not be published. Required fields are marked *