ขั้นตอนที่ 1 สร้างไฟล์ใหม่ชื่อ fetch_data_by_id.php
ให้สร้างไฟล์ใหม่ชื่อ fetch_data_by_id.php แล้วพิมพ์โค้ดคำสั่งดังนี้
<?php
include 'connect_db.php';
$query="select * from customers where custId='". $_GET['id']. "'"; // Fetch all the data from the table customers
$result=mysqli_query($conn,$query);
$row = mysqli_fetch_assoc($result);
?>
ขั้นตอนที่ 2 แก้ไขไฟล์ index.php
- แก้ไขไฟล์ index.php ให้เป็นดังนี้
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>How to Insert Form Data In Database using PHP</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<div class="row mt-5">
<div class="col-sm mx-1 border border-secondary p-2">
<div class="text-primary">
<h3>บันทึกข้อมูลลงในฐานข้อมูลด้วยภาษา php</h3>
</div>
<form action="insert.php" method="post">
<div class="form-group">
<label>ชื่อ</label>
<input type="text" name="fname" class="form-control">
</div>
<div class="form-group">
<label>นามสกุล</label>
<input type="text" name="lname" class="form-control">
</div>
<div class="form-group">
<label>E-mail</label>
<input type="email" name="email" class="form-control">
</div>
<input type="submit" class="btn btn-primary" name="submit" value="save">
</form>
</div>
<div class="col-sm mx-1 border border-secondary p-4">
<div class="page-header">
<h2>รายชื่อลูกค้าที่มีอยู่ในฐานข้อมูล</h2>
</div>
<table class="table">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">First</th>
<th scope="col">Last</th>
<!-- <th scope="col">Email</th> -->
<th scope="col">Action</th>
</tr>
</thead>
<tbody>
<?php include 'fetch_data.php'; ?>
<?php if ($result->num_rows > 0): ?>
<?php while($array=mysqli_fetch_row($result)): ?>
<tr>
<th scope="row"><?php echo $array[0];?></th>
<td><?php echo $array[1];?></td>
<td><?php echo $array[2];?></td>
<!-- <td><?php echo $array[3];?></td> -->
<td>
<a href="<?php echo "edit.php?id=".$array[0] ?>" class="btn btn-info">เรียกดู</a>
</td>
</tr>
<?php endwhile; ?>
<?php else: ?>
<tr>
<td colspan="3" rowspan="1" headers="">ไม่พบข้อมูล</td>
</tr>
<?php endif; ?>
<?php mysqli_free_result($result); ?>
</tbody>
</table>
</div>
</div>
</div>
</body>
</html>
จะได้หน้าตาของเว็บประมาณนี้

ขั้นตอนที่ 3 สร้างไฟล์ ใหม่ชื่อ view.php
- พิมพ์โค้ดังนี้
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>ข้อมูลลูกค้า</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<div class="row mt-5 justify-content-center">
<div class="col-6 mx-1 border border-secondary p-2">
<div class="text-primary">
<h3>ข้อมูลลูกค้า</h3>
</div>
<?php include 'fetch_data_by_id.php'; ?>
<form action="update.php" method="post" class="form-group">
<div class="form-group">
<label>ชื่อ</label>
<input disabled type="text" name="fname" class="form-control" value="<?php echo $row['fname'] ?>">
</div>
<div class="form-group">
<label>นามสกุล</label>
<input disabled type="text" name="lname" class="form-control" value="<?php echo $row['lname'] ?>">
</div>
<div class="form-group">
<label>E-mail</label>
<input disabled type="email" name="email" class="form-control" value="<?php echo $row['email'] ?>">
</div>
<div class="form-group d-flex">
<a href="index.php" class="btn btn-secondary">ย้อนกลับ</a>
</div>
</form>
</div>
</div>
</div>
</body>
</html>
ลองรันโปรแกรม จะได้ผลลัพธ์ดังนี้

