I'm seeking your help for two problems I'm unable to solve. The first one is not truly problematic but rather annoying :
?php
session_start();
$_SESSION['pseudo'];
$CAT="";
//tentative de connexion à la base de donnée
try
{
$bdd = new PDO('mysql:host=localhost;dbname=espace_membre;charset=utf8', 'root', '');
}
catch (Exception $e)
{
die('Erreur : ' . $e->getMessage()); //message d'erreur au cas où la connexion échoue
}
if(isset($_SESSION['id']))
{ //echo "ok";
}
else
{
//echo "lol";
header('location:connexion.php');
}
if(isset($_GET['id']) AND $_GET['id'] > 0)
{
$getid=intval($_GET['id']);
$requser= $bdd -> prepare('SELECT * FROM membres WHERE id= ?');
$requser->execute(array($getid));
$userinfo=$requser->fetch();
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
$CAT = test_input($_POST["categorie"]);
$TYPE= test_input($_POST["typeannonce"]);
$WILA= test_input($_POST["wilaya"]);
$fk_membres_id=$_SESSION['id'];
if(isset($_POST['revenirprofil']))
{
header("Location: profil.php?id=".$_SESSION['id']);
}
if(isset($_POST['article_titre']) AND isset($_POST['article_description']) AND isset($_POST['categorie']) AND isset($_POST['wilaya']) AND isset($_POST['typeannonce']) )
{
$article_titre=htmlspecialchars($_POST['article_titre']);
$article_description=htmlspecialchars($_POST['article_description']);
///insertion dans la BDD /////
$ins=$bdd->prepare('INSERT into articles (titre_article, description,date_publication,catégorie,type_article,wilaya,fk_membres_id) VALUES(?,?, NOW(),?,?,?,?)' );
$ins->execute(array($article_titre,$article_description,$CAT,$TYPE,$WILA,$fk_membres_id));
//header("Location: profil.php?id=".$_SESSION['id']);
}
else
{
$erreurAE = "veuillez remplir tous les champs du formulaire d'ajout";
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Ajouter une annonce</title>
<meta charset="utf-8">
</head>
<body>
<h3>Bonjour <?php echo $_SESSION['pseudo'] ?> ajoutez une annonce TROKI par ici : </h3>
<h3>Bonjour <?php echo $_SESSION['id'] ?> ajoutez une annonce TROKI par ici : </h3>
<h3>Ajoutez une annonce <?php echo $_SESSION['email']?></h3>
<a href="editionprofil.php"> éditer mon profil</a>
<a href="modifiermdp.php">modifier mon mot de passe</a>
<a href="profil.php">mon profil</a>
<a href="deconnexion.php"> se déconnecter</a>
<div align="center">
<form method="POST">
<label>titre de votre annonce</label>
<input type="text" placeholder="titre de votre annonce" name="article_titre" /> <br/>
<label>description de votre annonce</label>
<textarea name = "article_description" rows = "5" cols = "40" placeholder="décrivez votre annonce en insistant sur les mots clés pour attirer le plus de visiteurs possible"> </textarea> <br/>
<label>veuillez seletionner la catégorie de votre article</label>
<input type="radio" name="categorie" value="Livres">Livres
<input type="radio" name="categorie" value="Sport">Sports en tout genre
<br/> <br/>
<label>veuillez seletionner la catégorie de votre article</label>
<input type="radio" name="wilaya" value="25">Constantine
<input type="radio" name="wilaya" value="31">Oran
<br/> <br/>
<td>Que souhaitez-vous faire de votre objet ?:</td> <br/>
<td>
<input type = "radio" name = "typeannonce" value = "vente">vendre seulement
<input type = "radio" name = "typeannonce" value = "échange">troquer seulement
<input type = "radio" name = "typeannonce" value = "indécis">Je suis indécis
</td>
<br/>
<input type="submit" value="envoyer l'article" >
<?php
if (isset($erreur))
{
echo $erreur;
}
?> <br/>
<?php
if (isset($erreur))
{
echo $erreur;
}
?>
<button name="revenirprofil">revenir au profil</button>
</form>
</body>
</html>
Well, the page shows three errors :
Notice: Undefined index: categorie in C:\wamp\www\projet3\formulaireajout.php on line 44 Notice: Undefined index: typeannonce in C:\wamp\www\projet3\formulaireajout.php on line 45 Notice: Undefined index: wilaya in C:\wamp\www\projet3\formulaireajout.php on line 46
but surprisingly, the code still works and the the 3 notices disappear after completing and sending the form to the database. Everything works just fine apart from the mysterious 3 errors which are not truly errors
My second problem may look like a typing error in the SQL query but still not able to find the problem.
I am trying to update the informations sent to the database with the previous form.
Here is the SQL :
$update=$bdd->prepare('UPDATE articles SET titre_article= ?, description=?,catégorie=?,type_article=?,wilaya=?,fk_membres_id=? WHERE id = ?' );
$update->execute(array($article_titre,$article_description,$CAT,$TYPE,$WILA,$fk_membres_id,$fk_membres_id));
die('Edit successful');
I'm getting 'edit successful' but still no changes are being made in my DB. You would normally expect changes to be applied to the desired line but nothing seems to change
Thank you for reading. (hoping it's not something I'm missing in the query)
Its because your php code is getting executed as soon as the page loads irrespective of whether you have submitted the form or not. And until you haven't submitted the form, the $_POST global variable doesn't have access to categorie, typeannoance and wilaya.
And when you submit it, well, those values are accessible by the $_POST global variable and that's why those notices disappear.
Try to check their existence first with isset() function and that should solve your problem