PHP Code How To Delete Data From MySQL Database By Id Using PDO

Php Code To Delete Row From Mysql Database Table Using PDO.


________________________________________________________

In this Php Tutorial we will Learn How To Delete MySQL Database Table Row By Id In Php  using  PDO .
I Use In This Tutorial:
- NetBeans IDE .
- XAMPP .
- PhpMyAdmin .


 




Php Source Code:


<?php

// php delete data in mysql database using PDO

if(isset($_POST['delete']))
{
    try {
        $pdoConnect = new PDO("mysql:host=localhost;dbname=test_db","root","");
    } catch (PDOException $exc) {
        echo $exc->getMessage();
        exit();
    }
    
     // get id to delete

    $id = $_POST['id'];
    
     // mysql delete query 

    $pdoQuery = "DELETE FROM `users` WHERE `id` = :id";
    
    $pdoResult = $pdoConnect->prepare($pdoQuery);
    
    $pdoExec = $pdoResult->execute(array(":id"=>$id));
    
    if($pdoExec)
    {
        echo 'Data Deleted';
    }else{
        echo 'ERROR Data Not Deleted';
    }

}

?>

<!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_delete_data_in_mysql_database_using_pdo.php" method="post">

          ID To Delete : <input type="text" name="id" required><br><br>

          <input type="submit" name="delete" value="Delete Data">

        </form>

    </body>

</html>



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



                     
php delete mysql pdo
Php Delete




Share this

Related Posts

Previous
Next Post »