INSERT INTO : Fatal error...
Résolu
iMarvinR
Messages postés
5
Statut
Membre
-
iMarvinR Messages postés 5 Statut Membre -
iMarvinR Messages postés 5 Statut Membre -
Bonjour,
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...
// Personnage.class.php...
// PersonnageManager.class.php
J'ai une erreur dans ma requête sql
( ! ) 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
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...
<?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...
<?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
<?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
INSERT INTO...dans le fichier PersonnageManager.class.php
( ! ) 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
A voir également:
- INSERT INTO : Fatal error...
- Touche insert - Guide
- Disk boot failure insert system disk and press enter - Guide
- Insert boot media in selected boot device and press a key ✓ - Forum Windows 10
- Activer touche insert sur ASUS UX32A - Forum Clavier
- Monitor will go into standby mode traduction français ✓ - Forum Matériel & Système