-2

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. screen shot of the DB structure

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)

user3783243
  • 5,368
  • 5
  • 22
  • 41
Kouci
  • 3
  • 4
  • `catégorie` is not a valid column name unquoted. Use backticks on it or change the name. Get rid of `test_input` and stop using whatever tutorial you picked that up from. You should look at https://stackoverflow.com/questions/3999850/pdo-error-message so you can get errors from PDO. – user3783243 Jul 05 '20 at 03:43
  • The first error is because you don't check that indexs are set before using them. – user3783243 Jul 05 '20 at 03:48
  • Does this answer your question? ["Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset" using PHP](https://stackoverflow.com/questions/4261133/notice-undefined-variable-notice-undefined-index-and-notice-undefined) – user3783243 Jul 05 '20 at 03:48
  • Hi ! I do not have any PDO errors. The SQL query is being executed without apparent changes on my DB. And if you have a better alternative for that 'test_input' I'd be happy to take it ! :) Thanks for the link – Kouci Jul 05 '20 at 03:53

1 Answers1

0

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

Tushar
  • 665
  • 5
  • 11
  • Thank you for your answer, Tushar. But it seems like the problem persists. – Kouci Jul 05 '20 at 03:28
  • Can you please share the code snippet where you have made changes ? – Tushar Jul 05 '20 at 03:30
  • Sure ! :) ` – Kouci Jul 05 '20 at 03:34
  • Everything seems fine to me. Maybe you can trying checking for form submission before doing any of this :) – Tushar Jul 05 '20 at 03:39
  • Yeah, I have to admit I did only try two or three ideas on this one before asking on Stackoverflow. Gonna check this in depth later. Thank you for your precious time, Tushar ! – Kouci Jul 05 '20 at 03:42
  • Well, for all PHP newbies like me, It seems that declaring the variables like this solves the problem : `$_POST["categorie"]=''; $_POST["typeannonce"]=''; $_POST["wilaya"]=''; ` And thanks to Tushar and user3783243 for your help. – Kouci Jul 05 '20 at 04:46