We can update record into database using php script of existing record.

We will consider our old tables which been already use during INSERT operation in php in our tutorials.

Syntax

UPDATE table_name SET column_name1=’value’, column_name2=’value’ WHERE condition;

  • Let’s update the record of customer’s name,location on the basics of their phone number using PHP script.

Files – file1.html

<!DOCTYPE html>
<html lang=”en”>
<head>
    <meta charset=”UTF-8″>
    <meta http-equiv=”X-UA-Compatible” content=”IE=edge”>
    <meta name=”viewport” content=”width=device-width, initial-scale=1.0″>
    <title>file 1</title>
</head>
<body>
     <font style=”font-size: 20px;”>Enter Customer’s Name, Location for update</font>
    <br> <br>
    <form action=”file1.php” method=”post”>
        <font style=”font-size: 20px;”>Customer Name</font>
        <br>
        <input type=”text” name=”name” required>
        <br>
        <br>
        <font style=”font-size: 20px;”>Customer Phone</font>
        <br>
        <input type=”text” name=”phone_no” required>
        <br> <br>
        <font style=”font-size: 20px;”>Location</font>
        <br>
        <input type=”text” name=”loc” required>
        <br>
        <br>
        <input type=”submit” value=”submit”>
    </form>
</body>
</html>
output

File – file1.php

<?php
error_reporting(0);
// connection with database
$conn = mysqli_connect(‘localhost’,’root’,”,’microcodes’);
if($conn){
   // echo “connection established!”;
}
else{
    echo “connection error!”;
}
 
  $name =  $_POST[‘name’];
 $phone = $_POST[‘phone_no’];
 $loc =  $_POST[‘loc’];
 
 $sql=”UPDATE customers SET custname=’$name’,loc=’$loc’ WHERE phone=’$phone'”;
$query=mysqli_query($conn,$sql);
 
if($query){
    echo “customer record updated!!”;
    echo “<br>”;
    echo “<br> Customer name: $name”;
    echo “<br> Customer phone: $phone”;
    echo “<br> Customer location: $loc”;
}
else{
    echo “customer record not updated!!”;
}
?>
output

File – phpmyadmin before

File – phpmyadmin after

  • Let’s make a PHP code, where we update person name, date of birth on the basics of email & password. first authenticate their emailid and password is valid or not then update them.

File – file2.html

<!DOCTYPE html>
<html lang=”en”>
<head>
    <meta charset=”UTF-8″>
    <meta http-equiv=”X-UA-Compatible” content=”IE=edge”>
    <meta name=”viewport” content=”width=device-width, initial-scale=1.0″>
    <title>file 2 – update</title>
</head>
<body>
    <br>
    <font style=”font-size: 20px;”>Enter details for Update</font>
<br>
<br>
    <form action=”file2.php” method=”post”>
        <font style=”font-size: 20px;”>Name(will be updated)</font>
        <br>
        <input type=”text” name=”fname”>
        <br><br>
        <font style=”font-size: 20px;”>Date of Birth(will be updated)</font>
        <br>
        <input type=”date” name=”dob”>
        <br><br>
        <font style=”font-size: 20px;”>Email(required)</font>
        <br>
        <input type=”text” name=”emailid”>
        <br><br>
        <font style=”font-size: 20px;”>Your Password(required)</font>
        <br>
        <input type=”text” name=”passd”>
        <br><br>
        <input type=”submit” value=”update”>
    </form>
</body>
</html>
output

File – file2.php

<?php
error_reporting(0);
 
// connection with database
$conn = mysqli_connect(‘localhost’,’root’,”,’microcodes’);
if($conn){
   // echo “connection established!”;
}
else{
    echo “connection error!”;
}
 
 
$fname  =  $_POST[‘fname’];
$dob =   $_POST[‘dob’];
$dob = date(“d/m/y”, strtotime($dob));
$email =  $_POST[’emailid’];
$pass  =  $_POST[‘passd’];
 
$sql=”UPDATE registers SET fname=’$fname’,dob=’$dob’ WHERE emailid=’$email’ && passd=’$pass'”;
$query=mysqli_query($conn,$sql);
 
if($query){
    echo “record updated!!”;
    echo “<br> name is: $fname”;
    echo “<br> date of birth: $dob”;
    echo “<br> email is: $email”;
}
else{
    echo “record not updated!!”;
}
?>
output

File – phpmyadmin before

File – phpmyadmin after