First We have to fetch all data and display in table on user-list.php then from user-list we will update data.
Click here to understand flowNow we have to fetch single user data from database by click on edit button.
Recieve view_id on fetch_single.php (follow as in image below).
$dbhost ="localhost";
$dbuser ="root";
$dbpswd ="";
$dbname ="dbdemo";
$dbcon= mysqli_connect($dbhost,$dbuser,$dbpswd,$dbname);
if($dbcon){
//echo "Connected............!!!";
return $dbcon;
exit();
}else{
die("Connection failed ? ? ?". mysqli_error($dbcon));
}
mysqli_close($dbcon);
<?php include("dbcon.php"); //This is database connection file
$error='';
if(isset($_GET['edit_id'])){
$edit_id = $_GET['edit_id'];
$query = "SELECT * FROM users WHERE id ='$edit_id'";
$run = mysqli_query($dbcon,$query);
if(mysqli_num_rows($run) > 0){
$data = mysqli_fetch_assoc($run);
$edit_id = $data['id'];
$edit_fname = $data['fname'];
$edit_lname = $data['lname'];
$edit_username = $data['username'];
$edit_email = $data['email'];
}else{
$error = "No data found ";
}
}else{
$error = "No data found ";
}
mysqli_close($dbcon);
Update form with input value of single user data.
<?php include("fetch-single.php"); ?>
<?php include("update.php"); ?>
<?php include("common/header.php"); ?>
<body>
<div class="container-fluid" style="margin-top: 50px;">
<?php include("common/navbar.php"); ?>
<div class="text-center"> <?php echo $success; ?> <?php echo $error; ?> </div>
<div class="container w-50">
<form method="POST" action="update-form.php" class="border p-4" style="background: #f5f6fa;">
<h3 class="rounded bg-primary w-100 p-2 text-white text-center"> Update Data Form </h3>
<input type="hidden" value="<?php if(isset($edit_id)){ echo $edit_id; } ?>" name="edit_id">
<div class="form-group">
<label for="fname">First Name</label>
<input type="text" class="form-control" value="<?php if(isset($edit_fname)){ echo $edit_fname; } ?>" name="firstname" id="firstname" >
</div>
<div class="form-group">
<label for="lname">Last Name</label>
<input type="text" class="form-control" value="<?php if(isset($edit_lname)){ echo $edit_lname; } ?>" name="lastname" id="lastname">
</div>
<div class="form-group">
<label for="username">Username</label>
<input type="text" class="form-control" value="<?php if(isset($edit_username)){ echo $edit_username; } ?>" name="username" id="username">
</div>
<div class="form-group">
<label for="email">Email</label>
<input type="text" class="form-control" value="<?php if(isset($edit_email)){ echo $edit_email; } ?>" name="email" id="email">
</div>
<div class="form-group pb-2">
<input type="submit" name="update" value="Update" class="btn btn-success">
<a href="index.php" class="btn btn-danger float-right">Close <a>
</div>
</form>
</div>
</div>
<?php include("common/footer.php"); ?>
Output show in update form with input value of single data of user (update-form.php).
<?php include("dbcon.php"); //This is database connection file
$error='';
$success='';
if(isset($_POST['update'])){
$edit_id = mysqli_real_escape_string($dbcon,ucfirst($_POST['edit_id']));
$fname = mysqli_real_escape_string($dbcon,ucfirst($_POST['firstname']));
$lname = mysqli_real_escape_string($dbcon,ucfirst($_POST['lastname']));
$uname = mysqli_real_escape_string($dbcon,strtolower($_POST['username']));
$username = preg_replace('/\s+/','',$uname); //to remove space
$email = mysqli_real_escape_string($dbcon,strtolower($_POST['email']));
if(empty($fname)){
$error="<div class='alert alert-danger'>Please enter first name ! Required !!</div>";
}else if(empty($lname)){
$error="<div class='alert alert-danger'>Please enter last name ! Required !!</div>";
}else if (!preg_match("/^[a-zA-Z\\s]+$/", $fname)) {
$error="<div class='alert alert-danger'>Numbers are not allowedd !!</div>";
}else if (!preg_match("/^[a-zA-Z\\s]+$/", $lname)) {
$error="<div class='alert alert-danger'>Numbers are not allowed !!</div>";
}else if(empty($uname)){
$error="<div class='float-right'>Please enter username ! Required !!</div>";
}else if (strlen($uname) < 4 ) {
$error="<div class='alert alert-danger'>The Username must be at least 4 characters !!</div>";
}else if (!preg_match("/^[a-zA-Z0-9_-]+/i", $username)) {
$error="<div class='alert alert-danger'>The characters or symbols are not allowd !!</div>";
}else if (empty($email)) {
$error="<div class='alert alert-danger'>Please enter email address ! Required !!</div>";
}else if(!filter_var($email, FILTER_VALIDATE_EMAIL)){
$error="<div class='alert alert-danger'>The E-mail address is invalid !!</div>";
}else{
$sql = "UPDATE users SET fname='$fname', lname='$lname', username='$uname', email='$email' WHERE id=$edit_id";
if(mysqli_query($dbcon,$sql)){
$success ="<div class='alert alert-success'>User has been updated !! </div>";
}else{
$error = "<div class='alert alert-danger w-100'>User has not been updated !</div>";
}
}
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1,
shrink-to-fit=no">
<title>Codetechinfo</title>
<link rel="stylesheet" href="css/bootstrap.min.css">
</head>
<nav class="navbar navbar-expand-lg navbar-light bg-primary text-white fixed-top">
<a class="navbar-brand text-white" href="./index.php">Learn PHP </a>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav">
<li class="nav-item active">
<a class="nav-link text-white" href="./index.php">Home </a>
</li>
</ul>
</div>
</nav>
<br><br>
<div class="copyright py-4 text-center text-white">
<div class="container "><small>© Copyright 2025 by Saten Chauhan</small> </div>
</div>
</body>
</html>