How to check and uncheck a checkbox field by boolean value? - php

I want to update my Users table, and activate or deactivate a user. But i have problem in that.
I am trying to get a boolean value from Access database into checkbox or radio button. I can't do it. I want if the boolean value is 1 the checkbox should be checked if it's 0 the checkbox should be unchecked.
The same problem for User role I want to display it's current role if necessary update it by selecting Admin or User
Then if I check, it should register 1 or true to the database if unchecked register 0 to database.
Here is my code:
// PHP
include_once("../dbConnection.php");
// here i get a user by it's ID to update
$userId = $_GET['id'];
$sql = "SELECT * FROM Users WHERE User_ID = $userId ; ";
$result = $pdo->query($sql);
if (isset($_POST['update'])) {
$username = $_POST['username'];
$password = hash('sha256', $_POST['password']);
$email = $_POST['email'];
$role = $_POST['role'];
$isActive = $_POST['isActive'];
$editSql = "UPDATE Users
SET Login='$username', Password='$password', Email='$email', Type='$role', UserActive='$isActive'
WHERE User_ID=$userId;";
if ($pdo->query($editSql)) {
echo "Record successfully registered ! <br />";
header('location: ../index.php');
} else {
echo "Could not register ! <br>";
}
}
foreach ($result->fetchAll() as $row) {
}
//MY FORM
<form action="" class="container" method="POST">
<h3 class="text-center mt-0 mb-3">Updating User ! </h3>
<div class="row mt-3">
<div class="col-md-6">
<label class="form-label">Username </label>
<input type="text" class="form-control" name="username" value="<?= $row['Login'] ?>" required autofocus>
</div>
<div class="col-md-6">
<label class="form-label" for="">Password </label>
<input type="password" class="form-control" name="password" value="<?= $row['Password'] ?>" required>
</div>
</div>
<div class="row mt-3">
<div class="col-md-6">
<label class="form-label" for="">Email Address </label>
<input type="email" class="form-control" name="email" value="<?= $row['Email'] ?>">
</div>
<div class="col-md-6">
<label class="form-label" for="">Select User Type </label>
<select name="role" class="form-select" required>
<!-- <option>Choose a Role</option> -->
<option name="user" value="<?= $row['Type'] ?>" required>User</option>
<option name="user" value="2" required>User</option>
<option name="admin" value="1">Admin</option>
</select>
</div>
</div>
<div class="row mt-3">
<div class="col-md-6">
<label class="form-label" for="">Is Active </label> <br>
<?php if ($row['UserActive'] == 1) { ?>
YES: <input type="checkbox" name="isActive" value="1" checked>
<?php } else { ?>
NO: <input type="checkbox" name="isActive" value="0">
<?php } ?>
</div>
</div>
<div class="row">
<div class="col">
<button type="submit" class="btn btn-primary mt-2" name="update"> Update ! </button>
</div>
</div>
</form>

Related

How can I update user's data by clicking on the edit button?

Using php, I want to make the Edit button in register.php to display the form which shows fields that have the already existing user's data and then admin can update the data. I'm about to go crazy because of this thing it's been the third day in row not moving to the second step! I've watched many YT tutorials and explored many solutions here and in google as well, but couldn't actually figure out how to fix mine! Please if you know where my mistake is and what should I edit, do tell me and I will be more than happy to fix it.
These are my files:
register.php
<?php
session_start();
include('includes/header.php');
include('includes/navbar.php');
?>
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#addadminprofile">
Add Admin Profile
</button>
<!-- Modal -->
<div class="modal fade" id="addadminprofile" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Add Admin Data</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<form action="code.php" method="POST">
<div class="modal-body">
<div class="form-group">
<label>Username </label>
<input type="text" name="username" class="form-control" required>
</div>
<div class="form-group">
<label>Full name </label>
<input type="text" name="full_name" class="form-control" required>
</div>
<div class="form-group">
<label>Email </label>
<input type="email" name="email" class="form-control" required>
</div>
<div class="form-group">
<label>Password </label>
<input type="password" name="password" class="form-control" required>
</div>
<div class="form-group">
<label>Confirm password </label>
<input type="password" name="cpassword" class="form-control" required>
</div>
<div class="form-group">
<label>Gender </label>
<input type="radio" name="gender" tabindex="1" value="Male">
<span> Male</span>
<input type="radio" name="gender" tabindex="2" value="Female">
<span> Female</span>
</div>
<div class="form-group">
<label>Age </label>
<input type="text" name="age" class="form-control" required>
</div>
<div class="form-group">
<label>Phone number </label>
<input type="text" name="phone_number" class="form-control" required>
</div>
<div class="form-group">
<label>Work durationr </label>
<input type="text" name="work_duration" class="form-control" required>
</div>
<div class="form-group">
<label >Food Category:</label>
<select name="food_category" class="form-control" required>
<option value="hot_cold_dishes">Hot/Cold Dishes</option>
<option value="desserts">Desserts</option>
<option value="drinks">Drinks</option>
</select>
</div>
<div class="form-group">
<label>IG Account </label>
<input type="text" name="ig_account" class="form-control" required>
</div>
<div class="form-group">
<label >State:</label>
<select name="state" class="form-control" required>
<option value="1">First</option>
<option value="2">Second</option>
<option value="3n">Third</option>
</select>
</div>
<div class="form-group">
<label>Postcode </label>
<input type="text" name="postcode" class="form-control" required>
</div>
<div class="form-group">
<label>City </label>
<input type="text" name="city" class="form-control" required>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
<button type="Submit" name="submit" class="btn btn-primary">Register</button>
</div>
</form>
</div>
</div>
</div>
<?php
/*
if(isset($_SESSION['success']) && $_SESSION['success'] != ''){
echo '<h2>'.$_SESSION['success'].'</h2>';
unset ($_SESSION['success']);
}
if(isset($_SESSION['status']) && $_SESSION['status'] != ''){
echo '<script>alert('.$_SESSION['status'].')</script>';
unset ($_SESSION['status']);
}
*/
?>
<div class="card shadow">
<div class="card-body">
<div class="table-responsive">
<?php
$connection = mysqli_connect("localhost", "root", "", "finalproject") or die ("<script>alert('Connection Failed.')</script>");
$sql = "SELECT * FROM users";
$result = mysqli_query($connection, $sql);
?>
<table class="table table-borderd" id="dataTable" width="30%" cellspecing="0">
<thead>
<tr>
<th>Username</th>
<th>Full Name</th>
<th>Email</th>
<th>Password</th>
<th>Age</th>
<th>Gender</th>
<th>Phone Number</th>
<th>Work Duration</th>
<th>Food Category</th>
<th>IG Account</th>
<th>State</th>
<th>Postcode</th>
<th>City</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
<?php
if(mysqli_num_rows($result)>0){
while($row = mysqli_fetch_assoc($result)){
?>
<tr>
<td><?php echo $row['username']?></td>
<td><?php echo $row['full_name']?></td>
<td><?php echo $row['email']?></td>
<td><?php echo $row['password']?></td>
<td><?php echo $row['phone_number']?></td>
<td><?php echo $row['work_duration']?></td>
<td><?php echo $row['food_category']?></td>
<td><?php echo $row['ig_account']?></td>
<td><?php echo $row['state']?></td>
<td><?php echo $row['postcode']?></td>
<td><?php echo $row['city']?></td>
<td><?php echo $row['gender']?></td>
<td><?php echo $row['age']?></td>
<td>
<form action="first_register.php" method="POST">
<input type="hidden" name="edit_username" value="<?php echo $row['username'];?>">
<button type="submit" name="edit_button" class="btn btn-success">Edit</button>
</form>
</td>
<td>
<button type="submit" class="btn btn-danger">Delete</button>
</td>
</tr>
<?php
}
}
else{
echo "No record found";
}
?>
</tbody>
</table>
</div>
</div>
<?php
include('includes/script.php');
include('includes/footer.php');
?>
first_register.php
<?php
$connection = mysqli_connect("localhost", "root", "", "finalproject")or die ("<script>alert('Connection Failed.')</script>");
session_start();
include('includes/header.php');
include('includes/navbar.php');
?>
<div class="container-fluid">
<!-- DataTales Example -->
<div class="card shadow mb-4">
<div class="card-header py-3">
<h6 class="m-0 font-weight-bold text-primary"> EDIT Admin Profile </h6>
</div>
<div class="card-body">
<?php
if(isset($_POST['edit_button']))
{
$username = $_POST['username'];
$query = "SELECT * FROM register WHERE username='$username' ";
$query_run = mysqli_query($connection, $query);
foreach($query_run as $row)
{
?>
<form action="code.php" method="POST">
<input type="hidden" name="edit_username" value="<?php echo $row['username'] ?>">
<div class="form-group">
<label> Full name </label>
<input type="text" name="edit_full_name" value="<?php echo $row['full_name'] ?>" class="form-control">
</div>
<div class="form-group">
<label>Email</label>
<input type="email" name="edit_email" value="<?php echo $row['email'] ?>" class="form-control"
placeholder="Enter Email">
</div>
<div class="form-group">
<label>Password</label>
<input type="password" name="edit_password" value="<?php echo $row['password'] ?>"
class="form-control" placeholder="Enter Password">
</div>
<div class="form-group">
<label>Gender</label>
<input type="radio" name="edit_gender" value="Male" class="form-control">
<span> Male</span>
<input type="radio" name="edit_gender" value="Female" class="form-control">
<span> Female</span>
</div>
<div class="form-group">
<label>Age</label>
<input type="text" name="edit_age" value="<?php echo $row['age'] ?>"
class="form-control">
</div>
<div class="form-group">
<label>Phone Number</label>
<input type="text" name="edit_phone_number" value="<?php echo $row['phone_number'] ?>"
class="form-control">
</div>
<div class="form-group">
<label>Work Duration</label>
<input type="text" name="edit_work_duration" value="<?php echo $row['work_duration'] ?>"
class="form-control">
</div>
<div class="form-group">
<label>Food Category</label>
<select name="food_category" class="form-control" required>
<option value="hot_cold_dishes">Hot/Cold Dishes</option>
<option value="desserts">Desserts</option>
<option value="drinks">Drinks</option>
</select>
</div>
</div>
<div class="form-group">
<label>IG Account</label>
<input type="text" name="edit_ig_account" value="<?php echo $row['ig_account'] ?>"
class="form-control">
</div>
<div class="form-group">
<label>State</label>
<select name="state" class="form-control" required>
<option value="1">First</option>
<option value="2">Second</option>
<option value="3n">Third</option>
</select>
</div>
<div class="form-group">
<label>Postcode</label>
<input type="text" name="edit_postcode" value="<?php echo $row['postcode'] ?>"
class="form-control">
</div>
<div class="form-group">
<label>City</label>
<input type="text" name="edit_city" value="<?php echo $row['city'] ?>"
class="form-control">
</div>
<form action="edit_register.php" method="POST">
<input type="hidden" name="edit_username" value="<?php echo $row['username'];?>">
<button type="submit" name="edit_button" class="btn btn-success">Edit</button>
</form>
</form>
<?php
}
}
?>
<?php
include('includes/script.php');
include('includes/footer.php');
?>
edit_register.php
<?php
$connection = mysqli_connect("localhost", "root", "", "finalproject")or die ("<script>alert('Connection Failed.')</script>");
session_start();
include('includes/header.php');
include('includes/navbar.php');
?>
<div class="card shadow">
<div class="card-header">
<h6 class="font-weight-bold text-primary">EDIT Profile </h6>
</div>
<div class="card_body">
<?php
if(isset($_POST['updatebtn']))
{
$username2 = $_POST['edit_username'];
$full_name2 = $_POST['edit_full_name'];
$email2 = $_POST['edit_email'];
$password2 = $_POST['edit_password'];
$cpassword2 = $_POST['cpassword'];
$phone_number2 = $_POST['edit_phone_number'];
$work_duration2 = $_POST['edit_work_duration'];
$food_category2 = $_POST['edit_food_category'];
$ig_account2 = $_POST['edit_ig_account'];
$state2 = $_POST["edit_state"];
$postcode2 = $_POST['edit_postcode'];
$city2 = $_POST['edit_city'];
$gender2 = $_POST['edit_gender'];
$age2 = $_POST['edit_age'];
$query = "UPDATE users SET username='$username2', full_name='$full_name2', email='$email2', password='$password2', phone_number='$phone_number2', word_duration='$work_duration2', food_category='$food_category2',
ig_account='$ig_account2', state='$state2', postcode='$postcode2', city='$city2', gender='$gender2', age='$age2' WHERE username='$username2'";
$query_run = mysqli_query($connection, $query);
if($query_run)
{
$_SESSION['status'] = "Your Data is Updated";
$_SESSION['status_code'] = "success";
header('Location: register.php');
}
else
{
$_SESSION['status'] = "Your Data is NOT Updated";
$_SESSION['status_code'] = "error";
header('Location: register.php');
}
?>
<?php
}
?>
</div>
<?php
include('includes/script.php');
include('includes/footer.php');
?>
At register.php, you have this line this -
<input type="hidden" name="edit_username" value="<?php echo $row['username'];?>">
So at first_register.php, you should have this line like this instead -
$username = $_POST['username']; to $username = $_POST['edit_username'];
and this line like this instead -
<form action="code.php" method="POST"> to <form action="edit_register.php" method="POST">
i could see you have a form inside a another form like this
<form action="edit_register.php" method="POST">
<input type="hidden" name="edit_username" value="<?php echo $row['username'];?>">
<button type="submit" name="edit_button" class="btn btn-success">Edit</button>
</form>
//remove the form tag to this only since you have input for edit_username aready at the top of your form
<button type="submit" name="edit_button" class="btn btn-success">Edit</button>
//and finally at edit_register.php change
if(isset($_POST['updatebtn']))
to if(isset($_POST['edit_button']))
I guess this should help achieve the goal. good luck

PHP: Forget Password Module working unexpectedly

I am making a simple forget password module.
User will enter his username, security question answer, and new password.
So if the user is found in the database table, the password will get updated
or else it will show an error.
FORM CODE :
<form id="fogform" method="POST">
<div class="form-group row">
<label for="un" class="col-form-label col-md-6">Username:
</label>
<div class="col-md-6">
<input type="text" class="form-control" name="un" placeholder="Enter your username" required>
</div>
</div>
<div class="form-group">
<div class="row">
<label class="col-form-label col-md-6" for="ques">Security
Question:</label>
<div class="col-md-6">
<select name="ques" class="form-control" required>
<option value="" selected>Choose any:</option>
<option value="In which city you were
born?">In which city you were born?
</option>
<option value="What was your first
pet's name?">What was your first pet's name?</option>
</select>
</div>
</div>
</div>
<div class="form-group row">
<label for="answ" class="col-form-label col-md-6">Your Answer:
</label>
<div class="col-md-6">
<input type="text" class="form-control" name="answ" placeholder="Enter your answer" required>
</div>
</div>
<div class="form-group row">
<label for="npwd" class="col-form-label col-md-6">New Password:
</label>
<div class="col-md-6">
<input type="password" class="form-control" name="npass" placeholder="Enter a New Password" required>
</div>
</div>
<div class="form-group row">
<label for="rpwd" class="col-form-label col-md-6">Retype Password:
</label>
<div class="col-md-6">
<input type="password" class="form-control" name="pass" placeholder="Retype the password" required>
</div>
</div>
<div class="form-group row">
<div class="col-md-5 offset-2">
<button type="reset" class="btn btn-danger">Reset</button>
</div>
<div class="col-md-5">
<button type="submit" name="fogo" class="btn btn-success" data-toggle="modal" data-target="#infomod" form="fogform">Submit</button>
</div>
</div>
</form>
PHP CODE:
<?php
session_start();
$cn=mysqli_connect("localhost", "root", "", "imedtalks");
if(isset($_POST['fogo']))
{
$fogsql = "SELECT uname, passwd, que, ans FROM register WHERE passwd='".$_POST['pass']."'
AND que='".$_POST['ques']."' AND ans='".$_POST['ques']."' AND uname='".$_POST['un']."' ";
$rslt=mysqli_query($cn, $fogsql);
if($_POST['npass'] === $_POST['pass'])
{
if($row=mysqli_fetch_assoc($rslt))
{
$fogsql2 = "UPDATE register SET passwd='".$_POST['pass']."' WHERE que='".$_POST['ques']."' AND ans='".$_POST['ques']."'
AND uname='".$_POST['un']."' ";
if($cn->query($fogsql2) === TRUE)
{
echo " <script type='text/javascript'>alert('Password Reset Successful!')</script> ";
echo " <script type='text/javascript'>window.location.replace('http://localhost/PROII/HomePage.php')</script> ";
}
}
else
{
echo " <script type='text/javascript'>alert('Password Reset Unsuccessful! Try Again.')</script> ";
echo " <script type='text/javascript'>window.location.replace('http://localhost/PROII/HomePage.php')</script> ";
}
}
else
{
echo " <script type='text/javascript'>alert('Password Verification Failed! Try Again')</script> ";
echo " <script type='text/javascript'>window.location.replace('http://localhost/PROII/HomePage.php')</script> ";
}
}
?>
So here, whenever i try the wrong way it gives unsuccessful message but even for right field data, it gives an error.
I am new to php, that's why i can't understand where its going wrong.

while using session in php during log in when the user is directed to user account page the page is empty

I have a log in page in there i have set some sessions so once after the user is logged in the gets directed to user page ie www.sitename/users/index.php on this page there is profile info and password &email change update etc.. the regular stuff.
The issue i'm encountering is the when the user is logged in after redirecting via php header function the user profile page section is not showing.
This is my php log in code:
<?php
session_start();
include 'include/db-conn.php';
if (isset($_POST['submit'])) {
$username = $_POST['user_name'];
$email = $_POST['user_email'];
$password = $_POST['user_password'];
$sql = "SELECT * FROM `all_users` WHERE `user_name`='$username' AND `user_email`='$email'";
$result = mysqli_query($conn, $sql);
$user = mysqli_fetch_assoc($result);
if ($user['status'] == '0') {
echo "<script>alert('Your Account needs to be verified first , check your email that you got during signing up!')</script>";
exit();
header("Location: index.php");
}
if ($user['status'] == '2') {
echo "<script>alert('Your Account Status is 2!')</script>";
exit();
header("Location: index.php");
}
if ($user['status'] == '3') {
echo "<script>alert('Your Account Status is 3!!')</script>";
exit();
header("Location: index.php");
}
if ($user['status'] == '4') {
exit();
header('Location: index.php');
}
if ($user['user_name'] == $username && $user['user_email'] == $email && password_verify($password, $user['user_password'])) {
$_SESSION['id'] = $user['id'];
$_SESSION['userid'] = $user['user_random_id'];
$_SESSION['username'] = $user['user_name'];
$_SESSION['email'] = $user['user_email'];
$_SESSION['user_role_type'] = $user['user_role_type_name'];
$_SESSION['main_user_type'] = $user['main_user_group'];
$_SESSION['sub_user_type'] = $user['sub_user_group'];
$_SESSION['logged_in'] = true;
echo '<script>location.href=users/index.php;</script>';
header("Location: users/index.php");
//ob_enf_fluch();
} else {
echo "<script>alert('Please check your credentials. Your user name, email or password are incorrect')</script>";
header("Location: index.php");
}
}
?>
And this the session check for user account pages so they only have access when they logged in:
<?php
if (!isset($_SESSION['main_user_group'])) {
header("Location: index.php");
exit();
}
?>
the is the html code:
<div class="panel panel-default">
<div class="panel-heading">Log In</div>
<div class="panel-body">
<form name="userLogin" action="login-script.php" method="POST" onsubmit="return validateForm()">
<!---->
<div class="form-group">
<label for="user_name">Username</label>
<input required="required" placeholder="enter here" type="text" name="user_name" class="form-control">
</div>
<hr>
<!---->
<div class="form-group">
<label for="user_email">Email</label>
<input required="required" placeholder="enter here" type="email" name="user_email" class="form-control">
</div>
<hr>
<!---->
<div class="form-group">
<label for="user_password">Password</label>
<input required="required" placeholder="enter here" type="password" name="user_password" class="form-control">
</div>
</div>
<div class="panel-footer">
<div class="form-group">
<input placeholder="enter here" type="submit" name="submit" value="submit" class="form-control btn btn-success">
</div>
</div>
</form>
</div>
And this the user section www.somesite.com/users/index page content:
<?php include '../include/db-conn.php'; ?>
<?php include '../include/header.php'; ?>
<?php include 'session-check.php'; ?>
<!--Section-->
<div class="container-fluid p0 m0 ">
<ul class="breadcrumb m0 p20">
<li>Users</li>
<li class="active">Account</li>
</ul>
</div>
<!-- Section-->
<div class="container-fluid">
<div class="container">
<div class="row">
<br>
<div class="col-md-3">
<div class="panel panel-default">
<div class="panel-heading">
Links
</div>
<div class="panel-body p0">
<?php include 'common-links.php'; ?>
</div>
</div>
</div>
<div class="col-md-9">
<form action="">
<div class="col-md-3">
<div class="panel panel-default p0">
<div class="panel-heading">Image</div>
<div class="panel-body p0">
<img class="img-responsive" src="https://s3.amazonaws.com/uifaces/faces/twitter/rem/128.jpg" width="100%" height="100%" alt="">
<label for="user_image"><input class="form-control" type="file" name="user_image"></label>
</div>
<div class="panel-footer">
<p>User Role:</p>
<p>Username:</p>
<p>Joined:</p>
<p>User ID:</p>
<p>Email Verified:</p>
</div>
</div>
</div>
<div class="col-md-9">
<div class="panel panel-default p0">
<div class="panel-heading">Profile Info</div>
<div class="panel-body">
<div class="form-group">
<label for="user_title">Title</label>
<select class="form-control select" name="user_title" id="">
<option value="non-selected">Non Selected</option>
<option value="Mr">Mr</option>
<option value="Mrs">Mrs</option>
<option value="Miss">Miss</option>
<option value="oters">Others</option>
</select>
</div>
<hr>
<div class="form-group">
<label for="user_gender">Gender</label>
<select class="form-control select" name="user_gender" id="">
<option value="non-selected">Non Selected</option>
<option value="Female">Female</option>
<option value="Male">Male</option>
<option value="Tansgender">Tansgender</option>
<option value="Bi-Sexual">Bi-Sexual</option>
<option value="oters">Others</option>
</select>
</div>
<hr>
<div class="form-group">
<label for="user_first_name">First Name</label>
<input required class="form-control" type="text" name="user_first_name" placeholder="enter here">
</div>
<hr>
<div class="form-group">
<label for="user_middle_name">Middle Name</label>
<input required class="form-control" type="text" name="user_middle_name" placeholder="enter here">
</div>
<hr>
<div class="form-group">
<label for="user_last_name">Lastname</label>
<input required class="form-control" type="text" name="user_last_name" placeholder="enter here">
</div>
<hr>
<div class="form-group">
<label for="user_dob">D.O.B</label>
<input required class="form-control" type="date" name="user_dob" placeholder="enter here">
</div>
<hr class="">
<textarea class="tinymce form-control" cols="30" rows="10" name="about_user" placeholder="Write something about yourself!"></textarea>
<hr>
<h3>Address</h3>
<div class="form-group">
<label for="user_address_number">Address No</label>
<input required class="form-control" type="text" name="user_address_number" placeholder="enter here">
</div>
<hr>
<div class="form-group">
<label for="user_address_street">Address Street</label>
<input required class="form-control" type="text" name="user_address_street" placeholder="enter here">
</div>
<hr>
<!--get country from the db-->
<div class="form-group">
<label for="user_address_country">Country(select)</label>
<select class="form-control select" name="user_address_country" id="">
<option value="non-selected">None Selected</option>
<option value="GB">United Kingdom</option>
</select>
</div>
<hr>
<!--get state from the db-->
<div class="form-group">
<label for="user_address_state">State / County /Province / Region (select)</label>
<select class="form-control select" name="user_address_state" id="">
<option value="non-selected">None Selected</option>
<option value="GB">United Kingdom</option>
</select>
</div>
<hr>
<!--get city from the db-->
<div class="form-group">
<label for="user_address_city">City (select)</label>
<select class="form-control select" name="user_address_city" id="">
<option value="non-selected">None Selected</option>
<option value="GB">United Kingdom</option>
</select>
</div>
<hr>
<!--get town from the db-->
<div class="form-group">
<label for="user_address_town">Town (select)</label>
<select class="form-control select" name="user_address_town" id="">
<option value="non-selected">None Selected</option>
<option value="GB">United Kingdom</option>
</select>
</div>
<hr>
<div class="form-group">
<label for="user_address_post_code_zip">Post / Zip Code </label>
<input required class="form-control" type="text" name="user_address_post_code_zip" placeholder="enter here">
</div>
<hr>
<div class="form-group">
<label for="user_phone_number">Phone Number</label>
<input class="form-control" type="tel" name="user_phone_number" placeholder="enter here">
</div>
<hr>
<div class="form-group">
<label for="user_mobile_number">Mobile Number</label>
<input class="form-control" type="tel" name="user_mobile_number" placeholder="enter here">
</div>
<hr>
<div class="form-group">
<label for="user_email">User Email</label>
<input required class="form-control" type="email" name="user_email" placeholder="enter here">
</div>
<hr>
<div class="form-group">
<input class="form-control btn-success" type="submit" name="save" value="Save">
</div>
</div>
</div>
</div>
</form>
</div>
</div>
</div>
<br>
</div>
<?php include '../include/footer.php'; ?>
Try giving the code if this code does not work check your password hash format correct or not.
Note: user/index.php must start session "session_start();"
<?php
session_start();
include 'include/db-conn.php';
if (isset($_POST['submit'])) {
$username = $_POST['user_name'];
$email = $_POST['user_email'];
$password = md5($_POST['user_password']);
$sql = "SELECT * FROM `all_users` WHERE `user_name`='$username' AND `user_email`='$email' AND `user_password`='$password'";
$result = mysqli_query($conn, $sql);
$user = mysqli_fetch_assoc($result);
if(!empty($user)){
if ($user['status'] == '0') {
echo "<script>alert('Your Account needs to be verified first , check your email that you got during signing up!')</script>";
exit;
header("Location: index.php");
}elseif($user['status'] == '2') {
echo "<script>alert('Your Account Status is 2!')</script>";
exit;
header("Location: index.php");
}elseif($user['status'] == '3') {
echo "<script>alert('Your Account Status is 3!!')</script>";
exit;
header("Location: index.php");
}elseif($user['status'] == '4') {
exit;
header('Location: index.php');
}
$_SESSION['id'] = $user['id'];
$_SESSION['userid'] = $user['user_random_id'];
$_SESSION['username'] = $user['user_name'];
$_SESSION['email'] = $user['user_email'];
$_SESSION['user_role_type'] = $user['user_role_type_name'];
$_SESSION['main_user_group'] = $user['main_user_group'];
$_SESSION['sub_user_type'] = $user['sub_user_group'];
$_SESSION['logged_in'] = true;
header("Location: users/index.php");
}else {
echo "<script>alert('Please check your credentials. Your user name, email or password are incorrect')</script>";
header("Location: index.php");
}
}
?>
The reason the html section wasn't displaying was the html was before session_start(); as i was using php include function to render html across the site mainly the header.php.
The issue is resolved by adding a session_start(); at the start of the header.php which is rendered by the php include function or just make sure session_start(); is above html tag in any given situation.
Kindly correct me if I'm wrong, but it seem to have worked for me!
Thanks

Data wont insert into Database

I'm trying to insert data from a form to my database but it doesn't seem to work. I've put an echo after the insert query so I can verify that the data was inserted but it doesn't echo what I've written. Is there a problem with my query or any part of my php?
My PHP:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "stat_system";
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$fname = $lname = $mname = $contact = $age = $attain = $course = $school = $position = $exp = $ref = $batchtxt = $hiredate = $prevbpo = $remarks = $nho = $nonbpo = $holdAttain = $holdPos = "";
$error_flag = 0;
if (isset($_POST['submit'])) {
$holdAttain = (isset($_POST['cmbAttain']));
$holdPos = (isset($_POST['cmbPosition']));
if (!empty($_POST['firstname'])) {
$fname = $_POST['firstname'];
}
if (!empty($_POST['lastname'])) {
$lname = $_POST['lastname'];
}
if (!empty($_POST['middlename'])) {
$mname = $_POST['middlename'];
}
if (!empty($_POST['contact'])) {
$contact = $_POST['contact'];
}
if (!empty($_POST['age'])) {
$age = $_POST['age'];
}
if (isset($_POST['cmbAttain'])) {
$attain = $_POST['cmbAttain'];
}
if(isset($_POST['school'])) {
$school = $_POST['school'];
}
if(isset($_POST['course'])) {
$course = $_POST['course'];
}
if (isset($_POST['exp'])) {
$exp = $_POST['exp'];
}
if (!empty($_POST['remarks'])) {
$remarks = $_POST['remarks'];
}
if (isset($_POST['nonbpo'])) {
$nonbpo = $_POST['nonbpo'];
}
if (isset($_POST['prevbpo'])) {
$prevbpo = $_POST['prevbpo'];
}
if (!empty($_POST['ref'])) {
$ref = $_POST['ref'];
}
if (isset($_POST['hiredate'])) {
$hiredate = $_POST['hiredate'];
}
if (isset($_POST['batchtxt'])) {
$batchtxt = $_POST['batchtxt'];
}
if (!empty($_POST['nho'])) {
$nho = $_POST['nho'];
}
if($error_flag == 0){
$sql = mysqli_query($conn,"INSERT INTO applicants (appID, appLastName, appFirstName, appMidleName, Age, appPhoneNumber, appBatch, appExperience, appRemarks, appPreviousBPO, appSchool, appCourse, appGraduate, appNonBPO, appPosition, appHireDate, appNHO, appReferrer)
VALUES (NULL, '$lname', '$fname', '$mname', $age, '$contact', $batchtxt, '$exp', $remarks, '$school', '$course', '$attain', '$nonbpo', '$position', $hiredate, $nho, '$ref')");
echo "<script type='text/javascript'>
$(document).ready(function(){
$('#succModal').modal('show');
});
</script>";
$fname = $lname = $mname = $contact = $age = $attain = $course = $school = $batchtxt = $ref = $hiredate = $position = $exp = $prevbpo = $remarks = $nho = $nonbpo = $holdAttain = $holdPos = "";
}
else {
print '<script type="text/javascript">';
print 'alert("Please fill in all the fields!")';
print '</script>';
}
}
mysqli_close($conn);
?>
My HTML:
<div id="addApplicant" class="addApp-marginleft" style="height:1000px">
<form id="registration" class="form-horizontal" method="post" action="index.php">
<div class="row">
<div align="center">
<h3>Add Applicant</h3>
<br>
</div>
</div>
<div class="form-group" align="center" >
<label class="col-sm-4 control-label"><small>First name:</small></label>
<div class="col-sm-4">
<input required type="text" name="firstname" autocomplete="off" placeholder="Firstname" id="firstname" class="form-control" value="<?php echo $fname;?>">
</div>
<div class="col-sm-4">
</div>
</div>
<div class="form-group" align="center" >
<label class="col-sm-4 control-label"><small> Last name:</small></label>
<div class="col-sm-4">
<input required type="text" name="lastname" autocomplete="off" id="lastname" placeholder="Lastname" class="form-control" value="<?php echo $lname;?>">
</div>
<div class="col-sm-4">
</div>
</div>
<div class="form-group" align="center" >
<label class="col-sm-4 control-label"><small>Middle name:</small></label>
<div class="col-sm-4">
<input required type="text" name="middlename" autocomplete="off" id="middlename" placeholder="middlename" class="form-control" value="<?php echo $mname;?>">
</div>
<div class="col-sm-4">
</div>
</div>
<div class="form-group" align="center" >
<label class="col-sm-4 control-label"><small>Contact number:</small></label>
<span id="errmsg"></span>
<div class="col-sm-4">
<input required type="text" name="contactnum" autocomplete="off" onkeypress="return isNumber(event)" placeholder="Contact number" id="contact" class="form-control" maxlength="11" value="<?php echo $contact;?>"/>
</div>
<?php
echo '<script>';
echo 'function isNumber(evt) {';
echo 'evt = (evt) ? evt : window.event;';
echo 'var charCode = (evt.which) ? evt.which : evt.keyCode;';
echo 'if (charCode > 31 && (charCode < 48 || charCode > 57)) {';
echo 'return false;';
echo '}';
echo 'return true;';
echo '}';
echo '</script>';
?>
<div class="col-sm-4">
</div>
</div>
<div class="form-group" align="center" >
<label class="col-sm-4 control-label"><small>Age:</small></label>
<div class="col-sm-4">
<input required type="text" autocomplete="off" placeholder="age" name="age" id="age" class="form-control" value="<?php echo $age;?>">
</div>
<div class="col-sm-4">
</div>
</div>
<div class="form-group" align="center" >
<label class="col-sm-4 control-label"><small>Graduate:</small></label>
<div class="col-sm-4">
<select required name="cmbAttain" id="cmbAttain" class="form-control" onChange="disableCmb();">
<option value="">Choose</option>
<option value="Yes" <?php if($holdAttain == "Yes") echo "selected"; ?>>Yes</option>
<option value="No" <?php if($holdAttain == "No") echo "selected"; ?>>No</option>
</select>
</div>
<div class="col-sm-4">
</div>
</div>
<div class="form-group" align="center" >
<label class="col-sm-4 control-label"><small>School:</small></label>
<div class="col-sm-4">
<input type="text" name="school" autocomplete="off" id="school" placeholder="School" class="form-control" value="<?php echo $school;?>">
</div>
</div>
<div class="form-group" align="center" >
<label class="col-sm-4 control-label"><small>Course:</small></label>
<div class="col-sm-4">
<input type="text" name="course" autocomplete="off" id="course" placeholder="Course" class="form-control" value="<?php echo $course;?>">
</div>
</div>
<div class="form-group" align="center" >
<label class="col-sm-4 control-label"><small>Batch:</small></label>
<div class="col-sm-4">
<input type="text" name="batchtxt" autocomplete="off" id="batchtxt" placeholder="Batch" class="form-control" value="<?php echo $batchtxt;?>">
</div>
</div>
<div class="form-group" align="center" >
<label class="col-sm-4 control-label"><small>Experience:</small></label>
<div class="col-sm-4">
<input type="text" name="exp" autocomplete="off" id="exp" placeholder="Experience" class="form-control" value="<?php echo $exp;?>">
</div>
</div>
<div class="form-group" align="center" >
<label class="col-sm-4 control-label"><small>Previous BPO:</small></label>
<div class="col-sm-4">
<input type="text" name="prevbpo" autocomplete="off" id="prevbpo" placeholder="Previous BPO" class="form-control" value="<?php echo $prevbpo;?>">
</div>
</div>
<div class="form-group" align="center" >
<label class="col-sm-4 control-label"><small>Non-BPO:</small></label>
<div class="col-sm-4">
<input type="text" name="nonbpo" autocomplete="off" id="nonbpo" placeholder="Non-BPO" class="form-control" value="<?php echo $nonbpo;?>">
</div>
</div>
<div class="form-group" align="center" >
<label class="col-sm-4 control-label"><small>Remarks:</small></label>
<div class="col-sm-4">
<input type="text" name="remarks" autocomplete="off" id="remarks" placeholder="Remarks" class="form-control" value="<?php echo $remarks;?>">
</div>
</div>
<div class="form-group" align="center" >
<label class="col-sm-4 control-label"><small>Hire Date:</small></label>
<div class="col-sm-4">
<input type="date" name="hiredate" id="hiredate" class="form-control" autocomplete="off" value="<?php echo $hiredate;?>">
</div>
</div>
<div class="form-group" align="center" >
<label class="col-sm-4 control-label"><small>Position:</small></label>
<div class="col-sm-4">
<select required name="cmbPosition" id="cmbPosition" class="form-control" data-size="5" >
<option selected value="">Choose</option>
<option value="Customer Service Representative" <?php if($holdPos == "Customer Service Representative") echo "selected"; ?>>Customer Service Representative</option>
<option value="Image Enhancer" <?php if($holdPos == "Image Enhancer") echo "selected"; ?>>Image Enhancer</option>
</select>
</div>
<div class="col-sm-4">
</div>
</div>
<div class="form-group" align="center" >
<label class="col-sm-4 control-label"><small>NHO:</small></label>
<div class="col-sm-4">
<input type="date" name="nho" id="nho" class="form-control" autocomplete="off" value="<?php echo $nho;?>">
</div>
</div>
<div class="form-group" align="center" >
<label class="col-sm-4 control-label"><small>Referrer:</small></label>
<div class="col-sm-4">
<input type="text" name="ref" autocomplete="off" id="ref" placeholder="Name of Referrer" class="form-control" value="<?php echo $ref;?>">
</div>
</div>
<div class="form-group" align="center" >
<div class="col-sm-4">
</div>
<div class="col-sm-4">
<div class="btn-group " role="group" aria-label="...">
<input id="submitbtn" type="submit" name="submit" class="btn btn btn-success" value="Submit" data-target="#succModal">
<input type="reset" name="reset" class="btn btn-warning" value="Clear">
</div>
</div>
<div class="col-sm-4">
</div>
</div>
</form>
</div>
<div class="container">
<!-- Register Success Modal -->
<div class="modal fade" id="succModal" role="dialog">
<div class="modal-dialog modal-sm">
<div class="modal-content">
<div class="modal-body" align="center">
<p>REGISTRATION SUCCESSFUL</p>
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</div>
use $_POST instead of $_REQUEST..
like:
$lname = $_POST['lname'];
Just at a glance, you're NULL tests should be seperated by &&, not ||. The way you have it, it's only checking whether one (any) of them contain a value.
There is some errors with you query, just replace it with
$sql = "INSERT INTO applicants (appLastName, appFirstName, appMidleName, Age, appPhoneNumber, appBatch, appExperience, appRemarks, appPreviousBPO, appSchool, appCourse, appGraduate, appNonBpo, appPosition, appHireDate, appNHO, appReferrer) VALUES ('$lname','$fname','$mname',$age,'$con',$batch,'$exp','$rem','$prevbpo','$school','$course','$gradsit', '$nbpo', '$pos', '$hdate', '$nho', '$ref')";
here, there is no need to add appid as it is auto-incremented. Also there are some missing single quotes.
Skip to send value NULL in your insert statement
where you check whether $_POST is empty, if it is empty set $error_flag=1
Inside if (isset($_POST['submit'])) { print $_POST
Hope this will help to debug. If still issue, check your error_log

Undefined index when passing variable to another page from a form

I'm trying to pass a value from one page to the other but I can't for the life of me understand why I keep getting the undefined index error message.
This is the PHP code to the first page:
<?php
include 'database_connection.php';
$id = $_GET['id'];
$sql="select engineer.id, engineer.team_id, engineer.first_name, engineer.active, engineer.last_name, engineer.role, engineer.region, engineer.phone, to_date, team.team_name, team.manager_name, team.description, team.type, engineer.email from engineer inner join team on engineer.team_id=team.id where active=0 and engineer.team_id > 0 and engineer.id = '".$id."'";
$results = mysqli_query($connection, $sql);
while($row = mysqli_fetch_array($results)) {
$id=$row['id'];
$first_name=$row['first_name'];
$last_name=$row['last_name'];
$role=$row['role'];
$email=$row['email'];
$phone=$row['phone'];
$region=$row['region'];
$type=$row['type'];
?>
<form class="form-horizontal col-sm-12" role="form" method="post" action="../admin/update.php">
<fieldset disabled>
<div class="form-group">
<label for="sso" class="col-sm-2 control-label">SSO ID</label>
<div class="col-sm-10">
<input type="text" id="id" name="id" class="form-control" value="<?php echo $id; ?>">
</div>
</div>
</fieldset>
<div class="form-group">
<label for="name" class="col-sm-2 control-label">Name</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="name" name="first_name" placeholder="First Name" value="<?php echo $first_name; ?>">
</div>
</div>
<div class="form-group">
<label for="lastname" class="col-sm-2 control-label">Surname</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="name" name="last_name" placeholder="Last Name" value="<?php echo $last_name; ?>">
</div>
</div>
<div class="form-group">
<label for="team" class="col-sm-2 control-label">Team</label>
<div class="col-sm-10">
<select style="width:auto;" class="btn btn-default dropdown-toggle form-control" type="button" name="team_name" value="" id="dropdownMenu1" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">
<?php
//connect to the database
include 'database_connection.php';
//query the database
$sql_team = "SELECT DISTINCT id, team_name FROM team";
//uses $sql_team variable to make a specific query
$query = mysqli_query($connection, $sql_team);
?>
<option value="<?php echo $team_name; ?>"><?php echo $team_name; ?></option>
<?php
//initilises a while loop to retrieve all the rows
while ($row = mysqli_fetch_array($query) )
{
//echos all the distinct catDesc rows into a list
echo "<option value='" . $row['id'] . "' >".htmlspecialchars($row["team_name"])."</option>";
}
?>
</select>
</div>
</div>
<div class="form-group">
<label for="role" class="col-sm-2 control-label">Role</label>
<div class="col-sm-10">
<input type="role" class="form-control" id="role" name="role" placeholder="Role" value="<?php echo $role; ?>">
</div>
</div>
<div class="form-group">
<label for="email" class="col-sm-2 control-label">Email</label>
<div class="col-sm-10">
<input type="email" class="form-control" id="email" name="email" placeholder="example#domain.com" value="<?php echo $email; ?>">
</div>
</div>
<div class="form-group">
<label for="phone" class="col-sm-2 control-label">Phone Number</label>
<div class="col-sm-10">
<input type="phone" class="form-control" id="phone" name="phone" placeholder="Business number" value="<?php echo $phone; ?>">
</div>
</div>
<div class="form-group">
<label for="region" class="col-sm-2 control-label">Region</label>
<div class="col-sm-10">
<input type="region" class="form-control" id="region" name="region" placeholder="Region e.g. South" value="<?php echo $region; ?>">
</div>
</div>
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<input type="submit" value="Save" class="btn btn-primary">
</div>
</div>
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<! Will be used to display an alert to the user>
</div>
</div>
</form>
<?php
//closes and stops the loop
}
?>
This is the code to the second page, where the error message appears:
<?php
include 'database_connection.php';
$id = mysqli_real_escape_string($connection, $_GET['id']);
$first_name = mysqli_real_escape_string($connection, $_POST['first_name']);
$last_name = mysqli_real_escape_string($connection, $_POST['last_name']);
$team_name = mysqli_real_escape_string($connection, $_POST['team_name']);
$role = mysqli_real_escape_string($connection, $_POST['role']);
$email = mysqli_real_escape_string($connection, $_POST['email']);
$phone = mysqli_real_escape_string($connection, $_POST['phone']);
$region = mysqli_real_escape_string($connection, $_POST['region']);
if ($role == ''){
$role = NULL;
}
if ($email == ''){
$email = NULL;
}
if ($phone == ''){
$phone = NULL;
}
if ($region == ''){
$region = NULL;
}
$sql = "UPDATE people SET id='$id', first_name='$first_name', last_name='$last_name', team_id='$team_name', role='$role', email='$email', phone=$phone', region='$region' where id='$id')";
?>
So the error appears when I try to retrieve the ID onto the second page from the first page. What am I missing?
if you want to pass a variable as get method you can pass it inside action attribute of form
eg: action="xyz.php?id="
on line 3 you have assigned a $_GET[] variable you cannot assign there since those values only sets after submitting form so if you want to remove error please script it like this
if(isset($GET['id']){
$id = $_GET['id'];
}
use an
<input id="id" type="hidden" value=$id>
in your form and read it as
$_POST['id']
in the second page

Categories