-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDatabase.php
141 lines (117 loc) · 3.69 KB
/
Database.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?php
/**
* Class Database
* Manage Database from a form
*/
class Database {
private $_all_fields;
private $_table_name;
private $_bdd;
/**
*@param string Database name
*@param string User name to connect database
*@param string Password to connect bdd
*@param string Table name which will be created
*@param array Array of all fields name of the form
*@return void
*/
public function __construct($dbname, $dbuser, $dbpassword, $tableName, $data){
$this->_table_name = $tableName;
$this->_all_fields = $data;
try
{
$this->_bdd = new PDO('mysql:host=localhost;dbname='.$dbname.';charset=utf8', $dbuser , $dbpassword);
}
catch(Exception $e)
{
die('Erreur : '.$e->getMessage());
}
$requete = "CREATE TABLE IF NOT EXISTS ". $tableName ." (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, ";
foreach ($data as $key => $value) {
$requete = $requete. $value ." VARCHAR (255) NULL, ";
}
$requete = substr($requete, 0, -2);
$requete = $requete.")";
$this->_bdd->prepare($requete)->execute();
}
/**
*@return array array of all fields in the table
*/
public function get_all_fields() {
return $this->_all_fields;
}
/**
*@return string table name
*/
public function get_table_name() {
return $this->_table_name;
}
/**
*@return PDO object bdd
*/
public function get_bdd() {
return $this->_bdd;
}
/**
*@param array Array key : field name in the database, value : value to set in the database
*@return void
*/
public function add($array) : void {
$requete = "INSERT INTO " . $this->_table_name . " (";
$labels = "";
$values_string = "";
foreach ($array as $key => $value) {
$labels = $labels . $key . ", ";
$values_string = $values_string . "?, ";
$values[] = htmlentities($value);
}
$labels = substr($labels, 0, -2);
$values_string = substr($values_string, 0, -2);
$requete = $requete . $labels . ") VALUES (" . $values_string . ")";
$this->_bdd->prepare($requete)->execute($values);
}
/**
*@param array key = field to look for, value = the value which is to be found to catch the row in the table. This array can have multiple keys, you have to pass the minimum of key to identify the row which be deleted
*@return void
*/
public function delete($array){
$requete = "DELETE FROM " . $this->_table_name ." WHERE ";
foreach ($array as $key => $value){
$requete = $requete.$key."="."'".htmlentities($value)."'"."AND ";
}
$requete = substr($requete, 0, -4);
$this->_bdd->exec($requete);
}
/**
*@param array key = field to look for, value = the value which is to be found to catch the row in the table. This array can have multiple keys, you have to pass the minimum of key to identify the row which be updated
*@param array key = field which will be updated, value = the updated value
*@return void
*/
public function update($array_find, $array_update){
$check = "SELECT * FROM " . $this->_table_name ." WHERE ";
$requete = "UPDATE " . $this->_table_name ." SET ";
foreach ($array_update as $key => $value){
$requete = $requete.$key."="."'".htmlentities($value)."'".", ";
}
$requete = substr($requete, 0, -2);
$requete = $requete." WHERE ";
foreach ($array_find as $key => $value){
$requete = $requete.$key."="."'".htmlentities($value)."'"." AND ";
$check = $check.$key."="."'".htmlentities($value)."'"." AND ";
}
$check = substr($check, 0, -4);
$requete = substr($requete, 0, -4);
$check_request = $this->_bdd->query($check);
if ($check_request->fetch() == false){
?>
<script type="text/javascript">
alert("No user found");
</script>
<?php
} else {
$this->_bdd->exec($requete);
}
}
}
?>