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'); ?>
I use MAMP and phpmyadmin. I hope I didn't miss any important information.
Thanks a lot for the help !