-
Notifications
You must be signed in to change notification settings - Fork 0
/
myClass.php
81 lines (79 loc) · 2.34 KB
/
myClass.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
<?php
require_once("db.php");
class myClass{
private $id;
private $noms;
private $adresse;
protected $connection;
public function __construct($id=0,$noms="",$adresse=""){
$this->id=$id;
$this->noms=$noms;
$this->adresse=$adresse;
$this->connection=new PDO(DB_TYPE.":host=".DB_HOST.";dbname=".DB_NAME,DB_USER,DB_PWD,[PDO::ATTR_DEFAULT_FETCH_MODE=>PDO::FETCH_ASSOC]);
}
public function setId($id){
$this->id=$id;
}
public function getId(){
return $this->id=$id;
}
public function setNoms($noms){
$this->noms=$noms;
}
public function getNoms(){
return $this->noms=$noms;
}
public function setAdresse($adresse){
$this->adresse=$adresse;
}
public function getAdresse(){
return $this->adresse=$adresse;
}
public function insertData(){
try {
$stmt=$this->connection->prepare("INSERT INTO `tbl_data`(`noms`, `adresse`) VALUES (?,?)");
$stmt->execute([$this->noms,$this->adresse]);
echo "<script>alert('Données enregistrées avec succès!');document.location='index.php'</script>";
} catch (Exception $e) {
return $e->getMessage();
}
}
public function selectData(){
try {
$stmt=$this->connection->prepare("SELECT * FROM `tbl_data`");
$stmt->execute();
return $stmt->fetchAll();
} catch (Exception $e) {
return $e->getMessage();
}
}
public function selectOne(){
try {
$stmt=$this->connection->prepare("SELECT * FROM `tbl_data` WHERE id=?");
$stmt->execute([$this->id]);
return $stmt->fetchAll();
} catch (Exception $e) {
return $e->getMessage();
}
}
public function updateData(){
try {
$stmt=$this->connection->prepare("UPDATE `tbl_data` SET `noms`=?,`adresse`=? WHERE `id`=?");
$stmt->execute([$this->noms,$this->adresse,$this->id]);
echo "<script>alert('Données modifiées avec succès!');document.location='index.php'</script>";
} catch (Exception $e) {
return $e->getMessage();
}
}
public function deleteData(){
try {
$stmt=$this->connection->prepare("DELETE FROM `tbl_data` WHERE id=?");
$stmt->execute([$this->id]);
return $stmt->fetchAll();
echo "<script>alert('Données supprimées avec succès!');document.location='index.php'</script>";
} catch (Exception $e) {
return $e->getMessage();
}
}
}
?>