Commit eadb989f authored by Quentin Vrel's avatar Quentin Vrel

Tp Pdo 11/06

parent 9162cf40
<?php
define('_MYSQL_HOST','127.0.0.1');
define('_MYSQL_PORT',3306);
define('_MYSQL_DBNAME','dbtest');
define('_MYSQL_USER','root');
define('_MYSQL_PASSWORD','');
$connectionString = "mysql:host=". _MYSQL_HOST;
if(defined('_MYSQL_PORT'))
$connectionString .= ";port=". _MYSQL_PORT;
$connectionString .= ";dbname=" . _MYSQL_DBNAME;
$options = array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8' );
try {
$pdo = new PDO($connectionString,_MYSQL_USER,_MYSQL_PASSWORD,$options);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch (PDOException $erreur) {
myLog('Erreur : '.$erreur->getMessage());
die("HA LE NUL!");
}
<?php
// initialise une variable $pdo connecté à la base locale
require_once("initPDO.php"); // cf. doc / cours
$request = $pdo->prepare("select * from users");
// à vous de compléter...
// afficher un tableau HTML avec les donnéees en utilisant fetch(PDO::FETCH_OBJ)
if(isset($_POST['name']) && isset($_POST['mail'])){
$insert = $pdo->prepare("INSERT INTO `users` (`id`, `name`, `email`) VALUES (NULL, '".$_POST['name']."', '".$_POST['mail']."');");
$insert->execute();
}
$request->execute();
?>
<html>
<body>
<h1> Users</h1>
<table>
<tr>
<th>User</th>
<th>email</th>
</tr>
<?php
while($line = $request->fetch()){
//var_dump($line);
echo "<tr>
<td>".$line['name']."</th>
<td>".$line['email']."</th>
</tr>";
}
/*** close the database connection ***/
$pdo = null;
?>
</table>
<form target="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
Name:
<input type="text" id="name" name="name"/><br>
Mail:
<input type="text" id="mail" name="mail"/><br>
<input type="submit" value="Ajouter">
</form>
</body>
</html>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment