How to insert view update and delete data using PHP and MySQL step by step with Graphics

 How to insert view update and delete data using PHP and MySQL step by step with Graphics

Step 1 :
create table in mySQl using query

CREATE TABLE `users` (
  `userid` text NOT NULL,
  `name` text DEFAULT NULL,
  `password` text DEFAULT NULL,
  `email` text DEFAULT NULL,
  `mobile` text DEFAULT NULL,
  `address` text DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

step 2:
create a configuration file to connect mysql database with the php 
<?php

session_start();
$con = mysqli_connect('localhost','root');
mysqli_select_db($con,'fyp');
if(mysqli_connect_error())
{
    die("DB Connection Failed");
}

?>
Step 3:Create from for insert operation name as AddUsers.php and paste the code 
<?php include 'config.php';?>
<?php 
if(isset($_POST['done']))
{
if(!empty($_POST)) 
{
    if(!empty($_POST['userid'])&& !empty($_POST['password']))
{
          $userid = $_POST['userid'];
            $name = $_POST['name'];
$password = $_POST['password'];
          $email = $_POST['email'];
           
          $mobile = $_POST['mobile'];
            $address = $_POST['address'];
$q1="INSERT INTO `users`(`userid`, `name`, `password`, `email`, `mobile`, `address`) VALUES ('$userid', '$name', '$password', '$email', '$mobile', '$address')";
$query = mysqli_query($con,$q1);
  echo 'User is added successfully';
}
else
{
echo "Please enter user name and password";
}
}
else{
echo "Please fill the form first";
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    
   <link rel="stylesheet" href="styles.css">
    <title>PHP CRUD Operation</title>
</head>
<body>
<h1> Add Users </h1>
<a href="index.php">Back</a>
<br>
<br>
<div >

                <form method="POST" action="AddUsers.php">
<table border=1>
<tr><td>Enter User ID: </td><td><input type="text" name="userid"></td></tr>
<tr><td>Enter  User Name:</td><td><input type="text" name="name" ></td></tr>
  

<tr><td>Enter  Password:</td><td><input type="password" name="password" ></td></tr>
<tr><td>Enter  Email:</td><td><input type="text" name="email" ></td></tr>
 
<tr><td>Enter  Mobile Number:</td><td><input type="text" name="mobile" ></td></tr>
 
 <tr><td>Enter  address:</td><td><input type="text" name="address" ></td></tr>
 
 
                    <tr><td></td><td><button type="submit" name="done">Submit</button></td></tr>
</table>
            
</form>
            </div>
        </div>
    </main>
</div>
</body>
</html>
Step 4: EditUsers.php 
//editUsers.php
<?php include 'config.php'; 
 
if(isset($_POST["submit"]))
{
//`userid`, `name`, `password`, `email`, `mobile`, `address`
$userid= $_GET['id'];
$name= $_POST['name'];
$password= $_POST['password'];
$email=$_POST['email'];
$mobile= $_POST['mobile'];
$address=$_POST['address'];
$insert = $con->query("UPDATE `users` SET `name`='$name',`password`='$password',`email`='$email',`mobile`='$mobile',`address`='$address' where `userid`='$userid'"); 
             if($insert){ 
                $status = 'success'; 
                echo $statusMsg = "User is updates successfully."; 
            }else{ 
               echo $statusMsg = "Failed, please try again."; 
            }  

}
$userid= $_GET['id'];
//`userid`, `name`, `password`, `email`, `mobile`, `address`
$result = $con->query("SELECT * FROM users where userid='$userid'"); 
if($result->num_rows > 0)
{
while($row = $result->fetch_assoc())
{
$userid= $row['userid'];
$name= $row['name'];
$password= $row['password'];
$email=$row['email'];
$mobile= $row['mobile'];
$address=$row['address'];
}
}
?>
<html>
<head></head>
<title> Edit User</title>
<body>
<h1>
Edit User
</h1>
<br>
<form action="editUsers.php?id=<?php echo $userid ?>" method="post">
<table>
<tr><td>
<a href="index.php">Home</a></td><td> </td></tr>
<tr><td>User ID</td><td><?php echo $userid ?></td></tr>
<tr><td>Name</td><td><input type="Text" name="name" value="<?php echo $name ?>"></td></tr>
<tr><td>password</td><td><input type="password" name="password" value="<?php echo $password ?>"></td></tr>
<tr><td>Email</td><td><input type="Text" name="email" value="<?php echo $email ?>"></td></tr>
<tr><td>Mobile Number</td><td><input type="Number" name="mobile" value="<?php echo $mobile ?>"></td></tr>
<tr><td>Address</td><td><input type="Text" name="address" value="<?php echo $address ?>"></td></tr>
<tr><td></td><td><input type="submit" name="submit" value="Save Changes"></td>
</tr>
</table>
</form>
</body>
</html>
   

Step 5: deleteUsers.php

<?php include 'config.php'; 
 $userid= $_GET['id'];
$statusMsg ="";

$insert = $con->query("Delete from `users` where `userid`='$userid'"); 
             if($insert){ 
               
                 $statusMsg = "User is deleted successfully."; 
            }else{ 
                $statusMsg = "Failed, please try again."; 
            }  
?>

<html>
<head></head>
<title> Delete User</title>
<body>
<h1>Delete User</h1>
<a href="index.php">Back</a>
<h3>
<?php echo $statusMsg ?>

</h3>
<br>
</body>
</html>

Step6: Combine all pages togeather in index.php

//index.php

<?php include 'config.php'; 
 $result = $con->query("SELECT * FROM users"); 
?>
<html>
<head></head>
<title> PHP CRUD Operation</title>
<body>
    <div >
<h1> Insert Select  Update and Delete Operation using PHP 2022 !</h1>
<br>
<a href="index.php">Home</a>| <a href="AddUsers.php">Add Users</a>|
<br>
<br>
<br>
<?php if($result->num_rows > 0){ 
         while($row = $result->fetch_assoc()){ ?> 
<br>
<table border=1>
<tr>
<td>User ID</td><td> <?php echo $row['userid']?></td>
</tr>
<tr>
<td>Name</td><td> <?php echo $row['name']?> </td>
</tr>
<tr>
<td>Password</td><td><?php echo $row['password']?> </td>
</tr>
<tr>
<td>Email</td><td><?php echo $row['email']?> </td>
</tr>
<tr>
<td>Mobile</td><td><?php echo $row['mobile']?> </td>
</tr>
<tr>
<td>Address</td><td> <?php echo $row['address']?> </td>
</tr>
<tr>
<td> <?php echo '<a href="editUsers.php?id=' . $row['userid'] . '">Edit Details</a>';?></td>
<td> <?php echo '<a href="deleteUsers.php?id=' . $row['userid'] . '">Delete Details</a>';?> </td>
</tr>
  </table>
   </div>

   <?php
   } ?> 
    
<?php }else{ ?> 
    <p class="status error">User not found...</p> 
<?php } ?>
</body>
</html>
   
By Hafiz Muhammad Umar Hayat
   

Comments