How to Update Data in MySQL Database in PHP
Update data into database in php mysql
In this tutorial, you will be learning how to Edit & Update data into mysql database in php, where to update the record, Update Statement will be used.
So guys, we will be complete this task in 3 Steps as follows:
Step 1: Database Connection in a file dbconfig.php :
<?php
$host = "localhost";
$username = "your_username";
$password = "your_password";
$database = "your_database_name";
// Create DB Connection
$conn = mysqli_connect($host, $username, $password, $database);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
?>
Step 2: Create a file index.php and fetch data in html table and create a edit button in it as follows:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Fetch Data in PHP MySQL</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" />
</head>
<body>
<div class="container mt-5">
<div class="row justify-content-center">
<div class="col-md-6">
<div class="card shadow">
<div class="card-header">
<h4>Fetch data from database in PHP MySQL</h4>
</div>
<div class="card-body">
<table class="table table-bordered table-striped">
<thead>
<tr>
<th>ID</th>
<th>Full Name</th>
<th>Course</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<?php
include('dbconfig.php');
$query = "SELECT * FROM students";
$query_run = mysqli_query($conn, $query);
if(mysqli_num_rows($query_run) > 0) //Atleast 1 record is there or not
{
foreach($query_run as $row)
{
?>
<tr>
<td><?= $row['id'] ?></td>
<td><?= $row['fullname'] ?></td>
<td><?= $row['course'] ?></td>
<td><?= $row['email'] ?></td>
<td>
<a href="student-edit.php?id=<?=$row['id']?>" class="btn btn-success">Edit</a>
</td>
</tr>
<?php
}
}
else
{
?>
<tr>
<td colspan="4">No Record Found</td>
</tr>
<?php
}
?>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
Step 3: Create a file student-edit.php to get the data in the Form and Update data in php as given below:
You will have a URL as: http://localhost/php/student-edit.php?id=1
<?php
include_once 'dbconfig.php';
if(isset($_POST['update_buttton']))
{
$student_id = $_POST['student_id'];
$fullname = $_POST['fullname'];
$course = $_POST['course'];
$email = $_POST['email'];
$query = " UPDATE students SET fullname='$fullname', course='$course', email='$email' WHERE id='$student_id' ";
$result = mysqli_query($conn, $query);
if($result)
{
$message = "Data Updated Successfully!";
}
else
{
$message = "Data Not Updated!. Error: " . $sql . "" . mysqli_error($conn);
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Edit & Update data into database in PHP MySQL</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" />
</head>
<body>
<div class="container mt-5">
<?php if(isset($message)) { echo "$message"; } ?>
<div class="row justify-content-center">
<div class="col-md-6">
<div class="card shadow">
<div class="card-header">
<h4>Edit & Update data into database in PHP MySQL</h4>
</div>
<div class="card-body">
<?php
if(isset($_GET['id']))
{
$student_id = $_GET['id'];
$query = " SELECT * FROM students WHERE id='$student_id' LIMIT 1";
$result = mysqli_query($conn, $query);
if(mysqli_num_rows($result) > 0)
{
$row = mysqli_fetch_array($result);
?>
<form action="" method="POST">
<div class="mb-3">
<label>Full Name</label>
<input type="text" name="fullname" value="<?=$row['fullname'];?>" class="form-control" required>
</div>
<div class="mb-3">
<label>Course</label>
<input type="text" name="course" value="<?=$row['course'];?>" class="form-control" required>
</div>
<div class="mb-3">
<label>Email ID</label>
<input type="email" name="email" value="<?=$row['email'];?>" class="form-control" required>
</div>
<div class="mb-3">
<hr/>
<button type="submit" name="update_buttton" class="btn btn-primary">Update Data</button>
</div>
</form>
<?php
}
else
{
echo "<h4>No Record Found</h4>";
}
}
else
{
echo "<h4>No ID Found</h4>";
}
?>
</div>
</div>
</div>
</div>
</div>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
Thanks for reading.