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
| <?php
// Fichier : essai.php
// PARTIE TRAITEMENTS
header('Content-Type: text/html; charset=UTF-8');
try
{
$pdo_options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_EXCEPTION;
$pdo_options[PDO::MYSQL_ATTR_INIT_COMMAND] = 'SET NAMES utf8';
$bdd = new PDO('mysql:host=localhost;dbname=projet', 'root', '', $pdo_options);
}
catch (PDOException $e)
{
die('Erreur : ' . $e->getMessage());
}
// Modification de l'actualité SI c'est le cas
if (isset($_POST['valider'], $_POST['id_news'], $_POST['contenu']) && (int)$_POST['id_news'] > 0)
{
$id = (int)$_POST['id_news'];
$contenu = $_POST['contenu'];
$req = $bdd->prepare('UPDATE news SET contenu = :contenu WHERE id = :id');
$req->bindParam(':contenu', $contenu, PDO::PARAM_STR);
$req->bindParam(':id', $id, PDO::PARAM_INT);
$req->execute();
// Redirection (rafraichissement de cette page)
header('Location: essai.php?id_news='.(int)$id);
exit();
}
// Récupération de la news (Modifiée ou pas)
if (isset($_GET['id_news']) && (int)$_GET['id_news'] > 0)
{
$id = (int)$_POST['id_news'];
//
$req = $bdd->prepare('SELECT id, titre, contenu FROM news WHERE id = :id');
$req->bindParam(':id', $id, PDO::PARAM_INT);
$req->execute();
$actualite = $req->fetch(PDO::FETCH_ASSOC);
}
// PARTIE INTERFACE / HTML
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>modifier actualité</title>
</head>
<body>
<b>(Formulaire) Modification actualité</b>
<form action="essai.php" method="post">
<p>
Titre : <?php echo $actualite['titre']; ?><br />
<label for="contenu">Contenu</label> :<br />
<textarea name="contenu" rows="8" cols="45" id="contenu"><?php echo $actualite['contenu']; ?></textarea><br />
<input type="hidden" name="id_news" value="<?php echo (int)$actualite['id']; ?>" />
<input type="submit" name="valider" value="Valider" />
</p>
</form>
</body>
</html> |
Partager