I followed the register & login series from PHPAcademy and I built a login system. I wanted to add an announcement system to it. I have built that, but I am not able to store the first name and last name of the user logged-in in the table. The message and dates are getting inserted but the not the userdata.
The user details like name, sex, dept all are stored in other table but I want only two fields – first name and last name, so I thought of using session variables. Also, I am weak in that JOIN concept, can anyone please explain me, I have a strange feeling it can be done using that but I don’t know how, I tried two times but failed (maybe because I don’t get the concept)
Please help. Here are the codes:
<?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'; ?>
Here is my announce function placed in a functions file (required by 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())");
}
Can anyone please help me inserting the first name of faculty in the table? I have spent one whole day on this and couldn’t succeed.
Thank You
If you want to store the user information in the notices record, you want to store for example the primary key from the user table that corresponds to the user who is announcing the notice.
Your notices table will have to have a user_id column and the id’s that will be stored in this column are often referred to as foreign keys. In this way you are not storing duplicate information in your database. All the user specific information is already stored in the user_table, so there is no need to store it again in the notices table, just a means to connect a user to a notice.
To work this into your current code you might do something along the lines of the following.
When you want to insert a new notice in the notices table you first need to retrieve the user record from the database for the person that is submitting the notice. How you do this depends on how your login system is built, but for example, on login you could store the primary key from the user in the session. On notice submit, you retrieve the user id from the session and use it to query the database. In semi-pseudocode:
Now that we have the record of the logged in user you can insert the notice in the notices table
After you execute the query, the new record contains the user_id in the user column of table notices that corresponds to the primary key in the user_table where all the user spec ific information can be found.