PHP Classes

File: create.php

Recommend this page to a friend!
  Classes of Channaveer Hakari   PHP PDO CRUD Example   create.php   Download  
File: create.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: 765 bytes
 

Contents

Class file image Download
<?php
require_once './db.php';
try {
   
$userQuery = $pdo->prepare("
        INSERT INTO `users`
            (`first_name`, `last_name`, `email`, `password`)
        VALUES
            (:first_name, :last_name, :email, :password)
    "
);
   
$user = $userQuery->execute([
       
':first_name' => 'Channaveer',
       
':last_name' => 'Hakari',
       
':email' => 'someemail@gmail.com',
       
/** For the sake of demonstration I am using MD5, using hash for more secure */
       
':password' => md5('password')
    ]);

    if (!
$user) {
        throw new
Exception('Error in adding user details');
    }

   
$lastInsertedIs = $pdo->lastInsertId();
} catch (
Exception $e) {
   
/** Handle all your errors here */
   
exit($e->getMessage());
}