我遵循 PHPAcademy 的注册和登录系列,并构建了一个登录系统。我想为它添加一个公告系统。我已经建立了它,但我无法将登录用户的名字和姓氏存储在表中。消息和日期被插入,但不是用户数据。
姓名、性别、部门等用户详细信息都存储在其他表中,但我只需要两个字段——名字和姓氏,所以我想到了使用会话变量。另外,我在那个 JOIN 概念上很弱,谁能解释一下,我有一种奇怪的感觉,它可以使用它来完成,但我不知道如何,我尝试了两次但失败了(可能是因为我没有得到概念)
请帮忙。以下是代码:
<?php
include $_SERVER["DOCUMENT_ROOT"].'/TestSite/core/init.php';
faculty_protect_page();
include $_SERVER["DOCUMENT_ROOT"].'/TestSite/includes/overall/header.php';
if(empty($_POST)===false) {
$required_fields = array('announce');
foreach($_POST as $key=>$value) {
if(empty($value) && in_array($key, $required_fields) === true ) {
$errors[] = 'It seems like you have not made any announcement!';
break 1;
}
}
if(empty($errors) === true ){
if(strlen($_POST['announce']) < 20) {
$errors[] = 'Your announcement must be atleast 20 characters long!';
}
if(strlen($_POST['announce']) > 250) {
$errors[] = 'Your announcement cannot be more than 250 characters long!';
}
}
}
?>
<center><h3>Make an announcement</h3></center>
<?php
if(isset($_GET['success'])===true && empty($_GET['success'])===true) {
echo 'You have successfully made an announcement';
}
else {
if(empty($_POST)=== false && empty($errors)===true) {
$announcement = array(
'announce' => $_POST['announce'],
//'faculty_fname' => $_SERVER['faculty_fname'] this didn't work
);
announce($announcement);
header('Location: announce.php?success');
exit();
}
else if(empty($errors)===false) {
echo output_errors($errors);
}
?>
<form action="" method="POST">
<br/>
<input type="text" class="span12" name="announce" maxlength="250"/><br/><br/>
<center><input type="submit" class="btn btn" value="Announce" /></center>
</form>
<?php
}
include $_SERVER["DOCUMENT_ROOT"].'/TestSite/includes/overall/footer.php'; ?>
这是放置在函数文件中的我的宣布函数(init.php 需要):
function announce($announcement) {
array_walk($announcement, 'array_sanitize');
$announcement['announce'] = ucfirst($announcement['announce']);
$fields = '`' . implode('`, `', array_keys($announcement)) . '`';
$data = '\'' . implode('\', \'', $announcement) . '\'';
$one = $_SESSION['faculty_fname'];
mysql_query("INSERT INTO `notices` ($fields, `faculty_fname`, `datetime`) VALUES ($data, '$one', NOW())");
}
谁能帮我在表中插入教师的名字?我已经花了一整天的时间,但无法成功。
谢谢你