Edit/Update data into MySQL using PHP.

  • Post author:
  • Post category:PHP
  • Post comments:0 Comments
  • Post last modified:October 5, 2020
  • Reading time:7 min(s) read

In this tutorial, we’ll learn, how to edit/Update data into MySQL using PHP. In the previous tutorial, we learn how to insert data into the database using PHP And How to fetch/select data from a database using PHP and MySQL.

Now, it’s time to learn how to Edit/Update data into MySQL using PHP.

The SQL “UPDATE” statement is used to modify the existing data in a MySQL table. First of all, we have to create a (.) dot PHP file i.e, edit.php. After that, inside the view page to link up with edit.php the edit button should be clicked and the row id is fetched from the MySQL table.

<a href="edit.php?edit=<?php echo $row['id'];?>" class="btn btn-success">Edit</a>
Edit/Update data into MySQL using PHP.

Now, Inside the edit.php file, the bootstrap 4 code that is mentioned below should be written

<!DOCTYPE html>
<html>
<head>
<title>Data Insert Into Database Using PHP</title>
	<!---------------------------------MaxCDN----------------------------------------------->
	<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css">
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<!-- Popper JS -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<div class="row">
<div class="card">
<div class="card-header">
<h4>DATA UPDATE INTO DATABSE USING PHP</h4>
</div>
<div class="card-body">
<form action="<?php $_PHP_SELF?>" method="post">
<div class="row">
<div class="col-md-12">
<div class="form-group">
<label>FULL NAME <span style="color: red;">*</span></label>
<input type="text" name="fullname" class="form-control" required="" >
</div>
</div>
<div class="col-md-12">
<div class="form-group">
<label>EMAIL <span style="color: red;">*</span></label>
<input type="email" name="email" class="form-control" required="" >
</div>
</div>
<div class="col-md-12">
<div class="form-group">
<label>PHONE<span style="color: red;">*</span></label>
<input type="text" name="phone" class="form-control" required="" >
</div>
</div>
<input type="hidden" name="id" value="">
<div class="col-md-12">
<div class="text-center">
<input type="submit" name="submit" class="btn btn-primary">
</div>
</div>					
</div>
</form>
</div>
</div>
</div>
</div>
</body>
</html>	
Edit/Update data into MySQL using PHP

To Select/View a record in any table it is required to locate that record by using a conditional(WHERE) clause. The below example uses the primary key to match a record in the MySQL table.

<?php
if (isset($_GET['edit'])) {
$id=$_GET['edit'];
$sel="SELECT * FROM test WHERE id=$id";
$query=$con->query($sel);
while ($row=$query->fetch_assoc()) {
			}
	}
?>
<!DOCTYPE html>
<html>
<head>
<title>Data Insert Into Database Using PHP</title>
	<!---------------------------------MaxCDN----------------------------------------------->
	<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css">
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<!-- Popper JS -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<div class="row">
<div class="card">
<div class="card-header">
<h4>DATA UPDATE INTO DATABSE USING PHP</h4>
</div>
<div class="card-body">
<form action="<?php $_PHP_SELF?>" method="post">
<div class="row">
<?php
if (isset($_GET['edit'])) {
$id=$_GET['edit'];
$sel="SELECT * FROM test WHERE id=$id";
$query=$con->query($sel);
while ($row=$query->fetch_assoc()) {			
?>
<div class="col-md-12">
<div class="form-group">
<label>FULL NAME <span style="color: red;">*</span></label>
<input type="text" name="fullname" class="form-control" required="" value="<?php echo $row['fullname'];?>">
</div>
</div>
<div class="col-md-12">
<div class="form-group">
<label>EMAIL <span style="color: red;">*</span></label>
<input type="email" name="email" class="form-control" required="" value="<?php echo $row['email'];?>">
</div>
</div>
<div class="col-md-12">
<div class="form-group">
<label>PHONE<span style="color: red;">*</span></label>
<input type="text" name="phone" class="form-control" required="" value="<?php echo $row['phone'];?>">
</div>
</div>
<input type="hidden" name="id" value="<?php echo $row['id'];?>">
<div class="col-md-12">
<div class="text-center">
<input type="submit" name="submit" class="btn btn-primary">
</div>
</div>
<?php
 }
}
?>
</div>
</form>
</div>
</div>
</div>
</div>
</body>
</html>
Edit/Update data into MySQL using PHP

After that, the ‘UPDATE’ statement is used with WHERE CLUSE  to modify/change those record that matches specific criteria. Here is the updated code-

<?php
	$con=mysqli_connect("localhost","root","","php");
	/////now update new data /////
	//////update code start here ///////////////
		if (isset($_POST['submit'])) {
			$fullname=$_POST['fullname'];
			$email=$_POST['email'];
			$phone=$_POST['phone'];
			$id=$_POST['id'];


$update="UPDATE test SET fullname='$fullname',email='$email',phone='$phone' WHERE id=$id ";
			$up=$con->query($update);
			if ($up) {
				header('location:view.php');
			}else{
				echo "SOMETHING WENT WRONG";
			}


		}


?>
Edit/Update data into MySQL using PHP

Below is the complete code for update data into the database using PHP, MySQL, Bootstrap 4.

<?php
$con=mysqli_connect("localhost","root","","php");
	/////now update new data /////
	//////update code start here ///////////////
if (isset($_POST['submit'])) {
$fullname=$_POST['fullname'];
$email=$_POST['email'];
$phone=$_POST['phone'];
$id=$_POST['id'];


$update="UPDATE test SET fullname='$fullname',email='$email',phone='$phone' WHERE id=$id ";
$up=$con->query($update);
if ($up) {
header('location:view.php');
}else{
echo "SOMETHING WENT WRONG";
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Data Insert Into Database Using PHP</title>
	<!---------------------------------MaxCDN----------------------------------------------->
	<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css">

<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>

<!-- Popper JS -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>

<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<div class="row">
<div class="card">
<div class="card-header">
<h4>DATA UPDATE INTO DATABSE USING PHP</h4>
</div>
<div class="card-body">
<form action="<?php $_PHP_SELF?>" method="post">
<div class="row">
<?php
if (isset($_GET['edit'])) {
	$id=$_GET['edit'];
	$sel="SELECT * FROM test WHERE id=$id";
	$query=$con->query($sel);
	while ($row=$query->fetch_assoc()) {
										
?>
<div class="col-md-12">
<div class="form-group">
<label>FULL NAME <span style="color: red;">*</span></label>
<input type="text" name="fullname" class="form-control" required="" value="<?php echo $row['fullname'];?>">
</div>
</div>
<div class="col-md-12">
<div class="form-group">
<label>EMAIL <span style="color: red;">*</span></label>
<input type="email" name="email" class="form-control" required="" value="<?php echo $row['email'];?>">
</div>
</div>
<div class="col-md-12">
<div class="form-group">
<label>PHONE<span style="color: red;">*</span></label>
<input type="text" name="phone" class="form-control" required="" value="<?php echo $row['phone'];?>">
</div>
</div>
<input type="hidden" name="id" value="<?php echo $row['id'];?>">
<div class="col-md-12">
<div class="text-center">
<input type="submit" name="submit" class="btn btn-primary">
</div>
</div>
<?php
		}
	}
?>
</div>
</form>
</div>
</div>
</div>
</div>
</body>
</html>	
Edit/Update data into MySQL using PHP

Leave a Reply