0

I am new to php and I am trying to create a forum. I already managed to create some forum categories in my database and fetch them with php to display them on my website.

Now I am trying to create a "new topic" page that let's users create a topic. The problem is that when I submit the title and the message of the new topic, my database is still empty. It seems like nothing is sent at all to my database. Here is my php code :

<?php

//Connexion à la base de données
require('bd/connexionBDD.php');
//Requete sur la base de donnees des catégories

//Verifie si le formulaire avec toutes les info necéssaire à été envoyé
if(isset($_POST['envoiTopic'])) {
    if(isset($_POST['sujetTopic'], $_POST['commTopic'])) {
        $sujet = htmlspecialchars($_POST['sujetTopic']);
        $contenu = htmlspecialchars($_POST['commTopic']);
        //verification que le sujet et le contenu du message ne soit pas vide
        if(!empty($sujet) and !empty($contenu)) {
            //Ajout des informations à la base de données du topic
            $insert = $bdd->prepare('INSERT INTO topic (titre, contenu, date_creation) VALUES(?,?,NOW())');
            $insert->execute(array($sujet,$contenu));
        } else {
            $erreurTopic = "Veuillez écrire un sujet et un message";
        }
        
    }
}
?>

and my form code :

  <div class=container>
    <form class="creerTopic" method="POST">
        <table class="newTopic">
            <tr class="enTete">
                <th class="corps">Nouveau Topic</th>
            </tr>
            <tr>
                <td>Sujet</td>
                <td><input type="text" name="sujetTopic"/></td>
            </tr>
            <tr>
                <td>Message</td>
                <td><textarea name="commTopic"></textarea></td>
            </tr>
            <tr>
                <td><input type="submit" name="envoiTopic"></td>
            </tr>
            <?php if(isset($erreurTopic)) { ?>
            <tr>
                <td><?= $erreurTopic ?></td>
            </tr>

            <?php } ?>
           
        </table>
    </form>
</div>

My "ConnexionBDD.php" file just has <?php $bdd = new PDO('mysql:host=localhost;dbname=forum', 'root', 'root'); ?>

My database looks like this : enter image description here

I use MAMP and phpmyadmin. I hope I didn't miss any important information.

Thanks a lot for the help !

  • You are using PDO? – Professor Abronsius Mar 21 '21 at 13:49
  • Your script is open to [SQL Injection Attack](http://stackoverflow.com/questions/60174). Even [if you are escaping inputs, its not safe!](http://stackoverflow.com/questions/5741187) You should alway use [prepared parameterized statements](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) in either the `MYSQLI_` or `PDO` API's instead of concatenating user provided values into the query. Never trust ANY user input! – RiggsFolly Mar 21 '21 at 13:50
  • Since I already manage to retrieve informations from my database does that imply that i use PDO or i could still be not using it? I'm totally new and i don;t know how to be sure i'm using it properly – Michel Pinto Mar 21 '21 at 13:51
  • the above looks more or less OK as far as I can tell. Looking at the way you call the `execute` method suggests that you are using PDO instead of `mysqli` but can you confirm this by checking the connection in `connexionBDD.php` ? – Professor Abronsius Mar 21 '21 at 13:55
  • My connexionBDD.php is like this : `` And that's it just a connection to my database – Michel Pinto Mar 21 '21 at 13:58
  • The `topic` table has a `id_categories` column which does not allow NULL values yet your SQL does not insert a suitable value for that. the same appears ttrue for the `id_pseudo` column too. Supply a suitable ID for these columns or use a default value set in the table – Professor Abronsius Mar 21 '21 at 14:05
  • It worked ! i just set the values on the default column to NULL and it worked. Thanks a lot, you're the best. – Michel Pinto Mar 21 '21 at 14:12
  • pleasure - glad that it helped! – Professor Abronsius Mar 21 '21 at 14:13

0 Answers0