Friday, July 31, 2020

CRUD OPERATION IN PHP

Tags




Validation with Php.

Step-1. valinser.php


<?php 

$conn=mysqli_connect("localhost","root","","validationcrud")or die("Error in connection");

if(isset($_GET['eid']))
{
$eid=$_GET['eid'];

$query="select * from crud where id='$eid'";
$res=mysqli_query($conn,$query);
$row=mysqli_fetch_array($res);

}

 ?>

<!DOCTYPE html>
<html>
<head>
<title>CRUD OPeration</title>
</head>
<style type="text/css">
body{
background-color: #e33;
font-size:18px;

}
input[type="text"],input[type="email"]{
width:300px;
height:30px;
}
input[type="submit"]
{
height:30px;
width:200px;
}

</style>

<body>
<form action="" method="post">
<p><h1><center>Registration Form</center></h1></p>
<table border="2" align="center">
<tr>
<input type="hidden" name="id">
</tr>
<tr>
<td>Name</td>
<td><input type="text" name="fname"></td>
</tr>
<tr>
<td>Phone</td>
<td><input type="text" name="email"></td>
</tr>
<tr>
<td>Email</td>
<td><input type="text" name="phone"></td>
</tr>
<tr>
<td>Image</td>
<td><input type="file" name="image"></td>
</tr>
<tr>
<td>Submit</td>
<td colspan="2" align="center"><input type="submit" name="signup" value="signup"></td>
</tr>
</table>
</form>
<?php 
if(isset($_POST['signup'])){
$fname=$_POST['fname'];
$email=$_POST['email'];
$phone=$_POST['phone'];
$image=$_POST['image'];
// $type=$_FILES['image']['type'];
// $name1=$_FILES['image']['name1'];
// $size=$_FILES['image']['size'];
// $source=$_FILES['image']['tmp_name'];
// $dest="upload".$name1;


if($fname==""){
echo " First Name Is Required..";
}
elseif($email==""){
echo "Email is Required..";
}
elseif($phone==""){
echo "Plzz type Valid Email..";
}
elseif($image==""){
echo "Image is Required..";
}
else{
$query="insert into crud values('','$fname','$email','$phone','$image')";
if(mysqli_query($conn,$query))
{
echo "Record is Inserted..";
}
else
{
echo "Record is Not Inserted..";
}
}
}

?>
</body>
</html>



Step:- 2  view.php


<?php 

$conn=mysqli_connect("localhost","root","","validationcrud")or die("error in connection");


$query="select * from crud";
$res=mysqli_query($conn,$query);

 ?>
 <html>
  <body>
  <table border="2" align="center">
  <th>Id</th>
  <th>First Name</th>
  <th>Email</th>
  <th>Phone</th>
  <th>Image</th>
  <th>Action</th>
<?php
while($row=mysqli_fetch_array($res))
{
?>
<tr>
<td><?php echo $row['id']?></td>
<td><?php echo $row['fname']?></td>
<td><?php echo $row['email']?></td>
<td><?php echo $row['phone']?></td>
<td><?php echo $row['image']?></td>
<td>
<a href="upval.php?eid=<?php echo $row['id'];?>">Edit /
<a href="view.php?did=<?php echo $row['id'];?>">Delete</a>
</td>
</tr>
<?php
}
?>

  </table>
  </body>
 </html>

 <?php 

 if(isset($_GET['did']))
 {
  $did=$_GET['did'];

  $query="delete from crud where id='$did'";

  if(mysqli_query($conn,$query))
  {
  echo "Record is Deleted..";
  }
  else
  {
  echo "Record is Not Deleted..";
  }
 }

  ?>

Step:-3 now update upval.php


<?php 

$conn=mysqli_connect("localhost","root","","validationcrud")or die("Error in connection");

if(isset($_GET['eid']))
{
$eid=$_GET['eid'];

$query="select * from crud where id='$eid'";
$res=mysqli_query($conn,$query);
$row=mysqli_fetch_array($res);

}

 ?>

<!DOCTYPE html>
<html>
<head>
<title>CRUD OPeration</title>
</head>
<style type="text/css">
body{
background-color: #e33;
font-size:18px;

}
input[type="text"],input[type="email"]{
width:300px;
height:30px;
}
input[type="submit"]
{
height:30px;
width:200px;
}

</style>

<body>
<form action="" method="post">
<p><h1><center>Registration Form</center></h1></p>
<table border="2" align="center">
<tr>
<input type="hidden" name="id"  value="<?php 
if(isset($_GET['eid']))
{
echo $row['id'];
}

?>">
</tr>

<tr>
<td>Name</td>
<td><input type="text" name="fname" value="<?php 
if(isset($_GET['eid']))
{
echo $row['fname'];
}

?>"></td>
</tr>
<tr>
<td>Phone</td>
<td><input type="text" name="email" value="<?php 
if(isset($_GET['eid']))
{
echo $row['email'];
}

?>"></td>
</tr>
<tr>
<td>Email</td>
<td><input type="text" name="phone" value="<?php 
if(isset($_GET['eid']))
{
echo $row['phone'];
}

?>"></td>
</tr>
<tr>
<td>Image</td>
<td><input type="file" name="image" value="<?php 
if(isset($_GET['eid']))
{
echo $row['image'];
}

?>"></td>
</tr>
<tr>
<td>Submit</td>
<td colspan="2" align="center"><input type="submit" name="signup" value="signup"></td>
</tr>
</table>
</form>
<?php 
if(isset($_POST['signup'])){
if(isset($_POST['id']))
{
$fname=$_POST['fname'];
$email=$_POST['email'];
$phone=$_POST['phone'];
$image=$_POST['image'];
$id=$_POST['id'];

if($fname==""){
echo " First Name Is Required..";
}
elseif($email==""){
echo "Email is Required..";
}
elseif($phone==""){
echo "Plzz type Valid Phone..";
}
elseif($image==""){
echo "Image is Required..";
}
else{
if(isset($_POST['id']))
{
$id=$_POST['id'];
$query="update crud set fname='$fname',email='$email',phone='$phone',image='$image' where id='$id'";

if(mysqli_query($conn,$query))
{
echo "Record is Updated..";
}
else
{
echo "Record is Not Updated..";
}
}
}
}
}

?>
</body>
</html>


EmoticonEmoticon