Php Code : How To Update MySQL Database Table In Php Using Pdo

Php Code To Update Mysql Database Table Data Using Pdo.


________________________________________________________

In this Php Tutorial we will Learn How To Update MySQL Database Table Data From Form Inputs In Php using Pdo.
I Use In This Tutorial:
- NetBeans IDE .
- XAMPP .
- PhpMyAdmin .


 




Php Source Code:

<?php

// php update data in mysql database using PDO

if(isset($_POST['update']))
{
    try {
        $pdoConnect = new PDO("mysql:host=localhost;dbname=test_db","root","");
    } catch (PDOException $exc) {
        echo $exc->getMessage();
        exit();
    }
    
    // get values form input text and number
    
    $id = $_POST['id'];
    $fname = $_POST['fname'];
    $lname = $_POST['lname'];
    $age = $_POST['age'];
    
    // mysql query to Update data
    
    $query = "UPDATE `users` SET `fname`=:fname,`lname`=:lname,`age`=:age WHERE `id` = :id";
    
    $pdoResult = $pdoConnect->prepare($query);
    
    $pdoExec = $pdoResult->execute(array(":fname"=>$fname,":lname"=>$lname,":age"=>$age,":id"=>$id));
    
    if($pdoExec)
    {
        echo 'Data Updated';
    }else{
        echo 'ERROR Data Not Updated';
    }

}

?>

<!DOCTYPE html>
<html>
    <head>
        <title>PHP UPDATE DATA USING PDO</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
        <form action="php_update_data_from_mysql_database_using_pdo.php" method="post">
            <input type="text" name="id" required placeholder="Id"><br><br>
            <input type="text" name="fname" required placeholder="First Name"><br><br>
            <input type="text" name="lname" required placeholder="Last Name"><br><br>
            <input type="number" name="age" required placeholder="Age" min="10" max="100"><br><br>
            <input type="submit" name="update" required placeholder="Update Data">
        </form>
    </body>
</html>


///////////////OUTPUT:



                     
php update mysql pdo
Php Update




Share this

Related Posts

Previous
Next Post »

1 comments:

comments
5 octobre 2020 à 06:11 delete

Hello, how to get the information from the database, when clicking the button for the id?
Because I have a lot of fields and it would be ideal to show the information already recorded in the database.

Reply
avatar