PHP Classes

File: delete.php

Recommend this page to a friend!
  Classes of Channaveer Hakari   PHP PDO CRUD Example   delete.php   Download  
File: delete.php
Role: Example script
Content type: text/plain
Description: Example script
Class: PHP PDO CRUD Example
Example code to implement CRUD operations with PDO
Author: By
Last change:
Date: 8 months ago
Size: 678 bytes
 

Contents

Class file image Download
<?php
require_once './db.php';

/** Usually you do this */
// $userId = filter_input(INPUT_GET, 'user_id', FILTER_VALIDATE_INT);
/** For the sake of demo I am hard coding */
$userId = 2;
try {
   
$userQuery = $pdo->prepare("
        DELETE FROM
            `users`
        WHERE
            `id` = :user_id
    "
);
   
$userQuery->execute([
       
':user_id' => $userId
   
]);

   
/** If no records were deleted then it will throw exception */
   
if ($userQuery->rowCount() < 1) {
        throw new
Exception('No records deleted.');
    }

    echo
'Deleted successfully.';
} catch (
Exception $e) {
   
/** Handle all your errors here */
   
exit($e->getMessage());
}