我是PHP和MySQL中的新手。我試圖製作留言板,用戶可以在牆上發佈一些消息,每個登錄的用戶都可以閱讀。如何將日期和user_id插入到mysql
現在當有人添加消息不寫入表author_id和date_added。顯示結果時我需要它們。
這裏是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>
編輯: 我不知道你是否需要這一點,但我這是怎麼顯示的按摩:
$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>';
}
}
[請不要使用mysql_ *函數](在新代碼中使用mysql_ *函數)(http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php)。他們不再被維護,並[正式棄用](https://wiki.php.net/rfc/mysql_deprecation)。請改爲了解準備好的語句,並使用[pdo](https://wiki.php.net/rfc/mysql_deprecation)或[mysqli](http://stackoverflow.com/questions/tagged/mysqli)。 – zessx
感謝您的留言。 – Goro
@Goro在會話中保存了author_id嗎? – zzlalani