PHP Update Data in MySQL


Update Data In a MySQL Table Using MySQLi Object-oriented

The UPDATE statement is used to update existing records in a table:


UPDATE table_name SET column1=value, column2=value2,... WHERE some_column=some_value




WHERE clause in the UPDATE syntax:The WHERE clause specifies which record or records that should be updated. If you omit the WHERE clause, all records will be updated!

"MyGuests" table:

ID First Name Last Name Email ID Reg_Date

The following examples update the record with id=1 in the "fa_MyGuests" table:

Example (MySQLi Object-oriented)

<?php

include("mysqli_oop_connection.php");

$sql = "UPDATE fa_MyGuests SET firstname='Bhumi', lastname='Parmar', email='bhumi123@gmail.com' WHERE id=1";

if ($conn->query($sql)) {
echo "<script> alert('Record Updated Successfully')</script>";
} else
{
echo "&l;script> alert('Recorde not Updated.Please Try again.')</script>";
}

?>


mysqli_oop_connection.php
<?php
$servername = "localhost";
$username = "root";
$password = "";
$db="lipsphpstudent";
// Create connection
$conn = new mysqli($servername, $username, $password,$db);
// Check connection
if (!$conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
?>
mysqli_oop_update.php
<?php
include("mysqli_oop_connection.php");
if(isset($_POST['submit']))
{
$id=$_POST['id']; $fname=$_POST['fname'];
$lname=$_POST['lname'];
$email=$_POST['email'];
$sql = "UPDATE fa_MyGuests SET firstname='$fname', lastname='$lname', email='$email' WHERE id=$id";
if ($conn->query($sql)) {
echo "<script> alert('Record Updated Successfully')</script>";
echo "<script> window.location.href='mysqli_oop_update_live_example.php?';</script>";
} else { echo "<script> alert('Recorde not Updated.Please Try again.')</script>";
echo "<script> window.location.href='mysqli_oop_update_live_example.php';</script>";
} } }else{
?>
<?php include('header1.php'); ?>
<?php include("mysql_sidemenu.php"); ?>
<div class="container">
<div class="row">

<div class="col-sm-7">
<center><h3>Update Recorde in Database </h3></center>
<?php
if(isset($_GET['Edit'])){
$id=$_GET['Edit'];
$sql="select * from fa_MyGuests where id=$id";
$query=$conn->query($sql);
if(!empty($query)){
while($row=$query->fetch_assoc($query))
{
?>
<form method="post" action="mysql_update.php">
<input class="form-control" type="text" name="id" value="<?php echo $row['id'] ?>" required="" style="display: none;">
First Name:<input class="form-control" type="text" name="fname" value="<?php echo $row['firstname'] ?>" required="">
Last Name:<input class="form-control" type="text" name="lname" value="<?php echo $row['lastname'] ?>" required="">
Email ID:<input class="form-control" type="email" name="email" value="<?php echo $row['email'] ?>" required="">
$lt;input type="submit" name="submit" class="btn btn-primary" >
</form >
<?php
}
}
}
?> <br><br><br><br><br>
</div>
</div>
</div>
<?php include('footer.php');>
<?php } ?>
mysqli_oop_update_live_example.php
<?php
include("mysqli_oop_connection.php");
$sql="select * from fa_MyGuests ;
$query=$conn->query($sql);
if($query)
{
?>
<?php include('header1.php'); ?>
<?php include("mysql_sidemenu.php"); ?>
<div class="container">
<div class="row">
<div class="col-sm-12">
<div class="table-responsive">

<table class="table table-bordered">
<thead>
<tr>
<th>ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>Email ID</th>
<th>Reg_Date</th>
<th>Edit</th>
</tr>
</thead>
<tbody>
<?php
while($row=$query->fetch_assoc())
{
?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['firstname'];?></td>
<td><?php echo $row['lastname']; ?></td>
<td><?php echo $row['email']; ?></td>
<td><?php echo $row['reg_date']; ?></td>
<td><a href="mysqli_oop_update.php?Edit=<?php echo $row['id']; ?>"><i class="fa fa-edit"></i>Edit</a></td> </tr>
<?php
}
}?>
</tbody>
</table>
</div>
</div>
</div>
</div>
<?php include('footer.php');?>
</div>
</div>