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
 
class School {
 
	protected $_id;
	protected $_name;
 
	protected static $error;
 
	const MESSAGE_ERROR_ID = 'ID doit être un entier.';
	const MESSAGE_ERROR_TEXT = 'TEXT doit être une chaîne de caractères.';
	const MESSAGE_ERROR_DATECREATION = 'DATE doit être au format YYYY-MM-DD.';
	const MESSAGE_ERROR_TITLE = 'TITRE doit être une chaîne de caractères.';
	const MESSAGE_ERROR_END = 'L\'objet ne peut pas être créé.';
 
	public function __construct(array $schools) {
 
		$this->setId($schools['id']);
		$this->setName($schools['name']);
 
		if(!empty(self::$error)) {
 
			throw new Exception(self::$error . self::MESSAGE_ERROR_END);
		}
	}
 
	public function setError($message) {
 
		self::$error = $message;
	}
 
	public function getError() {
 
		return self::$error;
	}
 
	public function setId($id) {
 
		if((is_int($id)) AND ($id > 0)) {
 
			$this->_id = $id;
		}
		else {
 
			$this->setError(self::MESSAGE_ERROR_ID);
		}
	}
 
	public function setName($name) {
 
		if(is_string($name)) {
 
			$this->_name = $name;
		}
		else {
 
			$this->setError(self::MESSAGE_ERROR_TEXT);
		}
	}
 
	public function getId() {
 
		return $this->_id;
	}
 
	public function getName() {
 
		return $this->_name;
	}
}
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
 
class schoolManager {
 
	private $_bdd;
 
	public function __construct($bdd) {
 
		$this->setDb($bdd);
	}
 
	public function setDb(PDO $db) {
 
		$this->__bdd = $db;
	}
 
	public function addSchools(School $school) {
 
		$query = 'INSERT INTO school (name) VALUES(:name)';
		$stmnt = $this->__bdd->prepare($query);
		$stmnt->execute([':name' => htmlspecialchars(($school)->getName())]);
	}
 
	public function getSchools($id = '') {
 
		if(empty($id)) {
 
			$query = 'SELECT id, name FROM school';
            $stmnt = $this->__bdd->prepare($query);
		}
        elseif(is_numeric($id)) {
 
			$query = 'SELECT id, name FROM school WHERE id = :id';
			$stmnt = $this->__bdd->prepare($query);
			$stmnt->bindParam(':id', $id);
		}
 
		$stmnt->execute();
 
		while($row = $stmnt->fetch(PDO::FETCH_ASSOC)) {
 
			$results[] = $row;
		}
 
		return $results;
	}
}
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
 
require('class/School.php');
require('class/schoolManager.php');
 
try {
 
	$bdd = new PDO('mysql:host=localhost;dbname=schools', '10111110', '10111110');
}
 
catch(Exception $e) {
 
	'Erreur de connexion : ' . $e->getMessage();
}
 
$manager = new schoolManager($bdd);
 
$schools_data = array(
 
	1 => array('id' => 1, 'name' => 'Ecole A'),
	2 => array('id' => 2, 'name' => 'Ecole B'),
	3 => array('id' => 3, 'name' => 'Ecole C')
);
 
$schools = new School($schools_data);
 
$manager->addSchools($schools);
 
$school = $manager->getSchools();
var_dump($school);
J'ai une table school avec les champs id, name & je dois enregistrer dans name -> Ecole A Ecole B Ecole C mais j'ai ces messages d'erreurs ->

- Notice: Undefined index: id in C:\wamp64\www\php-expert\07-devoir-1-refait\class\School.php on line 18
- Notice: Undefined index: name in C:\wamp64\www\php-expert\07-devoir-1-refait\class\School.php on line 19
- Fatal error: Uncaught Exception: TEXT doit être une chaîne de caractères.L'objet ne peut pas être créé. in C:\wamp64\www\php-expert\07-devoir-1-refait\class\School.php on line 23
- Exception: TEXT doit être une chaîne de caractères.L'objet ne peut pas être créé. in C:\wamp64\www\php-expert\07-devoir-1-refait\class\School.php on line 23
Merci à ceux & celles qui pourraient m'apporter leurs aides. Bon Weekend à tous.