Please bare with me as English is not my native language. I will try the best I can to formulate a cohesive question the best I can.
I have created a the beginning of a website that I will use to manage tenants in a buildings that I manage. I am at the point where I had to make a decision to split my table and build normalized tables. I use the users table to get a user to sign up and login with personal information. the second phase of my project is to offer an area where prospect tenants can submit an application to rent a unit in my building. I have designed roughly 7 tables so far. I will need more in the future because I am thinking of scalability ahead of time and the more I normalize my tables the easier it will be for me. I know it can affect the performance of my database but since I dont ever expect to have millions of applicant and registered tenants on my website I think it will work fine.
so that's the background of my project.
my question:
bare in mind I am new at writing php codes.
I have copied my user profile setting page and modified it to build the first page of the application which is called "Employment" see below.
Code: Select all
<?php
include_once 'core/init.php';
$general->logged_out_protect();
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link rel="stylesheet" type="text/css" href="css/style.css" >
<title>Employment Info</title>
</head>
<body>
<div id="container">
<?php include 'includes/menu.php'; ?>
<?php
if (isset($_GET['success']) && empty($_GET['success'])) {
echo '<h3>Your details have been updated!</h3>';
} else{
if(empty($_POST) === false) {
if (isset($_POST['users_employer']) && !empty ($_POST['users_employer'])){
if (ctype_alpha($_POST['users_employer']) === false) {
$errors[] = 'Please enter your First Name with only letters!';
}
}
if (isset($_POST['users_emp_supervisor']) && !empty ($_POST['users_emp_supervisor'])){
if (ctype_alpha($_POST['users_emp_supervisor']) === false) {
$errors[] = 'Please enter your Last Name with only letters!';
}
}
if (isset($_POST['users_emp_working']) && !empty($_POST['users_emp_working'])) {
$workingyesno = array('undisclosed', 'yes', 'no');
if (in_array($_POST['users_emp_working'], $workingyesno) === false) {
$errors[] = 'Please select yes or no from the list';
}
}
if (isset($_POST['users_emp_phone']) && !empty ($_POST['users_emp_phone'])){
if (ctype_digit($_POST['users_emp_phone']) === false) {
$errors[] = 'Please enter your employer phone number, numbers only!';
}
}
if (isset($_POST['users_emp_email']) && !empty ($_POST['users_emp_email'])){
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",($_POST['users_emp_email'])) === false) {
$errors[] = 'Please enter a valid email address!';
}
}
if (isset($_POST['users_emp_start']) && !empty ($_POST['users_emp_start'])){
if (ctype_digit($_POST['users_emp_start']) === false) {
$errors[] = 'Please enter yyyy-mm-dd format for when you started working!';
}
}
if (isset($_POST['users_emp_end']) && !empty ($_POST['users_emp_end'])){
if (ctype_digit($_POST['users_emp_end']) === false) {
$errors[] = 'Please enter yyyy-mm-dd format for when you ended working!';
}
}
if (isset($_POST['users_emp_position']) && !empty ($_POST['users_emp_position'])){
if (ctype_alpha($_POST['users_emp_position']) === false) {
$errors[] = 'Please enter position title while using only letters!';
}
}
if (isset($_POST['users_emp_salary']) && !empty ($_POST['users_emp_salary'])){
if (ctype_digit($_POST['users_emp_salary']) === false) {
$errors[] = 'Please enter numbers only without "\$"!';
}
}
/*if (isset($_FILES['myfile']) && !empty($_FILES['myfile']['name'])) {
$name = $_FILES['myfile']['name'];
$tmp_name = $_FILES['myfile']['tmp_name'];
$allowed_ext = array('jpg', 'jpeg', 'png', 'gif' );
$a = explode('.', $name);
$file_ext = strtolower(end($a)); unset($a);
$file_size = $_FILES['myfile']['size'];
$path = "avatars";
if (in_array($file_ext, $allowed_ext) === false) {
$errors[] = 'Image file type not allowed';
}
if ($file_size > 2097152) {
$errors[] = 'File size must be under 2mb';
}
} else {
$newpath = $user['image_location'];
}
if(empty($errors) === true) {
if (isset($_FILES['myfile']) && !empty($_FILES['myfile']['name']) && $_POST['use_default'] != 'on') {
$newpath = $general->file_newpath($path, $name);
move_uploaded_file($tmp_name, $newpath);
}else if(isset($_POST['use_default']) && $_POST['use_default'] === 'on'){
$newpath = 'avatars/default_avatar.png';
}*/
$users_employer = htmlentities(trim($_POST['users_employer']));
$users_emp_supervisor = htmlentities(trim($_POST['users_emp_supervisor']));
$users_emp_working = htmlentities(trim($_POST['users_emp_working']));
$users_emp_phone = htmlentities(trim($_POST['users_emp_phone']));
$users_emp_email = htmlentities(trim($_POST['users_emp_email']));
$users_emp_start = htmlentities(trim($_POST['users_emp_start']));
$users_emp_end = htmlentities(trim($_POST['users_emp_end']));
$users_emp_position = htmlentities(trim($_POST['users_emp_position']));
$users_emp_salary = htmlentities(trim($_POST['users_emp_salary']));
$users_id = $_SESSION['id']; // I added this line myself (not sure if this is correct)
//$image_location = htmlentities(trim($newpath));
$users->Update_employment($users_employer, $users_emp_supervisor, $users_emp_working, $users_emp_phone, $users_emp_email, $users_emp_start, $users_emp_end, $users_emp_position, $users_emp_salary, $users_id, $id);//$user_id needs to be defined in my database or use alias
header('Location: settings.php?success');
exit();
} else if (empty($errors) === false) {
echo '<p>' . implode('</p><p>', $errors) . '</p>';
}
}
?>
<h2>Employment Information.</h2> <p><b>Note: Information posted are legally binding. </b></p>
<hr />
<form action="" method="post" enctype="multipart/form-data">
<div id="profile_picture">
<h3>Change Profile Picture</h3>
<ul>
<?php /*
if(!empty ($user['image_location'])) { //fix: pictures for tenants and selection UI display list and max number of pic
$image = $user['image_location'];
echo "<img src='$image'>";
}
?>
<li>
<input type="file" name="myfile" />
</li>
<?php if($image != 'avatars/default_avatar.png'){ ?>
<li>
<input type="checkbox" name="use_default" id="use_default" /> <label for="use_default">Use default picture</label>
</li>*/
//<?php
//}
?>
</ul>
</div>
<div id="personal_info">
<h3 >Change employment Information </h3>
<ul>
<li>
<h4>Employer:</h4>
<input type="text" name="users_employer" value="<?php if (isset($_POST['users_employer']) )
{echo htmlentities(strip_tags($_POST['users_employer']));} else { echo $user['users_employer']; }?>">
</li>
<li>
<h4>Supervisor Name: </h4>
<input type="text" name="users_emp_supervisor" value="<?php if (isset($_POST['users_emp_supervisor']) )
{echo htmlentities(strip_tags($_POST['users_emp_supervisor']));} else { echo $user['users_emp_supervisor']; }?>">
</li>
<li>
<h4>Are you Working:</h4>
<?php
$users_emp_working = $user['users_emp_working'];
$options = array("undisclosed", "yes", "no");
echo '<select name="users_emp_working">';
foreach($options as $option){
if($users_emp_working == $option){
$sel = 'selected="selected"';
}else{
$sel='';
}
echo '<option '. $sel .'>' . $option . '</option>';
}
?>
</select>
</li>
<li>
<h4>Employer Phone:</h4>
<input type="text" name="users_emp_phone" value="<?php if (isset($_POST['users_emp_phone']) )
{echo htmlentities(strip_tags($_POST['users_emp_phone']));} else { echo $user['users_emp_phone']; }?>">
</li>
<li>
<h4>Employer email:</h4>
<input type="text" name="users_emp_email" value="<?php if (isset($_POST['users_emp_email']) )
{echo htmlentities(strip_tags($_POST['users_emp_email']));} else { echo $user['users_emp_email']; }?>">
</li>
<li>
<h4>Employment Start Date:</h4>
<input type="text" name="users_emp_start" value="<?php if (isset($_POST['users_emp_start']) )
{echo htmlentities(strip_tags($_POST['users_emp_start']));} else { echo $user['users_emp_start']; }?>">
</li>
<li>
<h4>Employment End Date:</h4>
<input type="text" name="users_emp_end" value="<?php if (isset($_POST['users_emp_end']) )
{echo htmlentities(strip_tags($_POST['users_emp_end']));} else { echo $user['users_emp_end']; }?>">
</li>
<li>
<h4>Position / title:</h4>
<input type="text" name="users_emp_position" value="<?php if (isset($_POST['users_emp_position']) )
{echo htmlentities(strip_tags($_POST['users_emp_position']));} else { echo $user['users_emp_position']; }?>">
</li>
<li>
<h4>Salary:</h4>
<input type="text" name="users_emp_salary" value="<?php if (isset($_POST['salary']) )
{echo htmlentities(strip_tags($_POST['users_emp_salary']));} else { echo $user['users_emp_salary']; }?>">
</li>
</ul>
</div>
<div class="clear"></div>
<hr />
<span>Update Changes:</span>
<input type="submit" value="Update">
</form>
</div>
</body>
</html>
<?php
//}
Code: Select all
<?php
class Users{
private $db;
public function __construct($database) {
$this->db = $database;
}
public function update_user($first_name, $last_name, $gender, $bio, $image_location, $id){
$query = $this->db->prepare("UPDATE `users` SET
`first_name` = ?,
`last_name` = ?,
`gender` = ?,
`bio` = ?,
`image_location`= ?
WHERE `id` = ?
");
$query->bindValue(1, $first_name);
$query->bindValue(2, $last_name);
$query->bindValue(3, $gender);
$query->bindValue(4, $bio);
$query->bindValue(5, $image_location);
$query->bindValue(6, $id);
try{
$query->execute();
}catch(PDOException $e){
die($e->getMessage());
}
}
public function Update_employment($users_employer, $users_emp_supervisor, $users_emp_working, $users_emp_phone, $users_emp_email,$users_emp_start,$users_emp_end, $users_emp_position, $users_emp_salary,$users_id){
$users_id = $_SESSION['id'];
$query = $this->db->prepare("UPDATE `users_emp` SET
`users_employer` = ?,
`users_emp_supervisor` = ?,
`users_emp_working` = ?,
`users_emp_phone` = ?,
`users_emp_email` = ?,
`users_emp_start` = ?,
`users_emp_end` = ?,
`users_emp_position` = ?,
`users_emp_salary` = ?,
`users_id` = ?
WHERE `users_id` = ?
"); // the users_id above originate from users_emp table. it is the foreign key to users table.
$query->bindValue(1, $users_employer);
$query->bindValue(2, $users_emp_supervisor);
$query->bindValue(3, $users_emp_working);
$query->bindValue(4, $users_emp_phone);
$query->bindValue(5, $users_emp_email);
$query->bindValue(6, $users_emp_start);
$query->bindValue(7, $users_emp_end);
$query->bindValue(8, $users_emp_position);
$query->bindValue(9, $users_emp_salary);
$query->bindValue(10, $users_id);
$query->bindValue(11, $id);
try{
$query->execute();
}catch(PDOException $e){
die($e->getMessage());
}
}
every single field has teh same error message but with a different line of course.<br /><b>Notice</b>: Undefined index: users_employer in <b>C:\xampp\htdocs\lar\personalinfo.php</b> on line <b>169</b><br />
I have tried to update the form and I am able to connect with the database because I get a "success" message. the thing is I unfortunately am not saving anything to the database since I have undefined index problem with all my input field.
I am also trying to get the userid to copy itself into the foreign key users_id in my users_emp table.
can see where I might not be going at it the right way?