Client.php

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
 
 
<?php
 
class Client {
 
    protected $name;
    protected $email;
    protected $dt_creation;
    protected $dt_arrival;
    protected $dt_departure;
 
 
    protected static $error;
 
    const MSG_ERROR_NAME = 'NOM doit être une chaîne de caractères.';
    const MSG_ERROR_EMAIL = 'EMAIL doit être une chaîne de caractères.';
    const MSG_ERROR_DATECREATION = 'DATE doit être format YYYY-MM-DD.';
    const MSG_ERROR_DATEARRIVAL = 'DATE doit être format YYYY-MM-DD.';
    const MSG_ERROR_DATEDEPARTURE = 'DATE doit être format YYYY-MM-DD.';
    const MSG_ERROR_OBJET = 'L\'objet ne peut pas être crée.';
 
    public function __construct(array $data) {
 
    	$this->setName($data['name']);
    	$this->setEmail($data['email']);
    	$this->setDt_creation($data['dt_creation']);
        $this->setDt_arrival($data['dt_arrival']);
        $this->setDt_departure($data['dt_departure']);
 
    	if(!empty(self::$error)) {
 
        	throw new Exception(self::$error . self::MSG_ERROR_OBJET);
        }
    }
 
    public function setError($msg) {
 
    	self::$error = $msg;
    }
 
    public function getError() {
 
    	return self::$error;
    }
 
    public function setName($name) {
 
    	if(is_string($name)) {
 
    		$this->name = $name;
    	}
    	else {
 
    		$this->setError(self::MSG_ERROR_NAME);
    	}
    }
 
    public function setEmail($email) {
 
    	if(is_string($email)) {
 
    		$this->email = $email;
    	}
    	else {
 
    		$this->setError(self::MSG_ERROR_EMAIL);
    	}
    }
 
    public function setDt_creation($dt_creation) {
 
		list($y, $m, $d) = explode("-", $dt_creation);
 
		if(checkdate($m, $d, $y)) {
 
			$this->dt_creation = $dt_creation;
        }
        else {
 
        	$this->setError(self::MSG_ERROR_DATECREATION);
        }
	}
 
    public function setDt_arrival($dt_arrival) {
 
        list($y, $m, $d) = explode("-", $dt_arrival);
 
        if(checkdate($m, $d, $y)) {
 
            $this->dt_arrival = $dt_arrival;
        }
        else {
 
            $this->setError(self::MSG_ERROR_DATEARRIVAL);
        }
    }
 
    public function setDt_departure($dt_departure) {
 
        list($y, $m, $d) = explode("-", $dt_departure);
 
        if(checkdate($m, $d, $y)) {
 
            $this->dt_departure = $dt_departure;
        }
        else {
 
            $this->setError(self::MSG_ERROR_DATEDEPARTURE);
        }
    }
 
	public function getName() {
 
		return $this->name;
	}
 
	public function getEmail() {
 
		return $this->email;
	}
 
	public function getDt_creation() {
 
		return $this->dt_creation;
	}
 
    public function getDt_arrival() {
 
        return $this->dt_arrival;
    }
 
    public function getDt_departure() {
 
        return $this->dt_departure;
    }
}
 
?>
clientManager.php

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
 
 
<?php
 
class clientManager {
 
	private $_db;
 
	public function __construct($db) {
 
		$this->setDb($db);
	}
 
	public function setDb(PDO $dbh) {
 
		$this->_db = $dbh;
	}
 
	public function addClient(Client $client) {
 
		$sql = 'INSERT INTO client (name, email, dt_creation, dt_arrival, dt_departure)
        VALUES(:name, :email, :dt_creation, :dt_arrival, :dt_departure)';
 
		$name = htmlspecialchars($client->getName());
        $email = htmlspecialchars($client->getEmail());
        $dt_creation = $client->getDt_creation();
        $dt_arrival = $client->getDt_arrival();
        $dt_departure = $client->getDt_departure();
 
        $stmnt = $this->_db->prepare($sql);
        $stmnt->bindParam(':name', $name);
        $stmnt->bindParam(':email', $email);
        $stmnt->bindParam(':dt_creation', $dt_creation);
        $stmnt->bindParam('dt_arrival', $dt_arrival);
        $stmnt->bindParam('dt_departure', $dt_departure);
        $stmnt->execute();
 
        if($stmnt->rowCount() > 0) {
 
            $message = '<p class="error">Ce client est déjà enregistré. </p>';
        }
    }
}
 
?>
process/process_form.php

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
 
 
<?php
 
require('class/clientManager.php');
require('class/Client.php');
 
try {
 
	$db = new PDO('mysql:host=localhost; dbname=hotels', 'root', '10111110');
}
 
catch(Exception $e) {
 
	die('Erreur de connexion ' . $e->getMessage());
}
 
if(isset($_POST['submit_form'])) {
 
	$username_form = $_POST['username_form'];
	$email_form = $_POST['email_form'];
	$date_creation_form = $_POST['date_creation_form'];
	$date_arrival_form = $_POST['date_arrival_form'];
	$date_departure_form = $_POST['date_departure_form'];
 
	if((empty($username_form)) OR (empty($email_form)) OR (empty($date_creation_form)) OR (empty($date_arrival_form))
 
		OR (empty($date_departure_form))) {
 
		$message = '<p class="error">Tous les champs doivent être remplis !</p>';
	}
	else {
 
		if(!preg_match("#^[a-z0-9._-]+@[a-z0-9._-]{2,}\.[a-z]{2,4}$#", $email_form)) {
 
			$message = '<p class="error">Indiquer une adresse mail valide.</p>';
		}
		else {
 
			$clients_data = array(
 
				'name' => $username_form,
		        'email' => $email_form,
		        'dt_creation' => $date_creation_form,
		        'dt_arrival' => $date_arrival_form,
		        'dt_departure' => $date_departure_form
	        );
 
	        $clients = new Client($clients_data);
 
            $manager = new clientManager($db);
            $manager->addClient($clients);
            var_dump($clients);
        }
   }
}
 
?>
Bonjour, dans le fichier clientManager.php, j'ai utliser rowCount() pour vérifier si une entrée existe ou pas dans la table & ça ne fonctionne pas. J'ai des enregistrements en double.

Merci pour votre aide.