CRUD(Insert, Delete, Update, Display/Retrieve) Using PHP
//Make another php file for connection purpose(seprate one)
<?php
$connect= mysqli_connect("localhost", "root", "", "login");
if($connect)
{
echo "database connected successfully";
}
else
{
die("error in connection". mysqli_error($connect));
}
?>
//make a main file that includes a Form parameters, insert ,delete ,update and selection logic.
<?php
//this file is a separate file for connection that must be includes in main file using include() function.//
include('db.php');
?>
<html>
<head>
<title>login example</title>
</head>
<body>
<form method="POST" >
<p>id:</p>
<input type="text" name="id" placeholder="enter your id">
<p>name:</p>
<input type="text" name="name" placeholder="enter your name">
<p>email:</p>
<input type="email" name="email" placeholder="enter your mail id">
<p>password:</p>
<input type="password" name="password" placeholder="enter your mail id">
<p>
<input type="submit" value="submit" name="Submit"/>
</p>
<p><input type="submit" value="Update" name="Update"/>
</p>
<p><input type="submit" value="View" name="View"/>
</p>
<p><input type="submit" value="Delete" name="Delete">
</p>
</form>
<?php
if(isset($_REQUEST["Submit"]))
{
$i=$_POST["id"];
$n=$_POST["name"];
$e=$_POST["email"];
$p=$_POST["password"];
$query="insert into lg values('$i','$n','$e','$p')";
$data= mysqli_query($connect, $query);
if($data)
{
echo "data has been inserted successfully";
}
else{
die("error in sql insertion".mysqli_error($connect));
}
}
?>
<?php
if(isset($_REQUEST["Update"]))
{
$i=$_POST["id"];
$n=$_POST["name"];
$e=$_POST["email"];
$p=$_POST["password"];
//here Lg_Name,Lg_Id ,Lg_Email and Lg_Password are the table attributes from the database.
$query="update lg set Lg_Name='$n',Lg_Email='$e',Lg_Password='$p' where Lg_Id='2'";
$data= mysqli_query($connect, $query);
if($data)
{
echo "data updated successfully.";
}
}
?>
<?php
if(isset($_REQUEST["View"]))
{
$query="select *from lg";
$data= mysqli_query($connect, $query);
while($res= mysqli_fetch_array($data))
{
echo "</br>";
echo "<p>".$res["Lg_Id"].$res["Lg_Name"].$res["Lg_Email"].$res["Lg_Password"]."</p>";
}
}
?>
<?php
if(isset($_REQUEST["Delete"]))
{
$i=$_POST["id"];
$query="delete from lg where Lg_Id='$i'";
$data= mysqli_query($connect, $query);
if($data)
{
echo "data deleted successfully";
}
else
{
die("data not exists..");
}
}
?>
</body>
</html>
Comments