-
Notifications
You must be signed in to change notification settings - Fork 0
/
clientClass.php
82 lines (74 loc) · 2.06 KB
/
clientClass.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<?php
Class Customer {
private $pdo;
// Construtor
public function __construct($dbname, $host, $user, $password)
{
try
{
$this->pdo = new PDO("mysql:dbname=".$dbname.";host=".$host, $user, $password);
} catch (PDOException $e)
{
echo "Erro com o banco de dados: ".$e->getMessage();
exit();
} catch (Exception $e)
{
echo "Erro genérico: ".$e->getMessage();
}
}
// Select
public function fetchData()
{
$result = array(); // Caso o banco esteja vazio evita erro e retorna um array vazio
$cmd = $this->pdo->query("SELECT * FROM cliente ORDER BY id DESC");
$result = $cmd->fetchAll(PDO::FETCH_ASSOC);
return $result;
}
// Insert
public function registerCustomer($nome, $celular, $email)
{
// Validar se email já existe no banco
$cmd = $this->pdo->prepare("SELECT id FROM cliente WHERE email = :email");
$cmd->bindValue(":email", $email);
$cmd->execute();
if ($cmd->rowCount() > 0) // Email já existe no BD
{
return false;
} else
{
$cmd = $this->pdo->prepare("INSERT INTO cliente(nome, celular, email) VALUES(:nome, :celular, :email)");
$cmd->bindValue(":nome", $nome);
$cmd->bindValue(":celular", $celular);
$cmd->bindValue(":email", $email);
$cmd->execute();
return true;
}
}
// Delete
public function deleteCustomer($id)
{
$cmd = $this->pdo->prepare("DELETE FROM cliente WHERE id = :id");
$cmd->bindValue("id", $id);
$cmd->execute();
}
public function fetchDataCustomer($id)
{
$response = array();
$cmd = $this->pdo->prepare("SELECT * FROM cliente WHERE id = :id");
$cmd->bindValue(":id", $id);
$cmd->execute();
$response = $cmd->fetch(PDO::FETCH_ASSOC);
return $response;
}
// Update
public function updateData($id, $nome, $celular, $email)
{
$cmd = $this->pdo->prepare("UPDATE cliente SET nome = :n, celular = :c, email = :e WHERE id = :id");
$cmd->bindValue(":n", $nome);
$cmd->bindValue(":c", $celular);
$cmd->bindValue(":e", $email);
$cmd->bindValue(":id", $id);
$cmd->execute();
}
}
?>