How to Edit & Update data using bindParam function in php pdo
How to Edit & Update data using bindParam in php pdo
In this post, you will be learning about how to edit data by fetching data by id using "bindParam()" function and update data using "bindParam()" in PHP PDO.
Step 1: Create Database Connection.
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$database = "database_name";
try {
$conn = new PDO("mysql:host=$servername;dbname=$database", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // set the PDO error mode to exception
// echo "Connected Successfully";
} catch(PDOException $e) {
echo "Connection Failed" .$e->getMessage();
}
?>
Step 2: Add the edit button in your html table where you have fetch data:
<td>
<a href="student-edit.php?id=<?=$row['id']?>" class="btn btn-info">Edit</a>
</td>
Step 3: Create a file named "student-edit.php" and paste the below code:
We will retrieve data with the help of parameter which we created as id with send value on edit button.
<?php
include('dbcon.php');
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
<title>PHP PDO using bindParam() function CRUD</title>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-12 mt-4">
<div class="card">
<div class="card-header">
<h4> Edit & Update Data using PDO with bindParam() in PHP
<a href="index.php" class="btn btn-primary float-end">BACK</a>
</h4>
</div>
<div class="card-body">
<?php
if(isset($_GET['id']))
{
$student_id = $_GET['id'];
$query = "SELECT * FROM students WHERE id=? LIMIT 1";
$statement = $conn->prepare($query);
$statement->bindParam(1, $student_id, PDO::PARAM_INT);
$statement->execute();
$data = $statement->fetch(PDO::FETCH_ASSOC);
?>
<form action="code.php" method="POST">
<input type="hidden" name="student_id" value="<?=$data['id'];?>">
<div class="mb-3">
<label>Full Name</label>
<input type="text" name="fullname" value="<?=$data['fullname'];?>" class="form-control">
</div>
<div class="mb-3">
<label>Email Id</label>
<input type="email" name="email" value="<?=$data['email'];?>" class="form-control">
</div>
<div class="mb-3">
<label>Phone Number</label>
<input type="text" name="phone" value="<?=$data['phone'];?>" class="form-control">
</div>
<div class="mb-3">
<label>Course</label>
<input type="text" name="course" value="<?=$data['course'];?>" class="form-control">
</div>
<div class="mb-3">
<button type="submit" name="update_student" class="btn btn-primary">Update Student</button>
</div>
</form>
<?php
}
else
{
echo "<h5>No ID Found</h5>";
}
?>
</div>
</div>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
Step 4: Create a code.php file and paste the below code:
<?php
session_start();
include('dbcon.php');
if(isset($_POST['update_student']))
{
$student_id = $_POST['student_id'];
$fullname = $_POST['fullname'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$course = $_POST['course'];
try {
$query = "UPDATE students SET fullname=:fullname, email=:emailid, phone=:phoneno, course=:course WHERE id=:stud_id LIMIT 1";
$statement = $conn->prepare($query);
$statement->bindParam(':fullname', $fullname);
$statement->bindParam(':emailid', $email);
$statement->bindParam(':phoneno', $phone);
$statement->bindParam(':course', $course);
$statement->bindParam(':stud_id', $student_id, PDO::PARAM_INT);
$query_execute = $statement->execute();
if($query_execute)
{
$_SESSION['message'] = "Updated Successfully";
header('Location: index.php');
exit(0);
}
else
{
$_SESSION['message'] = "Not Updated";
header('Location: index.php');
exit(0);
}
} catch(PDOException $e) {
echo $e->getMessage();
}
}
?>
Thank you.