PHP | MySQL UPDATE Query

The MySQL UPDATE query is used to update existing records in a table in a MySQL database.

  • It can be used to update one or more field at the same time.
  • It can be used to specify any condition using the WHERE clause.


  • Syntax :
    The basic syntax of the Update Query is –

    Implementation of Where Update Query :

    Let us consider the following table “Data” with four columns ‘ID’, ‘FirstName’, ‘LastName’ and ‘Age’.



    To update the “Age” of a person whose “ID” is 201 in the “Data” table, we can use the following code :

    Update Query using Procedural Method :

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <?php
    $link = mysqli_connect("localhost", "root", "", "Mydb");
      
    if($link === false){
        die("ERROR: Could not connect. " 
                    . mysqli_connect_error());
    }
      
    $sql = "UPDATE data SET Age='28' WHERE id=201";
    if(mysqli_query($link, $sql)){
        echo "Record was updated successfully.";
    } else {
        echo "ERROR: Could not able to execute $sql. " 
                                . mysqli_error($link);
    mysqli_close($link);
    ?>

    chevron_right

    
    

    Output :
    Table After Updation –

    The output on Web Browser :

    Update Query using Object Oriented Method :

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <?php
    $mysqli = new mysqli("localhost", "root", "", "Mydb");
      
    if($mysqli === false){
        die("ERROR: Could not connect. "
                . $mysqli->connect_error);
    }
      
    $sql = "UPDATE data SET Age='28' WHERE id=201";
    if($mysqli->query($sql) === true){
        echo "Records was updated successfully.";
    } else{
        echo "ERROR: Could not able to execute $sql. " 
                                            . $mysqli->error;
    }
    $mysqli->close();
    ?>

    chevron_right

    
    

    Output :
    Table After Updation –

    The output on Web Browser :

    Update Query using PDO Method :

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <?php
    try{
        $pdo = new PDO("mysql:host=localhost;
                        dbname=Mydb", "root", "");
        $pdo->setAttribute(PDO::ATTR_ERRMODE, 
                            PDO::ERRMODE_EXCEPTION);
    } catch(PDOException $e){
        die("ERROR: Could not connect. " 
                        . $e->getMessage());
    }
      
    try{
        $sql = "UPDATE data SET Age='28' WHERE id=201";
        $pdo->exec($sql);
        echo "Records was updated successfully.";
    } catch(PDOException $e){
        die("ERROR: Could not able to execute $sql. "
                                    . $e->getMessage());
    }
    unset($pdo);
    ?>

    chevron_right

    
    

    Output :
    Table After Updation –

    The output on Web Browser :



    My Personal Notes arrow_drop_up

    I am a technology enthusiast who has a keen interest in programming I am pursuing Engineering in Computer Science from GEU, Dehradun I like to unwind by watching movies and English sitcomsI have a keen interest in music

    If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

    Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




    Article Tags :
    Practice Tags :


    Be the First to upvote.


    Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.