I'm quite new in PHP and MySQL. I tried to make message board where user can post some message on wall and the every logged user can read it.
Now when someone add message doesn't write in table author_id and date_added. I need them when results are displayed.
Here is new.php
if(isset($_POST['formSubmit']))
{
$errorMessage = "";
if(empty($_POST['formTitle']))
{
$errorMessage .= "<li>Doesn't have title!</li>";
}
if(empty($_POST['formContent']))
{
$errorMessage .= "<li>The field for content is empty!</li>";
}
if(empty($errorMessage))
{
$db = mysql_connect("localhost","root","");
if(!$db) die("Error connecting to MySQL database.");
mysql_select_db("homework3" ,$db);
$sql = "INSERT INTO massages (author_id, date_added, title, content) VALUES ('$_POST[author_id]', '$_POST[date_added]', '$_POST[formTitle]', '$_POST[formContent]')";
mysql_query($sql);
header("Location: index.php");
exit();
}
}
<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
<div><label for='formTitle'>Title<input type="text" name="formTitle" value="" style="width: 350px;"></label></div></br>
<div><label for='formContent'>Content</div><textarea name="formContent" style="width: 344px; height: 100px;"></textarea>
<input type="submit" class="formbutton" name="formSubmit" value="Send"/>
</form>
Edit: I don't know if you need this but this is how I display massages:
$sql = 'SELECT username, msg_id, title, content, date_added FROM massages as m, users as u WHERE author_id = user_id ORDER BY m.date_added DESC';
$result = mysqli_query($link, $sql);
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
$real_date = date('d.m.Y', $row['date_added']);
echo '<table>
<tr>
<td>' . $row['msg_id'] . '. ' . $row['title'] . '</td>
</tr>
<tr>
<td>' . $row['content'] . '</td>
</tr>
<tr>
<td>By<span style="color: #CC0033;">' . $row['username'] . '</span> on <span style="color: #CC0033;">' . $real_date . '</span></td></br>
</tr>
</table>';
}
}