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 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
| <?php
/* @var $bdd PDO */
session_start();
//pensÈ l'activation des exceptions
require_once('config/bdd.conf.php');
require_once('config/init.conf.php');
require_once('config/connexion.inc.php');
//empty inclue isset
// utilises directement les valeurs GET
//car je ne fais aucune modification sur les donnÈes
//et en plus je ne les utilises pas
//utilises === pour typage strict
//et !empty()
if (!empty($is_connect) && $is_connect === TRUE) {
if (isset($_POST['submit'])) {
$action = $_POST['submit'];
if ($_FILES['image']['error'] == 0) {
$notification = "aucune notification";
$date_du_jour = date("Y-m-d");
if (!empty($_POST['titre']) && !empty($_POST['texte'])) {
$publie = isset($_POST['publie']) ? $_POST['publie'] : 0;
var_dump($publie);
//ajout et modif
switch ($action) {
case "Ajouter":
$requete = "INSERT INTO articles (titre, texte, date, publie) ";
$requete .= "VALUES (:titre, :texte, :date, :publie)";
$sth = $bdd->prepare($requete);
$sth->bindValue(':titre', $_POST['titre'], PDO::PARAM_STR);
$sth->bindValue(':texte', $_POST['texte'], PDO::PARAM_STR);
$sth->bindValue(':date', $date_du_jour, PDO::PARAM_STR);
$sth->bindValue(':publie', $publie, PDO::PARAM_BOOL);
break;
case "modifier":
$requete = "UPDATE articles SET titre = :titre, texte = :texte, publie = :publie WHERE id = :id";
$sth = $bdd->prepare($requete);
$sth->bindValue(':titre', $_POST['titre'], PDO::PARAM_STR);
$sth->bindValue(':texte', $_POST['texte'], PDO::PARAM_STR);
$sth->bindValue(':publie', $publie, PDO::PARAM_BOOL);
//$id => $_GET['id'] directement
$sth->bindValue(':id', $_GET['id'], PDO::PARAM_INT);
break;
}
if ($sth->execute() === TRUE) {
if ($action = "Ajouter") {
$id_article = $bdd->lastInsertId();
$extension = pathinfo($_FILES['image']['name'], PATHINFO_EXTENSION);
} else {
$id_article = $_GET['id'];
}
echo $action;
move_uploaded_file($_FILES['image']['tmp_name'], 'img/' . $id_article . '.' . $extension);
$notification = "BDD upload";
$_SESSION['notification_result'] = TRUE;
//if ($sth->execute()
} else {
$notification = "erreur dans l'insertion";
$_SESSION['notification_result'] = FALSE;
}
// if (!empty($_POST['titre'])
} else {
$notification = "veuillez renseigner les champs obligatoires.";
$_SESSION['notification_result'] = FALSE;
}
}
// if ($_FILES['image']['error'] == 0)
else {
$notification = "erreur de traitement de l'image";
$_SESSION['notification_result'] = FALSE;
}
$_SESSION['notification'] = $notification;
header('Location:articles.php');
exit();
} else {
//else de if POST['submit']
include('include/header.inc.php');
if ($action == "modifier") {
$sql = "SELECT * FROM articles WHERE id = :id";
//echo $sql;
$sth = $bdd->prepare($sql);
$sth->bindValue(':id', $_GET['id'], PDO::PARAM_INT);
if ($sth->execute() === TRUE) {
$tab_articles_modifier = $sth->fetchAll(PDO::FETCH_ASSOC);
} else {
echo "Une erreur est apparu";
}
foreach ($tab_articles_modifier as $value) {
$publie = $value['publie'];
$titre = $value['titre'];
$texte = $value['texte'];
}
} else {
$publie = 0;
}
?>
<link rel="stylesheet" href="css/style.css" />
<div class="container col-md-6">
<div class="row justify-content-center " >
<h1 class="mt-4"><?= $action ?> un article </h1>
</div><br>
<?php
if (isset($_SESSION['notification'])) {
$notification_result = $_SESSION['notification_result'] == TRUE ? 'success' : 'danger';
?>
<div class="alert alert-<?= $notification_result ?> alert-dismissible fade show col-md-6" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
<strong>
<?php
echo $_SESSION['notification'];
?>
</strong>
</div>
<?php
unset($_SESSION['notification']);
unset($_SESSION['notification_result']);
}
?>
<form action="articles.php" method="POST" enctype="multipart/form-data" >
<div class="form-group">
<label for="titre" class="col-form-label">titre</label>
<input type="text" class="form-control" id="titre" name="titre" value="<?= isset($titre) ? $titre : "Indiquer un titre"; ?>">
</div>
<div class="form-group">a
<label for="texte" class="col-form-label">texte</label>
<textarea type="text" class="form-control" id="texte" name="texte" rows="6" ><?= isset($texte) ? $texte : "Indiquer un texte"; ?></textarea>
</div>
<div class="form-group">
<label class="custom-file">
<input type="file" id="image" class="custom-file-input" name="image" >
<span class="custom-file-control"></span>
</label>
<?php
if ($action == "modifier") {
?>
<img src="img/<?php echo $value['id'] ?>.png" alt="" width="200px">
<?php
} else {
}
//echo $id;
?>
</div>
<form class="was-validated">
<label class="form-check-label">
<input class="form-check-input" type="checkbox" id="publie" value="1" <?php if ($publie == 1) { ?> checked <?php } ?> name="publie"> Publie
<input type="hidden" name="id" value="<?= $id ?>" />
</label>
<div class="form-group">
<button type="submit" class="btn btn-primary" name="submit" value="<?= $action ?>"><?= $action ?></button>
</div>
</form>
</div>
<!-- Bootstrap core JavaScript -->
<script src="vendor/jquery/jquery.min.js"></script>
<script src="vendor/popper/popper.min.js"></script>
<script src="vendor/bootstrap/js/bootstrap.min.js"></script>
<script src="js/dist/jquery.validate.min.js"></script>
<?php
include ('include/footer.inc.php');
}
} else {
header("Location: connexion.php");
}
?> |
Partager