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
| <?php
class user
{
private $_id;
private $_nom;
private $_motdepass;
private $_email;
public function id()
{
return $this->_id;
}
public function nom()
{
return $this->_nom;
}
public function motdepass()
{
return $this->_motdepass;
}
public function email()
{
return $this->_email;
}
}
// ensuite le manager de la classe
<?php
class manager
{
private $_db;
public function add(user $user)
{
$manage = new manager($db);
$q = $this->Db()->prepare('INSERT INTO utilisateur(nom) VALUES(:nom)');
$q->bindValue(':nom', $user->nom());
$q->execute();
}
public function Db()
{
return $this->_db;
}
}
// et ma page contenant le formulaire
<?php
require("user.class.php");
require("manager.class.php");
$dns=('mysql:host=localhost;dbname=test');
$users='root';
$pass='';
$db = new PDO($dns,$users,$pass);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING); // On émet une alerte à chaque fois qu'une requête a échoué.
if (isset($_REQUEST['valid']) && isset($_REQUEST['nom'])) // Si on a voulucréer un personnage.
{
$user = new user(array('nom' => $_REQUEST['nom'])); // On créeun nouveau personnage.
$manage->add($user);
}
?>
<html>
<body>
<form method="get" action="">
NOM:<input type="text" name="nom"/><br/>
<input type="submit" name="valid" value="valider"/>
</form>
</body>
</html> |
Partager