我跟着登錄系列從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())");
}
任何人都可以請幫我在插入教師的名字表?我花了整整一天的時間,無法成功。
謝謝
從我看到的情況來看,你的公告功能有點不夠。在調用函數之前,請使用精確的數組鍵對陣列中的所有數據進行組合。不要直接崩潰$ _POST。另外,您可以在$ _SESSION中存儲使用名字和姓氏。另外,您需要在$ _SESSION中存儲唯一的用戶標識以查詢用戶信息。 –