Fatal Error => INSERT INTO
Bonsoir,
Mon projet est constituer de 3 fichiers :
* index.php (connexion à mysql, inclusion des classes, instanciation de la classe Personnage, instanciation de la classe PersonnageManager, requêtes sql)
* Personnage.class.php (représente des joueurs)
* PersonnageManager.class.php (envoie des requêtes à mysql)
// index.php...
Code:
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
| <?php
// Connexion à mysql...
try
{
$pdo_options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_EXCEPTION;
$bdd = new PDO('mysql:host=localhost;dbname=test', 'root', '', $pdo_options);
}
catch(Exception $e)
{
die('Erreur : ' .$e->getMessage());
}
// Inclusion des class...
function inclureClass($class)
{
$nomClass = $class . '.class.php';
require($nomClass);
}
spl_autoload_register('inclureClass');
// Instanciation de la class Personnage
$perso1 = new Personnage(array(
'nom' => 'Marvin',
'degats' => 0,
'force' => 10,
'experience' => 1
));
$perso2 = new Personnage(array(
'nom' => 'Gaetan',
'degats' => 0,
'force' => 9,
'experience' => 1
));
echo $perso1->getNom() . ' possède ' . $perso1->getDegats() . ' de degats, ' . $perso1->getForce() . ' de force et ' . $perso1->getExperience() . ' d\'experience !<br />';
echo $perso2->getNom() . ' possède ' . $perso2->getDegats() . ' de degats, ' . $perso2->getForce() . ' de force et ' . $perso2->getExperience() . ' d\'experience !<br />';
// Instanciation de la class PersonnageManager...
$manager = new PersonnageManager($bdd);
// Requêtes sql...
$manager->create($perso1); |
// Personnage.class.php...
Code:
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
| <?php
class Personnage
{
private $_id;
private $_nom;
private $_degats;
private $_force;
private $_experience;
public function __construct(array $data)
{
$this->hydrate($data);
}
public function hydrate(array $data2)
{
foreach($data2 as $key => $value)
{
$method = 'set' . ucfirst($key);
if(method_exists($this, $method))
{
$this->$method($value);
}
}
}
// Les getters...
public function getId()
{
return $this->_id;
}
public function getNom()
{
return $this->_nom;
}
public function getDegats()
{
return $this->_degats;
}
public function getForce()
{
return $this->_force;
}
public function getExperience()
{
return $this->_experience;
}
// Les setters...
public function setNom($perso)
{
if(is_string($perso) AND strlen($perso) <=30)
{
$this->_nom = $perso;
}
else
{
trigger_error('Le nom est invalide !');
return;
}
}
public function setDegats($force)
{
$force = (int) $force;
if($force >= 0 AND $force <= 30)
{
$this->_degats += $force;
}
else
{
trigger_error('La valeur de la force n\'est pas valide !');
return;
}
}
public function setForce($force2)
{
$force2 = (int) $force2;
if($force2 > 0 AND $force2 <= 30)
{
$this->_force = $force2;
}
else
{
trigger_error('La valeur de la force n\'est pas valide !');
return;
}
}
public function setExperience()
{
$this->_experience++;
}
public function frapper($perso2)
{
if(is_string($perso2) AND strlen($perso2) <= 30)
{
$perso2->setDegats($this->_force);
$this->setExperience;
}
else
{
trigger_error('Le nom de la victime est invalide !');
return;
}
}
} |
// PersonnageManager.class.php
Code:
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
|
<?php
class PersonnageManager
{
private $_bdd;
public function __construct(PDO $bdd)
{
$this->setBdd($bdd);
}
public function setBdd(PDO $bdd2)
{
$this->_bdd = $bdd2;
}
public function create(Personnage $perso)
{
$request = $this->_bdd->prepare('INSERT INTO personnages SET nom = :nom, degats = :degats, force = :force, experience = :experience');
$request->bindValue(':nom', $perso->getNom());
$request->bindValue(':degats', $perso->getDegats(), PDO::PARAM_INT);
$request->bindValue(':force', $perso->getForce(), PDO::PARAM_INT);
$request->bindValue(':experience', $perso->getExperience(), PDO::PARAM_INT);
$request->execute();
}
public function afficherObjet()
{
var_dump($this->_bdd instanceof PDO);
}
} |
J'ai une erreur dans ma requête sql dans le fichier PersonnageManager.class.php
Citation:
( ! ) Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'force = 10, experience = 1' at line 1' in C:\wamp\www\poo\PersonnageManager.class.php on line 23
( ! ) PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'force = 10, experience = 1' at line 1 in C:\wamp\www\poo\PersonnageManager.class.php on line 23