我是PHP和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>';
}
}
您可以使用隐藏属性type = 'hidden'
,例如auther_id
例如以您的形式
<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="hidden" name="author_id" value="<?php echo $_SESSION['what_ever']; ?>"/>
<input type="submit" class="formbutton" name="formSubmit" value="Send"/>
</form>
注意: <?php echo $_SESSION['what_ever']; ?>
这只是您author_id
可能会做的一个假设
对于date_added,您可以直接在查询中创建添加,而无需通过表单发布
$sql = "INSERT INTO massages (author_id, date_added, title, content) VALUES ('$_POST[author_id]', NOW(), '$_POST[formTitle]', '$_POST[formContent]')";
您还应该避免author_id
通过邮寄发送,而是以这种方式添加
$auther_id = $_SESSION['username'];
$sql = "INSERT INTO massages (author_id, date_added, title, content) VALUES ('$auther_id', NOW(), '$_POST[formTitle]', '$_POST[formContent]')";
重要的
PHP将弃用mysql
必须使用的函数mysqli
为什么我不应该在PHP中使用mysql_ *函数?
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句