Modules de news (et login)
Posté : 16 août 2013, 01:14
Bonjour... j'ai suivis un tuto pour faire un module news (simple!!!)
Pouvez-vous (si vous avez le temps et la patience) me dire si mon code n'est pas affreux?
Que dois-je faire pour l'améliorer?
J'ai déjà essayé de le tourner pdo...
Voici le code:
Liste_news.php
rediger_news.php
Trouvez vous mon code cohérent?
Merci pour votre aide ^^
Pouvez-vous (si vous avez le temps et la patience) me dire si mon code n'est pas affreux?
Que dois-je faire pour l'améliorer?
J'ai déjà essayé de le tourner pdo...
Voici le code:
Liste_news.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr" >
<head>
<title>Liste des news</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<style type="text/css">
h2, th, td
{
text-align:center;
}
table
{
border-collapse:collapse;
border:2px solid black;
margin:auto;
}
th, td
{
border:1px solid black;
}
</style>
</head>
<body>
<h2><a href="rediger_news.php">Ajouter une news</a></h2>
<?php
require_once 'connect.php';
if (isset($_POST['titre']) AND isset($_POST['contenu']))
{
$titre = addslashes($_POST['titre']);
$contenu = addslashes($_POST['contenu']);
if ($_POST['id_news'] == 0)
{
$retour = $bdd->prepare("INSERT INTO mynews VALUES('', :titre, :contenu, '" . time() . "')");
$retour->bindParam(':titre', $titre, PDO::PARAM_STR);
$retour->bindParam(':contenu', $contenu, PDO::PARAM_STR);
$retour->execute();
}
else
{
$_POST['id_news'] = addslashes($_POST['id_news']);
$retour = $bdd->prepare("UPDATE mynews SET titre=:titre, contenu=:contenu WHERE id='" . $_POST['id_news'] . "'");
$retour->bindParam(':titre', $titre, PDO::PARAM_STR);
$retour->bindParam(':contenu', $contenu, PDO::PARAM_STR);
$retour->execute();
}
}
if (isset($_GET['supprimer_news'])) // Si l'on demande de supprimer une news.
{
$retour = $bdd->exec('DELETE FROM mynews WHERE id=\'' . $_GET['supprimer_news'] . '\'');
}
?>
<table><tr>
<th>Modifier</th>
<th>Supprimer</th>
<th>Titre</th>
<th>Date</th>
</tr>
<?php
$retour = $bdd->query('SELECT * FROM mynews ORDER BY id DESC LIMIT 0, 10');
while ($donnees = $retour->fetch(PDO::FETCH_OBJ))
{
?>
<tr>
<td><?php echo '<a href="rediger_news.php?modifier_news=' . $donnees->id . '">'; ?>Modifier</a></td>
<td><?php echo '<a href="liste_news.php?supprimer_news=' . $donnees->id . '">'; ?>Supprimer</a></td>
<td><?php echo stripslashes($donnees->titre); ?></td>
<td><?php echo date('d/m/Y', $donnees->timestamp); ?></td>
</tr>
<?php
}
?>
</table>
</body>
</html>
rediger_news.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr" >
<head>
<title>Rédiger une news</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<style type="text/css">
h3, form
{
text-align:center;
}
</style>
</head>
<body>
<h3><a href="liste_news.php">Retour à la liste des news</a></h3>
<?php
require_once 'connect.php';
if (isset($_GET['modifier_news'])) // Si on demande de modifier une news.
{
$retour = $bdd->query('SELECT * FROM mynews WHERE id=\'' . $_GET['modifier_news'] . '\'');
$donnees = $retour->fetch(PDO::FETCH_OBJ);
$titre = stripslashes($donnees->titre);
$contenu = stripslashes($donnees->contenu);
$id_news = $donnees->id;
}
else
{
.
$titre = '';
$contenu = '';
$id_news = 0;
}
?>
<form action="liste_news.php" method="post">
<p>Titre : <input type="text" size="30" name="titre" value="<?php echo $titre; ?>" /></p>
<p>
Contenu :<br />
<textarea name="contenu" cols="50" rows="10">
<?php echo $contenu; ?>
</textarea><br />
<input type="hidden" name="id_news" value="<?php echo $id_news; ?>" />
<input type="submit" value="Envoyer" />
</p>
</form>
</body>
</html>
Trouvez vous mon code cohérent?
Merci pour votre aide ^^