PHP Classes

File: fwphp/01mater/shop/model/customer_db.php

Recommend this page to a friend!
  Classes of Slavko Srakocic  >  B12 PHP FW  >  fwphp/01mater/shop/model/customer_db.php  >  Download  
File: fwphp/01mater/shop/model/customer_db.php
Role: Example script
Content type: text/plain
Description: Example script
Class: B12 PHP FW
Manage database records with a PDO CRUD interface
Author: By
Last change: ver 7.0.1 mnu, msg, mkd FUNCTIONAL namespaces, CRUD PDO trait, pretty URL-s
Date: 3 months ago
Size: 4,107 bytes
 

Contents

Class file image Download
<?php
function is_valid_customer_email($email) {
    global
$db;
   
$query = '
        SELECT customerID FROM customers
        WHERE emailAddress = :email'
;
   
$statement = $db->prepare($query);
   
$statement->bindValue(':email', $email);
   
$statement->execute();
   
$valid = ($statement->rowCount() == 1);
   
$statement->closeCursor();
    return
$valid;
}

function
is_valid_customer_login($email, $password) {
    global
$db;
   
$password = sha1($email . $password);
   
$query = '
        SELECT * FROM customers
        WHERE emailAddress = :email AND password = :password'
;
   
$statement = $db->prepare($query);
   
$statement->bindValue(':email', $email);
   
$statement->bindValue(':password', $password);
   
$statement->execute();
   
$valid = ($statement->rowCount() == 1);
   
$statement->closeCursor();
    return
$valid;
}

function
get_customer($customer_id) {
    global
$db;
   
$query = 'SELECT * FROM customers WHERE customerID = :customer_id';
   
$statement = $db->prepare($query);
   
$statement->bindValue(':customer_id', $customer_id);
   
$statement->execute();
   
$customer = $statement->fetch();
   
$statement->closeCursor();
    return
$customer;
}

function
get_customer_by_email($email) {
    global
$db;
   
$query = 'SELECT * FROM customers WHERE emailAddress = :email';
   
$statement = $db->prepare($query);
   
$statement->bindValue(':email', $email);
   
$statement->execute();
   
$customer = $statement->fetch();
   
$statement->closeCursor();
    return
$customer;
}

function
customer_change_shipping_id($customer_id, $address_id) {
    global
$db;
   
$query = 'UPDATE customers SET shipAddressID = :address_id
              WHERE customerID = :customer_id'
;
   
$statement = $db->prepare($query);
   
$statement->bindValue(':address_id', $address_id);
   
$statement->bindValue(':customer_id', $customer_id);
   
$statement->execute();
   
$statement->closeCursor();
}

function
customer_change_billing_id($customer_id, $address_id) {
    global
$db;
   
$query = 'UPDATE customers SET billingAddressID = :address_id
              WHERE customerID = :customer_id'
;
   
$statement = $db->prepare($query);
   
$statement->bindValue(':address_id', $address_id);
   
$statement->bindValue(':customer_id', $customer_id);
   
$statement->execute();
   
$statement->closeCursor();
}

function
add_customer($email, $first_name, $last_name,
                     
$password_1) {
    global
$db;
   
$password = sha1($email . $password_1);
   
$query = '
        INSERT INTO customers (emailAddress, password, firstName, lastName)
        VALUES (:email, :password, :first_name, :last_name)'
;
   
$statement = $db->prepare($query);
   
$statement->bindValue(':email', $email);
   
$statement->bindValue(':password', $password);
   
$statement->bindValue(':first_name', $first_name);
   
$statement->bindValue(':last_name', $last_name);
   
$statement->execute();
   
$customer_id = $db->lastInsertId();
   
$statement->closeCursor();
    return
$customer_id;
}

function
update_customer($customer_id, $email, $first_name, $last_name,
                     
$password_1, $password_2) {
    global
$db;
   
$query = '
        UPDATE customers
        SET emailAddress = :email,
            firstName = :first_name,
            lastName = :last_name
        WHERE customerID = :customer_id'
;
   
$statement = $db->prepare($query);
   
$statement->bindValue(':email', $email);
   
$statement->bindValue(':first_name', $first_name);
   
$statement->bindValue(':last_name', $last_name);
   
$statement->bindValue(':customer_id', $customer_id);
   
$statement->execute();
   
$statement->closeCursor();

    if (!empty(
$password_1) && !empty($password_2)) {
       
$password = sha1($email . $password_1);
       
$query = '
            UPDATE customers
            SET password = :password
            WHERE customerID = :customer_id'
;
       
$statement = $db->prepare($query);
       
$statement->bindValue(':password', $password);
       
$statement->bindValue(':customer_id', $customer_id);
       
$statement->execute();
       
$statement->closeCursor();
    }
}
?>

For more information send a message to info at phpclasses dot org.