Session is not storing variables in PHP? - php

I'm facing dilemma that the session I created to store the varibale(the user name) is not showing. I want to show the user name in the maindash.html after when the user sign up in index.php.
maindash.html code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>
MY SITE PLANNER | DASHBOARD
</title>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"\>
<link href="custom.css" rel="stylesheet" type="text/css" />
</head>
<body class="formbg">
<div class="container-fluid pi">
<div class="row">
<div class="col-md-6 col-sm-6 col-xs-6">
<p class="ppp">
<img src="image/my-site-planner-logo.png" class="ppp" />
</p>
</div>
<div class="col-md-6 col-sm-6 col-xs-6">
<p class="up">
<!-- Hi, user -->
Hello, <?php echo $_SESSION["name"]; ?>
settings
logout
</p>
<p id="dt">
System:
</p>
</div>
</div>
</div>
<div class="container-fluid fill">
<div class="row">
<div class="col-md-12 col col-sm-12 col-xs-12">
<div class="navbar">
<ul class="nalink">
<li>
ViewIncomeStatement
</li>
<li>
View/AddSite
</li>
<li>
View/AddVendor
</li>
<li>
View/AddItem
</li>
</ul>
</div>
</div>
</div>
</div>
<div class="container-fluid">
<div class="row">
<div class="col-md-6 col-sm-6 col-xs-12">
<h3 class="side">
Summary of your site
</h3>
</div>
<div class="col-md-6 col-sm-6 col-xs-12">
<button class="btn btn-success view" name="submit" type="submit" >
View Income Statement
</button>
</div>
</div>
</div>
<div class="container-fluid">
<div class="row">
<div class="col-md-12 col-sm-12 col-xs-12">
<div class="table-responsive line">
<table class="table table-bordered dem">
<thead>
<tr class="hd">
<th width="3">Sr.</th>
<th width="6">Site Name</th>
<th width="6">Incomes</th>
<th width="6">Expenses</th>
<th width="6">Payables</th>
<th width="6">Balance</th>
<th width="10">Last Week Details</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>SITE A </td>
<td>200000</td>
<td>110000</td>
<td>36000</td>
<td>79000</td>
<td>Completed kitchen tiles work</td>
</tr>
<tr>
<td>2</td>
<td>SITE B</td>
<td>500000</td>
<td>425000</td>
<td>88000</td>
<td>-13000</td>
<td>completed kitchen tiles work</td>
</tr>
<tr>
<td></td>
<td>TOTAL</td>
<td>700000</td>
<td>555000</td>
<td>124000</td>
<td>66000</td>
<td></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
<div class="container-fluid">
<div class="row">
<div class="col-md-6 col-sm-6 col-xm-12">
<h3 class="side">
Your to do list
</h3>
</div>
<div class="col-md-6 col-sm-6 col-xm-12">
<button class="btn btn-success view" name="submit" type="submit">
Add Item
</button>
</div>
</div>
</div>
<div class="container-fluid">
<div class="row">
<div class="col-md-12 col-sm-12 col-xm-12">
<div class="table-responsive line">
<table class="table table-bordered dem">
<thead>
<tr class="hd">
<th width="3">Sr.</th>
<th width="15">Task</th>
<th width="15">Site</th>
<th width="15">Added</th>
<th width="20">Action</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>Need to co-ordinate with shahid bhai for comment bags</td>
<td>Site A</td>
<td>23-02-2016</td>
<td>
Delete Task Mark as complete
</td>
</tr>
<tr>
<td>2</td>
<td>Get registrations done</td>
<td>Site B</td>
<td>23-02-2016</td>
<td>Delete Task Mark as complete</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
<div class="container-fluid">
<div class="row">
<div class="col-md-6 col-sm-6 col-xm-12">
<h3 class="side">
Quick Navigation
</h3>
</div>
</div>
</div>
<div class="container-fluid">
<div class="row">
<div class="col-md-12 col-sm-12 col-xm-12">
<div class="table-responsive line">
<table class="table table-bordered dem">
<tr class="hd">
<th>
<span>Select Site</span>
<select name="site" class="hig">
<option value="" selected="selected">Fetch site list</option>
<option value="site A">site A</option>
<option value="site B">site B</option>
</select>
<span class="dis">Select Operations</span>
<select name="site" class="hig">
<option value="" selected="selected">select your option</option>
<option value="option 1">View Site-wise imcome statement</option>
<option value="option 2">View Site-wise vendors</option>
<option value="option 3">View Site-wise items list</option>
<option value="option 4">List of Vendors Vs Expenses</option>
<option value="option 5">List of Item Vs Expenses</option>
<option value="option 6">List of Labours Vs Expenses</option>
<option value="option 7">List of Service Vs Expenses</option>
<option value="option 8">Payables List</option>
<option value="option 9">To-do List</option>
<option value="option 10">Work Done List</option>
</select>
<button class="btn btn-success viewl" name="submit" type="submit" >GO</button>
</th>
</tr>
</table>
</div>
</div>
</div>
</div>
<div class="container-fluid">
<div class="row">
<div class="col-md-6 col-sm-6 col-xm-12">
<h3 class="side">
Reporting Definations
</h3>
</div>
</div>
</div>
<div class="container-fluid">
<div class="row">
<div class="col-md-12 col-sm-12 col-xm-12">
<div class="table-responsive line">
<table class="table table-bordered dem">
<tbody>
<tr class="info">
<td>View Site-wise imcome statement</td>
<td>Show Income Statement of a particular site</td>
</tr>
<tr>
<td>View Site-wise vendors</td>
<td>Show all the vandors of a perticular site</td>
</tr>
<tr class="info">
<td>View Site-wise items list</td>
<td>Show all the Items registerd in a particular</td>
</tr>
<tr>
<td>List of Vendors Vs Expenses</td>
<td>List of all vandors registerd in a perticular site vs amount paid to them</td>
</tr>
<tr class="info">
<td>List of Item Vs Expenses</td>
<td>List of all the Items used/registerd in a perticular site and the amount utilized on them</td>
</tr>
<tr>
<td>List of Labours Vs Expenses</td>
<td>List of the labours used in a perticular site and the amount paid to them</td>
</tr>
<tr class="info">
<td>List of Service Vs Expenses</td>
<td>List of the Service used in a perticular site and the amount paid to them</td>
</tr>
<tr>
<td>Payables List</td>
<td>List of Payables in perticula site</td>
</tr>
<tr class="info">
<td>To-do List</td>
<td>To-do List of a perticular site</td>
</tr>
<tr>
<td>Work Done List</td>
<td>List of Works Done on a perticular site</td>
</tr>
<tr class="info">
<td></td>
<td></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
<script>
document.getElementById("dt").innerHTML = Date();
</script>
<script src="https://code.jquery.com/jquery-2.2.0.js"></script>
</body>
</html>
index.php code:
<?php
session_start();
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "index";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$email = $_POST['email'];
$password = $_POST['password'];
$sql = "INSERT INTO form ( email, password )
VALUES ( '$email', '$password')";
if ($conn->query($sql) === TRUE) {
header('Location: sign-up.html');
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
if($count == 1)
{
$_SESSION['logged'] = true;
$_SESSION['username'] = $myusername;
echo "Login worked";
exit();
}
else
{
$_SESSION['logged']=false;
echo "Login failed";
exit();
}
?>
<li class='active' style='float:right;'>
<?php
if($_SESSION['logged'] == true)
{
echo $_SESSION["name"];
echo '<span>Logout</span></li>';
}
elseif($_SESSION['logged'] == false)
{
echo '<span>Login/Register</span></li>';
}
//Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM form where email = $email and password = $password";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo " Name: " . $row["name"]. ".<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
Here after the user log in, it doesn't show the user name. I want to make it as the user name should be taken from the database file and displayed in the maindash.html. But it only shows me the Hello as written in my code.

Rename your maindash.html page to have a .php extension. And at the top of the page (maindash.php i.e.) ensure you put
<?php
session_start();
?>

Related

INSERT query is not working on the addition of WHERE clause in SELECT statement

So to sum up my problem and what I'm trying to do:
I'm trying to code an Attendance feature in my PHP Website where the admin first chooses the Class he wants to mark the Attendance for, and then he's taken to a new page with a table that ONLY displays students from THAT class. He enter the date/status of each student and presses Submit, and multiple rows are inserted into the "attendance" table.
Now, this was working perfectly before. The only new addition to this feature was the "Class" feature, now each student has a class (you might call it grade in your region). Before, they didn't.
So naturally, the table's SELECT query went from
$rows2 = $conn->query("SELECT * FROM students")->fetchAll(PDO::FETCH_ASSOC);
to:
$rows2 = $conn->query("SELECT * FROM students WHERE grade_id = $grade")->fetchAll(PDO::FETCH_ASSOC);
This little addition has completely messed up my INSERT query, which works fine when I exclude the "WHERE" clause. Below, I'm attaching code for two pages - one is ChooseClass.php where admin picks the Class he wants to add Attendance for. This page basically sends the chosen class ID to the 2nd page, which is AddAttendance.php.
ChooseClass.php:
<?php include('../db.php');
session_start();
$email = $_SESSION["email"];
$user = $conn->query("SELECT * from admin where email = '$email' ")->fetchAll(PDO::FETCH_ASSOC);
$userID = $user[0]['admin_id'];
$someotherRow = $conn->query("SELECT * FROM grade")->fetchAll(PDO::FETCH_ASSOC);
?>
<!DOCTYPE html>
<html>
<?php include('adminheader.php'); ?>
<body style="background-color: #5e72e4 !important;">
<!-- Sidenav -->
<?php include('adminsidebar.php'); ?>
<div class="main-content" id="panel">
<!-- Topnav -->
<?php include('admintopbar.php'); ?>
<!-- Header -->
<!-- Header -->
<div class="header bg-primary pb-6" style="background-color: #5e72e4 !important;">
<div class="container-fluid">
<div class="header-body">
<div class="row align-items-center py-4">
<div class="col-lg-6 col-7">
<h6 class="h2 text-white d-inline-block mb-0">Attendance Managament</h6>
</div>
</div>
<!-- Card stats -->
<div class="row">
<div class="col-lg-12">
<div class="card">
<!-- Card body -->
<div class="card-header">Choose a Class</div>
<div class="card-body">
<div class="table-responsive" style="overflow-y: scroll; height: 600px;">
<table class="table align-items-center table-dark table-flush">
<thead class="thead-dark">
<tr>
<th scope="col" class="sort" data-sort="name">Class Name</th>
<th scope="col" class="sort" data-sort="status">Action</th>
</tr>
</thead>
<tbody class="list">
<?php
foreach ($someotherRow as $row) { ?>
<tr>
<th scope="row">
<div class="media align-items-center">
<div class="media-body">
<span class="name mb-0 text-sm"><?php echo $row['grade_name']; ?></span>
</div>
</div>
</th>
<td>
<form action="" method="POST">
<a type="submit" href="adminmarkattendance.php?gradeid=<?php echo $row['grade_id']; ?> " class="btn btn-primary">Select</a>
</form>
</td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<!-- Page content -->
<div class="container-fluid mt--6">
<!-- background-color: #5e72e4 !important; height: 63.9vh; -->
</div>
</div>
</body>
</html>
AddAttendance.php:
<?php include('../db.php');
session_start();
$email = $_SESSION["email"];
$user = $conn->query("SELECT * from admin where email = '$email' ")->fetchAll(PDO::FETCH_ASSOC);
$userID = $user[0]['admin_id'];
$grade = 0;
if (isset($_GET['gradeid'])) {
$grade = $_GET['gradeid'];
}
$rows2 = $conn->query("SELECT * FROM students WHERE grade_id = $grade")->fetchAll(PDO::FETCH_ASSOC);
$count = count($rows2);
if (isset($_POST["submit"])) {
for ($i = 0; $i < $count; $i++) {
$student_id = $_POST["id"][$i];
$status = $_POST['options'][$i];
$grade_id = $_POST['grade_id'];
$date = $_POST['attendancedate'];
$date = date('Y-m-d', strtotime($date));
$queryInsert = $conn->prepare("INSERT
into attendance
(
student_id,
grade_id,
date,
status
)
values
(
$student_id,
$grade_id,
'$date',
'$status'
)
");
$queryInsert->execute();
}
echo "<script> location.replace('chooseclass.php'); </script>";
}
?>
<!DOCTYPE html>
<html>
<?php include('adminheader.php'); ?>
<body style="background-color: #5e72e4 !important;">
<!-- Sidenav -->
<?php include('adminsidebar.php'); ?>
<div class="main-content" id="panel">
<!-- Topnav -->
<?php include('admintopbar.php'); ?>
<!-- Header -->
<!-- Header -->
<div class="header bg-primary pb-6" style="background-color: #5e72e4 !important;">
<div class="container-fluid">
<div class="header-body">
<div class="row align-items-center py-4">
<div class="col-lg-6 col-7">
<h6 class="h2 text-white d-inline-block mb-0">Mark Attendance</h6>
</div>
</div>
<!-- Card stats -->
<div class="row">
<div class="col-lg-12">
<form action="adminmarkattendance.php" method="post">
<div class="row">
<div class="col">
<div class="card bg-default shadow">
<div class="card-header bg-transparent border-0">
<div class="form-inline">
<div class="col-lg-6">
<h3 class="text-white mb-0">Registered Students</h3>
</div>
<div class="col-lg-6">
<div class="form-group">
<input style="width: 100%;" class="form-control" name="attendancedate" type="date" required>
</div>
</div>
</div>
</div>
<div class="table-responsive" style="overflow-y: scroll; height: 600px;">
<table class="table align-items-center table-dark table-flush">
<thead class="thead-dark">
<tr>
<th scope="col" class="sort" data-sort="name">Avatar</th>
<th scope="col" class="sort" data-sort="name">Student Name</th>
<th scope="col" class="sort" data-sort="status">Phone Number</th>
<th scope="col" class="sort" data-sort="status">Age</th>
<th scope="col" class="sort" data-sort="status">Gender</th>
<th scope="col" class="sort" data-sort="status">Address</th>
<th scope="col" class="sort" data-sort="status">Action</th>
</tr>
</thead>
<tbody class="list">
<?php
foreach ($rows2 as $row) { ?>
<tr>
<td>
<img src="<?php echo '../profileImages/' . $row['profile_image'] ?>" width="45" height="45" alt="">
<input type="hidden" name="id[]" value="<?php echo $row['student_id']; ?>">
<input type="hidden" name="grade_id" value="<?php echo $grade ?>">
</td>
<th scope="row">
<div class="media align-items-center">
<div class="media-body">
<span class="name mb-0 text-sm"><?php echo $row['fname'] . ' ' . $row['lname']; ?></span>
</div>
</div>
</th>
<td>
<span class="badge badge-dot mr-4">
<span class="status"><?php echo $row['phonenumber']; ?></span>
</span>
</td>
<td>
<span class="badge badge-dot mr-4">
<span class="status"><?php echo $row['age']; ?></span>
</span>
</td>
<td>
<span class="badge badge-dot mr-4">
<span class="status"><?php echo $row['gender']; ?></span>
</span>
</td>
<td>
<span class="badge badge-dot mr-4">
<span class="status"><?php echo $row['address']; ?></span>
</span>
</td>
<td>
<select class="form-control" name="options[]">
<option value="Present" selected>Present</option>
<option value="Absent">Absent</option>
</select>
</td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
</div>
</div>
</div>
<div class="text-center">
<button type="submit" name="submit" style="width: 100%;" class="btn btn-warning">Mark Attendance</button>
</div>
</form>
</div>
</div>
</div>
</div>
<!-- Page content -->
<div class="container-fluid mt--6">
<!-- background-color: #5e72e4 !important; height: 63.9vh; -->
</div>
</div>
</body>
</html>
I've spent 2 hours looking at this code and I can't figure out why the addition of WHERE breaks my code. I've also var_dumped most of the variables, just to check if I was entering some NULL value and I'm not. So kindly look into it and see if you can figure out why data is not being inserted into the database..
You are sending gradeid value in -
href="adminmarkattendance.php?gradeid= "
And getting value in AddAttendance.php
So, first give right path and second, there is also a space within ahref. So, when you try to get the value use trim, it will remove spaces -
$grade = trim($_GET['gradeid']);

Auto reloding/refreshing div with jquery makes impact on page style

I was set up my index page with pending order,sales and completed order. Each and every minutes that data are changed.so i decided to refresh particular div by jquery and got succeed by changing data dynamically.But issues start on my style.When the div was reloded style of card was distract.I dont know why this happened.
<div class="row" >
<div class="col-xl-3 col-lg-3 col-md-6 col-sm-12 col-12" id="here">
<div class="row">
<div class="col-xl-12">
<div class="card border-3 border-top border-top-primary">
<div class="card-body">
<h5 class="text-muted">Total Sales</h5>
<div class="metric-value d-inline-block">
<h1 class="mb-1 text-center"><? if($total != '') { echo "Rs: ".$total; } else { echo "Rs: 0";} ?></h1>
</div>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-xl-12">
<div class="card border-3 border-top border-top-primary">
<div class="card-body">
<h5 class="text-muted">Complted Order</h5>
<div class="metric-value d-inline-block">
<h1 class="mb-1 text-center"><?= $complte." Nos"; ?></h1>
</div>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-xl-12">
<div class="card border-3 border-top border-top-primary">
<div class="card-body">
<h5 class="text-muted">Pending Order</h5>
<div class="metric-value d-inline-block">
<h1 class="mb-1 text-center"><?= $pending." Nos"; ?></h1>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="col-xl-9 col-lg-12 col-md-6 col-sm-12 col-12">
<?
$orderquery = mysqli_query($conn,"SELECT * FROM ordermaster o LEFT JOIN customer c ON o.custid = c.custid WHERE o.status = 'p' AND orderdt = CURRENT_DATE() ");
?>
<div class="card">
<h5 class="card-header">Recent Orders</h5>
<div class="card-body p-0">
<div class="table-responsive">
<table class="table">
<thead class="bg-light">
<tr class="border-0">
<th class="border-0">#</th>
<th class="border-0">Name</th>
<th class="border-0">Phone</th>
<th class="border-0">Address</th>
<th class="border-0">Pincode</th>
<th class="border-0">City</th>
<th class="border-0">Order Time</th>
<th class="border-0">Amount</th>
<th class="border-0">View</th>
</tr>
</thead>
<tbody>
<?
$count = 1;
while($res = mysqli_fetch_array($orderquery))
{
?>
<tr>
<td><?= $count; ?></td>
<td><?= $res['name']; ?> </td>
<td><?= $res['phone']; ?> </td>
<td><?= $res['addr1']."/".$res['addr2']; ?> </td>
<td><?= $res['pincode']; ?> </td>
<td><?= $res['city']; ?> </td>
<td><?= $res['orderdtm']; ?> </td>
<td><?= $res['net']; ?> </td>
<td><button class="btn btn-sm btn-outline-primary" title="View Product" onclick="del('<? echo $res['orderid'];?>')" value="id" data-toggle="modal" data-target="#delete" data-backdrop="static" data-keyboard="false"><i class="fas fa-eye"></i></button></td>
</tr>
<?
$count++;
}
?>
<tr>
<td colspan="9">View Details</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
and my jquery coding to reload div is
$(document).ready(function(){
setInterval(function(){
$("#here").load(window.location.href + " #here" );
}, 3000);
});
whats error going on i cann't understand.image before apply load
image after load
To stop the loop you can use clearInterval()
Jquery replace with this
$(document).ready(function(){
window.setInterval(function(){
$("#here").load(window.location.href + " #here");
}, 3000);
clearInterval()
});

Filter database table through drop-down list

I need to filter the table depends on the selected values of the drop-down lists. So know I need to know how to return the filter result into the page. I made the filter result in search.php but I can't return the filter result to the table again. The filter should filter by user ID or product ID for now.
<body>
<?php include('navbar.php'); ?>
<!-- Page header -->
<div class="container">
<h1 class="page-header text-center">Order system</h1>
</div>
<!-- Search -->
<div class="container">
<h3 class="text-left">Search</h3>
<div class="col-md-4">
<form method="post" action="search.php">
<div class="form-group">
<select name="search_date" class="form-control">
<option value="">Date</option>
<option value="">All time</option>
<option value="">Last 7 days</option>
<option value="">Today</option>
</select>
</div>
<div class="form-group">
<select name="user_id" class="form-control">
<option value="">User</option>
<option value="1">John Smith</option>
<option value="2">Laura Stone</option>
<option value="3">Jon Olseen</option>
</select>
</div>
<div class="form-group">
<select name="product_id" class="form-control">
<option value="">Product</option>
<option value="1">Pepsi Cola</option>
<option value="2">Coca Cola</option>
<option value="3">Fanta</option>
</select>
</div>
<div>
<button type="submit" class="btn btn-primary"><span class="glyphicon glyphicon-search"></span> Filter</button>
</div>
</form>
</div>
</div>
<!-- Order History -->
<div class="container">
<h3 class="text-left">Order History</h3>
<table class="table table-striped table-bordered">
<thead>
<th>User</th>
<th>Product</th>
<th>Price</th>
<th>Quantity</th>
<th>Total</th>
<th>Date</th>
<th>Actions</th>
</thead>
<tbody>
<?php
$sql="SELECT * FROM orders o JOIN user u ON o.user_id=u.user_id JOIN product p ON o.product_id=p.product_id";
$query=$conn->query($sql);
while($row=$query->fetch_array()){
?>
<tr>
<td><?php echo $row['user_name']; ?></td>
<td><?php echo $row['product_name']; ?></td>
<td><?php echo $row['product_price']; ?></td>
<td><?php echo $row['order_quantity']; ?></td>
<td><?php echo $row['order_price']; ?></td>
<td><?php echo $row['order_date']; ?></td>
<td>
<span class="glyphicon glyphicon-pencil"></span> Edit || <span class="glyphicon glyphicon-trash"></span> Delete
<?php include('order_modal.php'); ?>
</td>
</tr>
<?php
}
?>
</tbody>
</table>
</div>
</body>
</html>
<?php
include('conn.php');
$search_date=$_POST['search_date'];
$user_id=$_POST['user_id'];
$product_id=$_POST['product_id'];
$sql="select * from orders where user_id='$user_id' OR product_id='$product_id'";
$query=$conn->query($sql);
$row=$query->fetch_array();
header('location:orderhistory.php');
?>
<?php include('header.php'); ?>
<body>
<?php include('navbar.php'); ?>
<!-- Page header -->
<div class="container">
<h1 class="page-header text-center">Order system</h1>
</div>
<!-- Search -->
<div class="container">
<h3 class="text-left">Search</h3>
<div class="col-md-4">
<form method="post" action="">
<div class="form-group">
<select name="search_date" class="form-control">
<option value="100000">Date</option>
<option value="100000">All time</option>
<option value="7">Last 7 days</option>
<option value="0">Today</option>
</select>
</div>
<div class="form-group">
<select name="user_id" class="form-control">
<option value="%">User</option>
<option value="1">John Smith</option>
<option value="2">Laura Stone</option>
<option value="3">Jon Olseen</option>
</select>
</div>
<div class="form-group">
<select name="product_id" class="form-control">
<option value="%">Product</option>
<option value="1">Pepsi Cola</option>
<option value="2">Coca Cola</option>
<option value="3">Fanta</option>
</select>
</div>
<div>
<button type="submit" name="search" class="btn btn-primary"><span class="glyphicon glyphicon-search"></span> Filter</button>
</div>
</form>
</div>
</div>
<!-- Order History -->
<div class="container">
<h3 class="text-left">Order History</h3>
<table class="table table-striped table-bordered">
<thead>
<th>User</th>
<th>Product</th>
<th>Price</th>
<th>Quantity</th>
<th>Total</th>
<th>Date</th>
<th>Actions</th>
</thead>
<tbody>
<?php
if(isset($_POST["search"])){
$sql="SELECT * FROM orders o JOIN user u ON o.user_id=u.user_id JOIN product p ON o.product_id=p.product_id WHERE o.product_id LIKE '$_POST[product_id]' AND o.user_id LIKE '$_POST[user_id]' AND o.order_date>=DATE(NOW()) - INTERVAL '$_POST[search_date]' DAY";
}else{
$sql="SELECT * FROM orders o JOIN user u ON o.user_id=u.user_id JOIN product p ON o.product_id=p.product_id";
}
$query=$conn->query($sql);
while($row=$query->fetch_array()){
?>
<tr>
<td><?php echo $row['user_name']; ?></td>
<td><?php echo $row['product_name']; ?></td>
<td><?php echo $row['product_price']; ?></td>
<td><?php echo $row['order_quantity']; ?></td>
<td><?php echo $row['order_price']; ?></td>
<td><?php echo $row['order_date']; ?></td>
<td>
<span class="glyphicon glyphicon-pencil"></span> Edit || <span class="glyphicon glyphicon-trash"></span> Delete
<?php include('order_modal.php'); ?>
</td>
</tr>
<?php
}
?>
</tbody>
</table>
</div>
</body>
</html>

Login in page not redirecting to index.php after entering correct details with no error displayed

I'm developing a login system for my website using php 7. The login page redirects to index.php when i use the following code
if(isset($_SESSION['username'])){
header('Location: login.php');
}
But fails to redirect to index.php or any page when i use
if(!isset($_SESSION['username'])){
header('Location: login.php');
}
I use !isset to make sure a user does not go direct to a page by typing its url on the browser. This is to ensure you get redirected back to the login page to avoid unauthorized access in the admin panel. But it is rather not logging in even when correct details are entered.
LOGIN.PHP
<?php
ob_start();
session_start();
require_once('../inc/db.php');
if(isset($_POST['submit'])){
$username = mysqli_real_escape_string($con,strtolower($_POST['username']));
$password = mysqli_real_escape_string($con,$_POST['password']);
$check_username_query = "SELECT * FROM users WHERE username = '$username'";
$check_username_run = mysqli_query($con, $check_username_query);
if(mysqli_num_rows($check_username_run) > 0){
$row = mysqli_fetch_array($check_username_run);
$db_username = $row['username'];
$db_password = $row['password'];
$db_role = $row['role'];
$password = crypt($password, $db_password);
if($username == $db_username && $password == $db_password){
header('Location: index.php');
$_SESSION['username'] = $db_username;
$_SESSION['role'] = $db_role;
}
else{
$error = "Wrong Username or Password";
}
}
else{
$error = "Wrong Username or Password";
}
}
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="">
<meta name="author" content="Mark Otto, Jacob Thornton, and Bootstrap contributors">
<meta name="generator" content="Jekyll v3.8.5">
<link rel="icon" href="img/circleda.png">
<title>Login | Amasiatu Admin</title>
<!-- Bootstrap core CSS -->
<link href="css/animate.css" rel="stylesheet">
<link href="css/bootstrap.min.css" rel="stylesheet">
<style>
.bd-placeholder-img {
font-size: 1.125rem;
text-anchor: middle;
}
#media (min-width: 768px) {
.bd-placeholder-img-lg {
font-size: 3.5rem;
}
}
</style>
<!-- Custom styles for this template -->
<link href="login.css" rel="stylesheet">
</head>
<body class="text-center">
<form class="form-signin animated shake" action="" method="post">
<div class="login-logo" style="margin: 0 auto; margin-bottom: 18px">A</div>
<h1 class="h3 mb-3 font-weight-normal">Amasiatu Login!</h1>
<label for="inputEmail" class="sr-only">Username</label>
<input type="text" id="inputEmail" name="username" class="form-control" placeholder="username" required autofocus>
<label for="inputPassword" class="sr-only">Password</label>
<input type="password" id="inputPassword" name="password" class="form-control" placeholder="Password" required>
<input type="submit" name="submit" value="Sign In" class="btn btn-lg btn-primary btn-block">
<div class="checkbox ">
<label>
<?php
if(isset($error)){
echo "$error";
}
?>
</label>
</div>
<p class="mt-5 mb-3 text-muted year">© 2018-2019</p>
</form>
</body>
</html>
INDEX.PHP
<?php require_once ('inc/top.php');
if(isset($_SESSION['username'])){
header('Location: login.php');
}
?>
</head>
<body>
<div id="wrapper">
<?php require_once ('inc/header.php'); ?>
<div class="container-fluid body-section">
<div class="row">
<div class="col-md-3">
<?php require_once ('inc/sidebar.php'); ?>
</div>
<div class="col-md-9">
<h1><i class="fas fa-tachometer-alt"></i> Dashboard <small style="color: #495057">Statistics Overview</small></h1><hr>
<nav aria-label="breadcrumb">
<ol class="breadcrumb">
<li class="breadcrumb-item active" aria-current="page"><i class="fas fa-tachometer-alt"></i> Dashboard</li>
</ol>
</nav>
<div class="row tag-boxes">
<div class="col-md-6 col-lg-3">
<div class="panel panel-blue">
<div class="panel-heading">
<div class="row">
<div class="col-xs-3">
<i class="fas fa-comments fa-5x"></i>
</div>
<div class="col-xs-9">
<div class="text-right huge">11</div>
<div class="text-right">New Comment</div>
</div>
</div>
</div>
<a href="">
<div class="panel-footer">
<span class="float-left">View All Comments</span>
<span class="float-right"><i class="fas fa-arrow-circle-right"></i></span>
<div class="clearfix"></div>
</div>
</a>
</div>
</div>
<div class="col-md-6 col-lg-3">
<div class="panel panel-red">
<div class="panel-heading">
<div class="row">
<div class="col-xs-3">
<i class="fas fa-file-alt fa-5x"></i>
</div>
<div class="col-xs-9">
<div class="text-right huge">20</div>
<div class="text-right">All Posts</div>
</div>
</div>
</div>
<a href="">
<div class="panel-footer">
<span class="float-left">View All Posts</span>
<span class="float-right"><i class="fas fa-arrow-circle-right"></i></span>
<div class="clearfix"></div>
</div>
</a>
</div>
</div>
<div class="col-md-6 col-lg-3">
<div class="panel panel-yellow">
<div class="panel-heading">
<div class="row">
<div class="col-xs-3">
<i class="fas fa-users fa-5x"></i>
</div>
<div class="col-xs-9">
<div class="text-right huge">41</div>
<div class="text-right">All Users</div>
</div>
</div>
</div>
<a href="">
<div class="panel-footer">
<span class="float-left">View All Users</span>
<span class="float-right"><i class="fas fa-arrow-circle-right"></i></span>
<div class="clearfix"></div>
</div>
</a>
</div>
</div>
<div class="col-md-6 col-lg-3">
<div class="panel panel-green">
<div class="panel-heading">
<div class="row">
<div class="col-xs-3">
<i class="fas fa-folder-open fa-5x"></i>
</div>
<div class="col-xs-9">
<div class="text-right huge">8</div>
<div class="text-right">All Categories</div>
</div>
</div>
</div>
<a href="">
<div class="panel-footer">
<span class="float-left">View All Categories</span>
<span class="float-right"><i class="fas fa-arrow-circle-right"></i></span>
<div class="clearfix"></div>
</div>
</a>
</div>
</div>
</div><hr>
<h3>New Users</h3>
<table class="table table-hover table-striped">
<thead>
<tr>
<th>SN #</th>
<th>Date</th>
<th>Name</th>
<th>Username</th>
<th>Role</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>18 Jan 2019</td>
<td>Val Amasiatu</td>
<td>Computacity</td>
<td>Admin</td>
</tr>
<tr>
<td>2</td>
<td>18 Jan 2019</td>
<td>Val Amasiatu</td>
<td>Computacity</td>
<td>Admin</td>
</tr>
<tr>
<td>3</td>
<td>18 Jan 2019</td>
<td>Val Amasiatu</td>
<td>Computacity</td>
<td>Admin</td>
</tr>
<tr>
<td>4</td>
<td>18 Jan 2019</td>
<td>Val Amasiatu</td>
<td>Computacity</td>
<td>Admin</td>
</tr>
<tr>
<td>5</td>
<td>18 Jan 2019</td>
<td>Val Amasiatu</td>
<td>Computacity</td>
<td>Admin</td>
</tr>
</tbody>
</table>
View All Users<hr>
<h3>New Posts</h3>
<table class="table">
<thead>
<tr>
<th>SN #</th>
<th>Date</th>
<th>Post Title</th>
<th>Category</th>
<th>Views</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>18 Jan 2019</td>
<td>Learn Python and build a Complete Project</td>
<td>Video Tutorials</td>
<td><i class="far fa-eye"></i> 35</td>
</tr>
<tr>
<td>2</td>
<td>18 Jan 2019</td>
<td>Learn Python and build a Complete Project</td>
<td>Video Tutorials</td>
<td><i class="far fa-eye"></i> 35</td>
</tr>
<tr>
<td>3</td>
<td>18 Jan 2019</td>
<td>Learn Python and build a Complete Project</td>
<td>Video Tutorials</td>
<td><i class="far fa-eye"></i> 35</td>
</tr>
<tr>
<td>4</td>
<td>18 Jan 2019</td>
<td>Learn Python and build a Complete Project</td>
<td>Video Tutorials</td>
<td><i class="far fa-eye"></i> 35</td>
</tr>
<tr>
<td>5</td>
<td>18 Jan 2019</td>
<td>Learn Python and build a Complete Project</td>
<td>Video Tutorials</td>
<td><i class="far fa-eye"></i> 35</td>
</tr>
</tbody>
</table>
View All Posts
</div>
</div>
</div>
<?php require_once ('inc/footer.php'); ?>
Highly likely some output is being given to unlogged user in "inc/top.php".
Try this:
<?php
ob_start();
require_once ('inc/top.php');
if(!isset($_SESSION['username'])){
header('Location: login.php');
die();
}
?>

cart button not working

Hi I have some more issues in my coding.when i have to changed my coding structure from div to table add to cart button not working.when it is working when it is in div but i have change to table cart button not working please help me to resolve my issue..
Code below is the old one (div) thats working fine here..
<?php if($microproduct['nr']){?>
<div class="product-box">
<div class="list" id="micropro">
<?php foreach ($microproduct['result'] as $resultmicroproduct){ ?>
<div class="row">
<div class="col-md-3">
<img src="images/feature-two-one.png" class="img-fluid mx-auto d-block">
</div>
<div class="col-md-7">
<div class="pro-desc">
<h5><?php echo $conn->stripval($resultmicroproduct['microsoft_product_legalname']);?></h5>
<p>Part number: <?php echo $conn->stripval($resultmicroproduct['microsoft_product_partnumber']);?></p>
<p>Item Name: <?php echo $conn->stripval($resultmicroproduct['microsoft_product_name']);?></p>
<p>Product family: <?php echo $conn->stripval($resultmicroproduct['microsoft_product_family']);?></p>
<h4><i class="fa fa-rupee"></i> <?php echo $conn->stripval($resultmicroproduct['microsoft_product_price']);?></h4>
</div>
</div>
<div class="col-md-2">
<div class="wrapper-div">
<input min="1" type="number" name="quantity<?php echo $resultmicroproduct['microsoft_product_id'] ?>" class="form-control" id="quantity" value="1" autocomplete="off">
<br>
<?php $postvalue = base64_encode(json_encode($resultmicroproduct)); ?>
<input type="hidden" name="type" value="microsoft_product">
<button class="btn btn-dark btn-sm add_to_cart" id="<?php echo $resultmicroproduct['microsoft_product_id'] ?>" >Add to Cart</button>
</div>
</div>
</div>
<?php } ?>
</div>
</div>
<?php } ?>
Code Below is the new one (table) cart button not working..
<table id="example" class="table table-striped table-bordered" style="width:100%">
<thead>
<tr>
<th>Image</th>
<th>Product Description</th>
<th>Quantity & Cart</th>
</tr>
</thead>
<?php if($microproduct['nr']){?>
<tbody>
<?php foreach ($microproduct['result'] as $resultmicroproduct){ ?>
<tr class="product-box">
<td><img src="images/feature-two-one.png" class="img-fluid mx-auto d-block"></td>
<td>
<div class="pro-desc">
<h5><?php echo $conn->stripval($resultmicroproduct['microsoft_product_legalname']);?></h5>
<p>Part number: <?php echo $conn->stripval($resultmicroproduct['microsoft_product_partnumber']);?></p>
<p>Item Name: <?php echo $conn->stripval($resultmicroproduct['microsoft_product_name']);?></p>
<p>Product family: <?php echo $conn->stripval($resultmicroproduct['microsoft_product_family']);?></p>
<h4><i class="fa fa-rupee"></i> <?php echo $conn->stripval($resultmicroproduct['microsoft_product_price']);?></h4>
</div>
</td>
<td>
<div class="wrapper-div">
<input min="1" type="number" name="quantity<?php echo $resultmicroproduct['microsoft_product_id'] ?>" class="form-control" id="quantity" value="1" autocomplete="off">
<br>
<input type="hidden" name="type" value="microsoft_product">
<button class="btn btn-dark btn-sm add_to_cart" id="<?php echo $resultmicroproduct['microsoft_product_id'] ?>">Add to Cart</button>
</div>
</td>
</tr>
<?php } ?>
</tbody>
<?php } ?>
<tfoot>
<th>Image</th>
<th>Product Description</th>
<th>Quantity & Cart</th>
</tfoot>
</table>
You don't even have a form.
<form action ="?" method="?">
//your inputs and submit
</form>
What do you expect to happen if you don't define what to do?

Categories