-1

Hey i want to create a minichat just as exercise but for some reason it A) goes to a seperate page after i hit Send and doesnt return and B) does NOT post anything nor does it save anything to the database. Minichat form his here:

 <!DOCTYPE html>
 <html>
 <head>
 <title>Mini-Chat</title>
 <meta charset="UTF-8">
 <style>
 form
 {
 text-align: center;
 }
 </style>
 <body>

 <form action="minichat-post.php" method ="post">
 <p>
    <label for="username">Username</label> : <input type="text"    name="username" id="username"/><br>
    <label for="message">Message</label> : <input type="text" name="message"    id="message"/><br>
    <input type="submit" value="Send"/>
 </p>
 </form>
 <?php
 try
 {
    $bdd = new PDO('mysql:host=localhost;dbname=minichat', 'root', '');
 }
 catch(Exception $e)
 {
    die('Error :'.$e->getMessage());
 }
 $response = $bdd->query('SELECT username, message FROM minichat ORDER BY ID    DESC LIMIT 0, 10');
 while ($data = $response->fetch())
 {
 echo '<p><strong>' . htmlspecialchars($data['username']) . '</strong> : ' .     htmlspecialchars($data['message']) . '</p>';
 }
 $response->closeCursor();
 ?>
 </body>
 </html>

Here is the Post form:

<?php
try
{
$bdd = new PDO('mysql:host=localhost;dbname=minichat', 'root', '');
}
catch(Exception $e)
{
die('Error :'.$e->getMessage());
}

$req = $bdd->prepare('INSERT INTO minichat (username, message VALUES (?, ?)');
$req->execute(array($_POST['username'], $_POST['message']));
header('Location: mini-chat.php');
?>

in my PHP DB i got a 2 fields, username and message, i added a new one saying ID but that did not help either.

  • if you have an error in your php script it will not reach your header and redirect back to mini-chat.php – ambe5960 Oct 30 '15 at 09:46

1 Answers1

0

You missed a closing ')' in INSERT query. Change this line

$req = $bdd->prepare('INSERT INTO minichat (username, message VALUES (?, ?)');

to

$req = $bdd->prepare('INSERT INTO minichat (username, message) VALUES (?, ?)');
Arun Krish
  • 2,153
  • 1
  • 10
  • 15
  • oh gosh eveytime the same, thanks yet the problem that it redirects to another blank page and sets up the minichat there persists as it normaly should stay there where i included it in the file – Patrick Hunter Oct 30 '15 at 09:52
  • I didnt understand your point?Please explain ? – Arun Krish Oct 30 '15 at 09:58
  • For redirection add the full URL : `header('Location: http://mysite/mini-chat.php');` – ThinkTank Oct 30 '15 at 10:00
  • imagin i put the minichat right here next to this comment section. but, whenever you enter yor name and message and press Send, it goes away from this page onto a completely blank area with only the messages and the input fields. thats what happening to me which obviously i dont want as i want everything to remain on this page even after hitting send – Patrick Hunter Oct 30 '15 at 10:00
  • nvm got it, i put the location as minichat.php wich is not the mainpage ofc, just changed it to index.php and now its working as intented, thx for the showing of my "closing" mistake! marked as helpful =) – Patrick Hunter Oct 30 '15 at 10:02