How To Use SQL Update Statement In Query Using PHP

admin_img Posted By Bajarangi soft , Posted On 18-09-2020

SQL UPDATE statement is used to modify the existing records in a table.We can update single columns as well as multiple columns using UPDATE statement as per our requirement.SQL UPDATE Query is used to modify the existing records in a table.we can use the WHERE clause with the UPDATE query to update the selected rows, otherwise all the rows would be affected.

sql_insert

The SQL UPDATE Statement

The UPDATE statement is used to modify the existing data in a table.We can update single columns as well as multiple columns using UPDATE statement as per our requirement.SQL UPDATE Query is used to modify the existing records in a table.we can use the WHERE clause with the UPDATE query to update the selected rows, otherwise all the rows would be affected.


UPDATE Syntax and Usage:

UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition

Updating  table by setting column values by the where condition.

Let's learn


1.Create database and table.
student table.
StudentID StudentName ParentsContact Address Country DOB Section

Branch


  1
    Liam 10101010 23 MG road
Banglore  
INDIA 12/06 A CSE 
  2    Andrew 20202020 2415 Anthony  market str Germany 01/02 A ISE 
  3    Joshua 30303030 26547 David church road USA 18/04 B CL 
 4     Ryan 40404040 18 Krishna sq.
Banglore
INDIA 26/12 B EE
 5    Charles 50505050 878 Eli Berguvsvägen 8 INDIA 23/11 C ECE

UPDATE  Example:
$sql = "UPDATE Student SET StudentName='Jeo' WHERE Country='USA'";

SQL statement will update the StudentName to "Jeo" for all records where country is "USA"
when we have country name as USA for that specified country it will changes studentname.
or you want to update only of one row or one syudent then you need to give unique Countryname or ID.


complete code of SQL UPDATE statement in PHP script
<!DOCTYPE html>
<html lang="en">
<head>
    <title>How to use   SQL UPDATE Statement</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
</head>
<body>
<div class="container">
    <br>
    <br>
    <br>
    <div class="text-center">
        <h1>SQL UPDATE Statement</h1>
    </div>
    <div class="well">
        <?php
        $connect = mysqli_connect("localhost", "root", "", "school_management");
        if($connect === false){
            die("ERROR: Could not connect. " . mysqli_connect_error());
        }

        $sql = "UPDATE Student SET StudentName='Juan' WHERE Country='USA'";
        if(mysqli_query($connect, $sql)){
            echo "<h1>Update Records successfully.</h1>";
        } else{
            echo "ERROR: Could not able to execute $sql. " . mysqli_error($connect);
        }
        mysqli_close($connect);
        ?>
    </div>
    <br>
</div>
</body>
</html>



 

Related Post