Bonjour, J'ai fait exactement ce que vous m'avez dit de faire, mais J'ai ce message d'erreur :
Fatal error: Uncaught Exception: PAYS_ID doit être un entier.L'objet ne peut pas être créé. in C:\wamp64\www\villes-site-en-poo-et-pdo\class\Ville.php on line 27
Exception: PAYS_ID doit être un entier.L'objet ne peut pas être créé. in C:\wamp64\www\villes-site-en-poo-et-pdo\class\Ville.php on line 27
Besoin d'aide, merci d'avance & bonne journée.
voici mon code :
process/process-ajout-ville.php
require('../includes/inc-connexion.php');
require('../class/Ville.php');
require('../class/villeManager.php');
if(isset($_POST['submit_form'])) {
$ville_nom_form = trim($_POST['ville_nom_form']);
$ville_texte_form = trim($_POST['ville_texte_form']);
$pays_id_form = trim($_POST['pays_id_form']);
if((empty($ville_nom_form)) OR empty($ville_texte_form) OR !isset($pays_id_form)) {
$message = '<p class="error">Veuillez remplir tous les champs !</p>';
}
else {
$ville_data = array(
'ville_id' => 1,
'ville_nom' => $ville_nom_form,
'ville_texte' => $ville_texte_form,
'pays_id' => $pays_id_form
);
$city_data = new Ville($ville_data);
$manager = new villeManager($db);
if(!$manager->getVilleExiste($ville_nom_form)) {
$manager->insertVille($city_data);
$message = '<p class="success">La ville ' . $ville_nom_form . ' a bien été ajouté !</p>';
}
else {
$message = '<p class="error">La ville ' . $ville_nom_form . ' n\'a pas été ajouté !</p>';
}
}
}
admin/ajout.php
<head>
<title>Ajouter une ville</title>
<meta charset="utf-8">
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<div id="wrapper">
<div id="container_add">
<h1>ajouter une ville</h1>
<form method="post" action="">
<p>Nom de la ville : <input type="text" id="ville_nom_form" name="ville_nom_form"></p>
<p>Texte de présentation<br>
<textarea name="ville_texte_form" cols="75" rows="25"></textarea>
</p>
<p>
<label>Choisissez le pays : </label>
<select name="pays_id_form">
<?php if(!empty($pays_data)) : ?>
<?php foreach($pays_data as $pays) : ?>
<option value="<?php echo $pays['pays_id']; ?>"><?php echo $pays['pays_nom']; ?></option>
<?php endforeach; ?>
<?php endif; ?>
</select>
<p><input type="submit" id="submit_form" name="submit_form" value="valider"></p>
</form>
<?php if(isset($message)) echo $message; ?>
</div>
</div>
</body>
class/Ville.php
class Ville {
private $_ville_id;
private $_ville_nom;
private $_ville_texte;
private $_pays_id;
private static $error;
const MSG_ERROR_VILLE_ID = 'VILLE_ID doit être un entier.';
const MSG_ERROR_VILLE_NOM = 'VILLE_NOM doit être une chaîne de caractères.';
const MSG_ERROR_VILLE_TEXTE = 'VILLE_TEXTE doit être une chaîne de caractères.';
const MSG_ERROR_PAYS_ID = 'PAYS_ID doit être un entier.';
const MSG_ERROR_END = 'L\'objet ne peut pas être créé.';
public function __construct(array $data) {
$this->setVilleId($data['ville_id']);
$this->setVilleNom($data['ville_nom']);
$this->setVilleTexte($data['ville_texte']);
$this->setPaysId($data['pays_id']);
if(!empty(self::$error)) {
throw new Exception(self::$error . self::MSG_ERROR_END);
}
}
public function setError($msg) {
self::$error = $msg;
}
public function getError() {
return self::$error;
}
public function setVilleId($ville_id) {
if((is_int($ville_id)) AND ($ville_id > 0)) {
$this->_ville_id = $ville_id;
}
else {
$this->setError(self::MSG_ERROR_VILLE_ID);
}
}
public function setVilleNom($ville_nom) {
if(is_string($ville_nom)) {
$this->_ville_nom = $ville_nom;
}
else {
$this->setError(self::MSG_ERROR_VILLE_NOM);
}
}
public function setVilleTexte($ville_texte) {
if(is_string($ville_texte)) {
$this->_ville_texte = $ville_texte;
}
else {
$this->setError(self::MSG_ERROR_VILLE_TEXTE);
}
}
public function setPaysId($pays_id) {
if((is_int($pays_id)) AND ($pays_id > 0)) {
$this->_pays_id = $pays_id;
}
else {
$this->setError(self::MSG_ERROR_PAYS_ID);
}
}
public function getVilleId() {
return $this->_ville_id;
}
public function getVilleNom() {
return $this->_ville_nom;
}
public function getVilleTexte() {
return $this->_ville_texte;
}
public function getPaysId() {
return $this->_pays_id;
}
}
class/villeManager.php
class villeManager {
private $_db;
public function __construct($db) {
$this->setDb($db);
}
public function setDb(PDO $dbh) {
$this->_db = $dbh;
}
public function getVilleExiste($ville_nom) {
$sql = 'SELECT ville_nom FROM villes WHERE ville_nom = :ville_nom';
$stmnt = $this->_db->prepare($sql);
$stmnt->execute(
array(
':ville_nom' => $ville_nom
)
);
$count = $stmnt->rowCount();
if($count > 0) {
return true;
}
else {
return false;
}
}
public function insertVille(Ville $ville) {
$sql = 'INSERT INTO villes (ville_nom, ville_texte, pays_id) VALUES (:ville_nom, :ville_texte, :pays_id)';
$ville_nom = htmlspecialchars($ville->getVilleNom());
$ville_texte = htmlspecialchars($ville->getVilleTexte());
$pays_id = $ville->getPaysId();
$stmnt = $this->_db->prepare($sql);
$stmnt->bindParam(':ville_nom', $ville_nom);
$stmnt->bindParam(':ville_texte', $ville_texte);
$stmnt->bindParam(':pays_id', $pays_id);
$stmnt->execute();
}
Bonjour, J'ai fait exactement ce que vous m'avez dit de faire, mais J'ai ce message d'erreur :
Fatal error: Uncaught Exception: PAYS_ID doit être un entier.L'objet ne peut pas être créé. in C:\wamp64\www\villes-site-en-poo-et-pdo\class\Ville.php on line 27
Exception: PAYS_ID doit être un entier.L'objet ne peut pas être créé. in C:\wamp64\www\villes-site-en-poo-et-pdo\class\Ville.php on line 27
Besoin d'aide, merci d'avance & bonne journée.
voici mon code :
process/process-ajout-ville.php
[PHP]
require('../includes/inc-connexion.php');
require('../class/Ville.php');
require('../class/villeManager.php');
if(isset($_POST['submit_form'])) {
$ville_nom_form = trim($_POST['ville_nom_form']);
$ville_texte_form = trim($_POST['ville_texte_form']);
$pays_id_form = trim($_POST['pays_id_form']);
if((empty($ville_nom_form)) OR empty($ville_texte_form) OR !isset($pays_id_form)) {
$message = '<p class="error">Veuillez remplir tous les champs !</p>';
}
else {
$ville_data = array(
'ville_id' => 1,
'ville_nom' => $ville_nom_form,
'ville_texte' => $ville_texte_form,
'pays_id' => $pays_id_form
);
$city_data = new Ville($ville_data);
$manager = new villeManager($db);
if(!$manager->getVilleExiste($ville_nom_form)) {
$manager->insertVille($city_data);
$message = '<p class="success">La ville ' . $ville_nom_form . ' a bien été ajouté !</p>';
}
else {
$message = '<p class="error">La ville ' . $ville_nom_form . ' n\'a pas été ajouté !</p>';
}
}
}
[/PHP]
admin/ajout.php
[html]
<head>
<title>Ajouter une ville</title>
<meta charset="utf-8">
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<div id="wrapper">
<div id="container_add">
<h1>ajouter une ville</h1>
<form method="post" action="">
<p>Nom de la ville : <input type="text" id="ville_nom_form" name="ville_nom_form"></p>
<p>Texte de présentation<br>
<textarea name="ville_texte_form" cols="75" rows="25"></textarea>
</p>
<p>
<label>Choisissez le pays : </label>
<select name="pays_id_form">
<?php if(!empty($pays_data)) : ?>
<?php foreach($pays_data as $pays) : ?>
<option value="<?php echo $pays['pays_id']; ?>"><?php echo $pays['pays_nom']; ?></option>
<?php endforeach; ?>
<?php endif; ?>
</select>
<p><input type="submit" id="submit_form" name="submit_form" value="valider"></p>
</form>
<?php if(isset($message)) echo $message; ?>
</div>
</div>
</body>
[/html]
class/Ville.php
[PHP]
class Ville {
private $_ville_id;
private $_ville_nom;
private $_ville_texte;
private $_pays_id;
private static $error;
const MSG_ERROR_VILLE_ID = 'VILLE_ID doit être un entier.';
const MSG_ERROR_VILLE_NOM = 'VILLE_NOM doit être une chaîne de caractères.';
const MSG_ERROR_VILLE_TEXTE = 'VILLE_TEXTE doit être une chaîne de caractères.';
const MSG_ERROR_PAYS_ID = 'PAYS_ID doit être un entier.';
const MSG_ERROR_END = 'L\'objet ne peut pas être créé.';
public function __construct(array $data) {
$this->setVilleId($data['ville_id']);
$this->setVilleNom($data['ville_nom']);
$this->setVilleTexte($data['ville_texte']);
$this->setPaysId($data['pays_id']);
if(!empty(self::$error)) {
throw new Exception(self::$error . self::MSG_ERROR_END);
}
}
public function setError($msg) {
self::$error = $msg;
}
public function getError() {
return self::$error;
}
public function setVilleId($ville_id) {
if((is_int($ville_id)) AND ($ville_id > 0)) {
$this->_ville_id = $ville_id;
}
else {
$this->setError(self::MSG_ERROR_VILLE_ID);
}
}
public function setVilleNom($ville_nom) {
if(is_string($ville_nom)) {
$this->_ville_nom = $ville_nom;
}
else {
$this->setError(self::MSG_ERROR_VILLE_NOM);
}
}
public function setVilleTexte($ville_texte) {
if(is_string($ville_texte)) {
$this->_ville_texte = $ville_texte;
}
else {
$this->setError(self::MSG_ERROR_VILLE_TEXTE);
}
}
public function setPaysId($pays_id) {
if((is_int($pays_id)) AND ($pays_id > 0)) {
$this->_pays_id = $pays_id;
}
else {
$this->setError(self::MSG_ERROR_PAYS_ID);
}
}
public function getVilleId() {
return $this->_ville_id;
}
public function getVilleNom() {
return $this->_ville_nom;
}
public function getVilleTexte() {
return $this->_ville_texte;
}
public function getPaysId() {
return $this->_pays_id;
}
}
[/PHP]
class/villeManager.php
[PHP]
class villeManager {
private $_db;
public function __construct($db) {
$this->setDb($db);
}
public function setDb(PDO $dbh) {
$this->_db = $dbh;
}
public function getVilleExiste($ville_nom) {
$sql = 'SELECT ville_nom FROM villes WHERE ville_nom = :ville_nom';
$stmnt = $this->_db->prepare($sql);
$stmnt->execute(
array(
':ville_nom' => $ville_nom
)
);
$count = $stmnt->rowCount();
if($count > 0) {
return true;
}
else {
return false;
}
}
public function insertVille(Ville $ville) {
$sql = 'INSERT INTO villes (ville_nom, ville_texte, pays_id) VALUES (:ville_nom, :ville_texte, :pays_id)';
$ville_nom = htmlspecialchars($ville->getVilleNom());
$ville_texte = htmlspecialchars($ville->getVilleTexte());
$pays_id = $ville->getPaysId();
$stmnt = $this->_db->prepare($sql);
$stmnt->bindParam(':ville_nom', $ville_nom);
$stmnt->bindParam(':ville_texte', $ville_texte);
$stmnt->bindParam(':pays_id', $pays_id);
$stmnt->execute();
}
[/PHP]