Update query won't update my product list - php

I am having serious problems with my product details not updating. It fetches all the product information when I click edit but when I press the submit button to update the product details it does not have an affect on the database. I have been spending quite some time on this and have looked at solutions online as well. None of them seem to work
Here is my code :
<?php
include("functions/mysqli_connect.php");
if(isset($_GET['edit'])) {
$get_id = $_GET['edit'];
$get_pro = "select * from shop where product_id='$get_id'";
$run_pro = mysqli_query($con, $get_pro);
$row_pro=mysqli_fetch_array($run_pro);
$pro_id = $row_pro['product_id'];
$pro_name = $row_pro['name'];
$pro_cat = $row_pro['category'];
$pro_description = $row_pro['description'];
$pro_quantity = $row_pro['quantity'];
$pro_price = $row_pro['price'];
$image = $row_pro['images'];
}
?>
<!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">
<meta name="description" content="">
<meta name="author" content="">
<title>SB Admin - Bootstrap Admin Template</title>
<!-- Bootstrap Core CSS -->
<link href="css/bootstrap.min.css" rel="stylesheet">
<!-- Custom CSS -->
<link href="css/sb-admin.css" rel="stylesheet">
<!-- Custom Fonts -->
<link href="font-awesome/css/font-awesome.min.css" rel="stylesheet" type="text/css">
<!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
<script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body>
<div id="wrapper">
<!-- Navigation -->
<nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-ex1-collapse">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="index.html">SB Admin</a>
</div>
<!-- Top Menu Items -->
<ul class="nav navbar-right top-nav">
<li class="dropdown">
<i class="fa fa-user"></i> <?php echo $_SESSION['admin_username']; ?> <b class="caret"></b>
<ul class="dropdown-menu">
<li>
<i class="fa fa-fw fa-power-off"></i> Log Out
</li>
</ul>
</li>
</ul>
<!-- Sidebar Menu Items - These collapse to the responsive navigation menu on small screens -->
<div class="collapse navbar-collapse navbar-ex1-collapse">
<ul class="nav navbar-nav side-nav">
<li class="active">
<i class="fa fa-fw fa-dashboard"></i> Insert Products
</li>
<li>
<i class="fa fa-fw fa-table"></i> View Products
</li>
<li>
<i class="fa fa-fw fa-bar-chart-o"></i> Edit Products
</li>
<li>
<i class="fa fa-fw fa-table"></i> Delete Products
</li>
</ul>
</div>
<!-- /.navbar-collapse -->
</nav>
<div id="page-wrapper">
<div class="container-fluid">
<!-- Page Heading -->
<div class="row">
<div class="col-lg-6">
<form action="edit_pro.php" method="post" enctype="multipart/form-data">
<div class="form-group">
<label for="name">Product Name</label>
<input type="text" class="form-control" id="name" name="name" value="<?php echo $pro_name;?>" >
</div>
<div class="form-group">
<label for="category">Category</label>
<select type="text" class="form-control" id="category" name="category" >
<option><?php echo $pro_cat;?></option>
<option>Henna</option>
<option>Gliter</option>
<option>Cajeput Oil</option>
<option>Henna Cones</option>
</select>
</div>
<div class="form-group">
<label for="image">Image</label>
<input type="file" id="image" name="image" ><img src="stock_images/<?php echo $image; ?>"width="60" height="60" />
<p class="help-block"></p>
</div>
<div class="form-group">
<label for="description">Description</label>
<textarea class="form-control" id="description" name="description"><?php echo $pro_description;?></textarea>
</div>
<div class="form-group">
<label for="quantity">Quantity</label>
<input type="number" class="form-control" id="quantity" name="quantity" value="<?php echo $pro_quantity;?>" >
</div>
<div class="form-group">
<label for="price">Price</label>
<input type="number" class="form-control" id="price" name="price" value="<?php echo $pro_price;?>" >
</div>
<div class="form-group">
<input name="id" type="hidden" id="id" value="<? echo $pro_id; ?>">
</div>
<input type="submit" name="update" class="btn btn-default" value="Add Stock"></a>.
</form>
</div>
</div>
<!-- /.row -->
</div>
<!-- /.container-fluid -->
</div>
<!-- /#page-wrapper -->
</div>
<!-- /#wrapper -->
<!-- jQuery -->
<script src="js/jquery.js"></script>
<!-- Bootstrap Core JavaScript -->
<script src="js/bootstrap.min.js"></script>
</body>
</html>
<?php
if(isset($_POST['update'])){
//getting the text data from the form
$id=mysql_real_escape_string($_GET['product_id']);
$update_id = $pro_id;
$pro_name = $_POST['name'];
$pro_cat = $_POST['category'];
$pro_description = $_POST['description'];
$pro_quantity = $_POST['quantity'];
$pro_price = $_POST['price'];
$image = $_FILES['image'] ['name'];
$image_tmp = $_FILES['image'] ['tmp_name'];
move_uploaded_file($image_tmp, "stock_images/$image");
$servername = "localhost";
$username = "Naina";
$password = "Mhendi2015";
$dbname = "farhanaina";
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "UPDATE shop SET name='$pro_name', category='$pro_cat', images='$image', description='$pro_description', quantity='$pro_quantity', price='$pro_price' WHERE product_id='$id'";
// Prepare statement
$stmt = $conn->prepare($sql);
// execute the query
$stmt->execute();
// echo a message to say the UPDATE succeeded
echo $stmt->rowCount() . " records UPDATED successfully";
}
catch(PDOException $e)
{
echo $sql . "<br>" . $e->getMessage();
}
$conn = null;
}
?>

You're using PDO and a prepared statement here. You can't feed a ready SQL query with the variable values into PDO::prepare. Quoting an example from the manual:
$sth = $dbh->prepare('SELECT name, colour, calories
FROM fruit
WHERE calories < ? AND colour = ?');
$sth->execute(array(150, 'red'));
The array you pass into your PDO::execute will replace each ? with a value in order of the values in the array. Otherwise, you can pass in an associative array and do it with named parameters:
$sql = 'SELECT name, colour, calories
FROM fruit
WHERE calories < :calories AND colour = :colour';
$sth = $dbh->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));
$sth->execute(array(':calories' => 150, ':colour' => 'red'));

Related

Data no inserted to database

I am a beginner and trying to follow some tutorial videos for my school project. I am stuck when the data not inserted into the database. I double-check the code to tutorial and nothing wrong. There's no error message or notification too. I really appreciate your help.
action.php
<!--action.php-->
<?php
require 'config.php';
if(isset($_POST['pid'])){
$pid = $_POST['pid'];
$pname = $_POST['pname'];
$pprice = $_POST['pprice'];
$pimage = $_POST['pimage'];
$pcode = $_POST['pcode'];
$pqty = 1;
$stmt = $conn->prepare("SELECT product_code FROM cart WHERE product_code=?");
$stmt->bind_param("s",$pcode);
$stmt->execute();
$res = $stmt->get_result();
$r = $res->fetch_assoc();
$code = $r['product_code'];
if(!$code){
$query = $conn->prepare("INSERT INTO cart (product_name,product_price,product_image,qty,total_price,produk_code) VALUES (?,?,?,?,?,?)");
$query->bind_param("sssiss",$pname,$pprice,$pimage,$pqty,$pprice,$pcode);
$query->execute();
echo '<div class="alert alert-success alert-dismissible">
<button type="button" class="close" data-dismiss="alert">×</button>
<strong>Item added to cart!</strong>
</div>';
}
else{
echo '<div class="alert alert-success alert-dismissible">
<button type="button" class="close" data-dismiss="alert">×</button>
<strong>Item already added to your cart!</strong>
</div>';
}
}
?>
index.php
<!--index.php-->
<!DOCTYPE html>
<html>
<head>
<title>Map Store</title>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<script src="https://kit.fontawesome.com/fc847822ba.js" crossorigin="anonymous"></script>
<link rel="stylesheet" href="css/bootstrap.min.css" >
<link rel="stylesheet" type="text/css" href="fontawesome/css/all.min.css"/>
</head>
<body>
<nav class="navbar navbar-expand-md bg-dark navbar-dark">
<!-- Brand -->
<a class="navbar-brand" href="index.php">Map Store</a>
<!-- Toggler/collapsibe Button -->
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#collapsibleNavbar">
<span class="navbar-toggler-icon"></span>
</button>
<!-- Navbar links -->
<div class="collapse navbar-collapse" id="collapsibleNavbar">
<ul class="navbar-nav ml-auto">
<li class="nav-item">
<a class="nav-link active" href="index.php">Products</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Category</a>
</li>
<li class="nav-item">
<a class="nav-link" href="cart.php">Checkout</a>
</li>
<li class="nav-item">
<a class="nav-link" href="cart.php"><i class="fas fa-shopping-cart text-white"> <span id="cart-item" class="badge badge-danger">0</span> </i></a>
</li>
</ul>
</div>
</nav>
<div class="container">
<div class="row mt-2">
<div id="message">
</div>
<?php
include 'config.php';
$stmt = $conn->prepare("SELECT * FROM product");
$stmt->execute();
$result = $stmt->get_result();
while($row = $result->fetch_assoc()):
?>
<div class="col-lg-3">
<div class="card-deck">
<div class="card p-2 border-secondary mb-2">
<img src="<?= $row['product_image'] ?>" class="card-img-top" height="250">
<div class="card-body p-1">
<h4 class="card-title text-center text-info"><?= $row['product_name']?></h4>
<h5 class="card-text text-center text-danger"><?= number_format($row
['product_price'],2) ?>/-</h5>
</div>
<div class="card-footer p-1">
<form action="#" class="form-submit">
<input type="hidden" class="pid" value="<?= $row['id']?>">
<input type="hidden" class="pname" value="<?= $row['product_name']?>">
<input type="hidden" class="pprice" value="<?= $row['product_price']?>">
<input type="hidden" class="pimage" value="<?= $row['product_image']?>">
<input type="hidden" class="pcode" value="<?= $row['product_code']?>">
<button class="btn btn-info btn-block addItemBtn"><i class="fas fa-cart-plus "></i>Add to cart</button>
</form>
</div>
</div>
</div>
</div>
<?php endwhile; ?>
</div>
</div>
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<!-- Popper JS -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$(".addItemBtn").click(function(e){
e.preventDefault();
var $form = $(this).closest(".form-submit");
var pid = $form.find(".pid").val();
var pname = $form.find(".pname").val();
var pprice = $form.find(".pprice").val();
var pimage = $form.find(".pimage").val();
var pcode = $form.find(".pcode").val();
$.ajax({
url: 'action.php',
method: 'post',
data: {pid:pid,pname:pname,pprice:pprice,pimage:pimage,pcode:pcode},
success:function(response){
$("message").html(response);
}
});
});
});
</script>
</body>
</html>
config.php
<!--config.php-->
<?php
$conn = new mysqli("localhost","root","","checkout_system");
if($conn->connect_error){
die("Connection Failed!".$conn->connect_error);
}
?>
sorry i am new here and this my first post
The HTML elements in your form must have a name attribute to be available in $_POST, you set only a class :
<input type="hidden" name="pid" class="pid" value="<?= $row['id']?>">

PHP Login just refreshes page no error

Hello I seem to be having a very frustrating problem with my login which i cannot seem to fix as there is no error output.I seem to have fixed this problem on my mac but i transfered the exact files over to my windows pc and its stopped working again. whenever I try and login with the example a#a.com and password a is seems to just refresh the page over and over no matter how many times I have tried.I was wondering if anyone could help with this error?
I have tried adding the action to login.php it seems to make no difference.
picture of table im taking data from:
session.php:
<?php
include('config.php');
session_start();
$user_check = $_SESSION['login_user'];
$ses_sql = mysqli_query($conn,"select email_adress from customer where email_adress = '$user_check' ");
$row = mysqli_fetch_array($ses_sql,MYSQLI_ASSOC);
$login_session = $row['email_adress'];
if(!isset($_SESSION['login_user'])){
header("location:login.php");
}
?>
login.php
<?php
include("config.php");
session_start();
if($_SERVER["REQUEST_METHOD"] == "POST") {
// username and password sent from form
$myusername = mysqli_real_escape_string($conn,$_POST['username']);
$mypassword = mysqli_real_escape_string($conn,$_POST['password']);
$sql = "SELECT customer_id FROM customer WHERE email_adress = '$myusername' and password = '$mypassword'";
$result = mysqli_query($conn,$sql);
$row = mysqli_fetch_array($result,MYSQLI_ASSOC);
$active = $row['customer_id'];
$count = mysqli_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row
if($count == 1) {
$_SESSION['login_user'] = $myusername;
header("location: index2.php");
}else {
$error = "Your Login Name or Password is invalid";
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Login | Bid4MyJob</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Bid4MyJob">
<meta name="author" content="James Wood">
<!-- CSS -->
<link href="assets/css/bootstrap.css" rel="stylesheet" type="text/css">
<link href="assets/css/font-awesome.min.css" rel="stylesheet" type="text/css">
<link href="assets/css/main.css" rel="stylesheet" type="text/css">
<link href="assets/css/my-custom-styles.css" rel="stylesheet" type="text/css">
<!-- IE 9 Fallback-->
<!--[if IE 9]>
<link href="assets/css/ie.css" rel="stylesheet">
<![endif]-->
<!-- GOOGLE FONTS -->
<link href='http://fonts.googleapis.com/css?family=Open+Sans:300,400italic,400,600,700' rel='stylesheet' type='text/css'>
<link href='http://fonts.googleapis.com/css?family=Roboto+Condensed:300,300italic,400italic,700,400,300' rel='stylesheet' type='text/css'>
<!-- FAVICONS -->
<link rel="apple-touch-icon-precomposed" sizes="144x144" href="assets/ico/repute144x144.png">
<link rel="apple-touch-icon-precomposed" sizes="114x114" href="assets/ico/repute114x114.png">
<link rel="apple-touch-icon-precomposed" sizes="72x72" href="assets/ico/repute72x72.png">
<link rel="apple-touch-icon-precomposed" href="assets/ico/repute57x57.png">
<link rel="shortcut icon" href="assets/ico/favicon.png">
</head>
<body>
<!-- WRAPPER -->
<div class="wrapper">
<!-- NAVBAR -->
<nav class="navbar navbar-default " role="navigation">
<div class="container">
<!-- TOPBAR -->
<div class="topbar">
<ul class="list-inline top-nav">
<li>
<div class="btn-group">
<button type="button" class="btn btn-link dropdown-toggle btn-xs" data-toggle="dropdown"><img src="assets/img/flags/United-Kingdom.png" alt="United Kingdom"> United Kingdom <span class="caret"></span></button>
<ul class="dropdown-menu dropdown-menu-right country-selector" role="menu">
<li>
<img src="assets/img/flags/United-Kingdom.png" alt="United Kingdom"> United Kingdom
</li>
<li>
<img src="assets/img/flags/Japan.png" alt="Japan"> Japan
</li>
<li>
<img src="assets/img/flags/China.png" alt="China"> China
</li>
<li>
<img src="assets/img/flags/Germany.png" alt="Germany"> Germany
</li>
</ul>
</div>
</li>
<li>Help</li>
<li>Support</li>
</ul>
<div class="searchbox">
<form method="post">
<div class="input-group input-group-sm">
<input type="text" class="form-control" placeholder="search ...">
<span class="input-group-btn">
<button class="btn btn-default" type="button"><i class="fa fa-search"></i></button>
</span>
</div>
</form>
</div>
</div>
<!-- END TOPBAR -->
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#main-nav">
<span class="sr-only">Toggle Navigation</span>
<i class="fa fa-bars"></i>
</button>
<a href="index.html" class="navbar-brand navbar-logo navbar-logo-bigger">
</a>
</div>
<!-- MAIN NAVIGATION -->
<div id="main-nav" class="navbar-collapse collapse">
<ul class="nav navbar-nav navbar-right">
<li>
HOME
</li>
<li>
HOW IT WORKS
</li>
<li>
POST JOB
</li>
<li>
FIND JOB
</li>
<li>
SIGN UP
</li>
<li>
LOGIN
</li>
</ul>
</div>
<!-- END MAIN NAVIGATION -->
</div>
</nav>
<!-- END NAVBAR -->
<!-- BREADCRUMBS -->
<div class="page-header">
<div class="container">
<h1 class="page-title pull-left">Login</h1>
<ol class="breadcrumb">
<li>Home</li>
<li class="active">Login</li>
</ol>
</div>
</div>
<!-- END BREADCRUMBS -->
<!-- PAGE CONTENT -->
<div class="page-content">
<div class="col-md-6">
<!-- LOGIN FORM -->
<h2 class="section-heading">Login Form</h2>
<form class="form-horizontal" role="form" action = "login.php" method = "post">
<div class="form-group">
<label for="username" class="control-label sr-only">Email</label>
<div class="col-sm-12">
<div class="input-group">
<input type="email" class="form-control" id="username" name = "username" placeholder="Email">
<span class="input-group-addon"><i class="fa fa-envelope"></i></span>
</div>
</div>
</div>
<div class="form-group">
<label for="password" class="control-label sr-only">Password</label>
<div class="col-sm-12">
<div class="input-group">
<input type="password" class="form-control" id="password" name="password" placeholder="Password">
<span class="input-group-addon"><i class="fa fa-lock"></i></span>
</div>
</div>
</div>
<div class="form-group">
<div class="col-sm-12">
<label class="fancy-checkbox">
<input type="checkbox">
<span>Remember me</span>
</label>
</div>
</div>
<div class="form-group">
<div class="col-sm-12">
<button type="submit" class="btn btn-primary"><i class="fa fa-sign-in"></i> Sign in</button>
</div>
</div>
</form>
<br>
<p><em>Don't have an account yet?</em> <strong>Sign Up</strong>
<br>
<em>Forgot your password?</em> Recover Password</p>
<!-- END LOGIN FORM -->
</div>
</div>
<br>
<br>
<div class="row">
<div class="col-md-6">
<!-- LOGIN FORM WITH LABEL -->
<!-- END LOGIN FORM WITH LABEL -->
</div>
<div class="col-md-6">
<!-- SIMPLE FORM -->
<!-- END SIMPLE FORM -->
</div>
</div>
<br>
<br>
<!-- INLINE FORM -->
<!-- END INLINE FORM -->
</div>
</div>
</div>
</div>
<!-- END PAGE CONTENT -->
<!-- FOOTER -->
<footer>
<div class="container">
<div class="row">
<div class="col-md-4">
<!-- COLUMN 1 -->
<h3 class="sr-only">ABOUT US</h3>
<img src="assets/img/logo/repute-logo-light.png" class="logo" alt="Repute">
<p>Proactively aggregate B2B initiatives before extensive channels. Monotonectally extend interactive methods of empowerment through excellent applications. Rapidiously synergize visionary products with sticky technology.</p>
<br>
<address class="margin-bottom-30px">
<ul class="list-unstyled">
<li>Unit 5, Block B Nesfield Road
<br/> Colchester, Essex CO4 3ZL 222222</li>
<li>Phone: 01206 588 000</li>
<li>Email: sales#universalwebdesign.co.uk</li>
</ul>
</address>
<!-- END COLUMN 1 -->
</div>
<div class="col-md-4">
<!-- COLUMN 2 -->
<h3 class="footer-heading">USEFUL LINKS</h3>
<div class="row margin-bottom-30px">
<div class="col-xs-6">
<ul class="list-unstyled footer-nav">
<li>About Us</li>
<li>News</li>
<li>Community</li>
<li>Career</li>
<li>Blog</li>
</ul>
</div>
<div class="col-xs-6">
<ul class="list-unstyled footer-nav">
<li>Press Kit</li>
<li>FAQ</li>
<li>Terms</li>
<li>Privacy Policy</li>
<li>Contact Us</li>
</ul>
</div>
</div>
<!-- END COLUMN 2 -->
</div>
<div class="col-md-4">
<!-- COLUMN 3 -->
<div class="newsletter">
<h3 class="footer-heading">NEWSLETTER</h3>
<p>Get the latest update from us by subscribing to our newsletter.</p>
<form class="newsletter-form" method="POST">
<div class="input-group input-group-lg">
<input type="email" class="form-control" name="email" placeholder="youremail#domain.com">
<span class="input-group-btn"><button class="btn btn-primary" type="button"><i class="fa fa-spinner fa-spin"></i><span>SUBSCRIBE</span></button>
</span>
</div>
<div class="alert"></div>
</form>
</div>
<div class="social-connect">
<h3 class="footer-heading">GET CONNECTED</h3>
<ul class="list-inline social-icons">
<li><i class="fa fa-facebook"></i></li>
<li><i class="fa fa-twitter"></i></li>
<li><i class="fa fa-google-plus"></i></li>
<li><i class="fa fa-rss"></i></li>
</ul>
</div>
<!-- END COLUMN 3 -->
</div>
</div>
</div>
<!-- COPYRIGHT -->
<div class="text-center copyright">
©2018 Bid4MyJob. All Rights Reserved.
</div>
<!-- END COPYRIGHT -->
</footer>
<!-- END FOOTER -->
</div>
<!-- END WRAPPER -->
<!-- JAVASCRIPTS -->
<script src="assets/js/jquery-2.1.1.min.js"></script>
<script src="assets/js/bootstrap.min.js"></script>
<script src="assets/js/plugins/autohidingnavbar/jquery.bootstrap-autohidingnavbar.min.js"></script>
<script src="assets/js/repute-scripts.js"></script>
</body>
</html>
You run the query:
$sql = "SELECT customer_id FROM customer WHERE email_adress = 'a#a.com' and password = 'a'";
this returns multiple results for $count = mysqli_num_rows($result); is thus > 1.
So you always fall into the else:
if($count == 1) {
$_SESSION['login_user'] = $myusername;
header("location: index2.php");
}else {
$error = "Your Login Name or Password is invalid";
}
Try for testing purposes try:
if($count >= 1) {
$_SESSION['login_user'] = $myusername;
header("location: index2.php");
}else {
$error = "Your Login Name or Password is invalid";
}
ofc later you will have to make sure you can't sign up email addresses twice.
Secondly you might want to forward to your session.php instead?
header("location:session.php"); // instead of index2.php

How to use PHP display unique user data to a html page

currently I have a website with a basic login, I was just wondering how I would display a the name,skill and description of the unique user who is logged in.This is what I have done so far.I can only find articles on how to display data into table.This is the updated code:
<?php
include('session.php');
require 'config.php';
$sql = "SELECT * FROM profile";
$result = $conn->query($sql);
//echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
?>
<html>
<head>
<link rel="stylesheet" type="text/css" href="profile.css">
</head>
<body>
<ul>
<li>Home</li>
<li>PostJob</li>
<li>Find Job</li>
<li>How It Works</li>
<li>Notifications</li>
<li>Message</li>
<li>profile</li>
</ul>
<h1>Welcome To Bid4MyJob</h1>
<li>edit profile</li>
<div id="ProfilePage">
<div id="LeftCol">
<div id="Photo"></div>
<div id="ProfileOptions">
a
</div>
</div>
<div id="Info">
<p>
<strong>Name:<?php echo $row["name"]?></strong>
<!--<span>James</span>-->
</p>
<p>
<strong>Skill:<?php echo $row["skill"]?><</strong>
<!--span>James</span>-->
</p>
<!-- <p>
<strong>review:<?php /*echo $row["review"]*/?><</strong>
<span>james</span>
</p> -->
<p>
<strong>Description:<?php echo $row["description"]?><</strong>
<span>James</span>
</p>
<!--<p>
<strong>Name:</strong>
<span>james</span>
</p>-->
</div>
<!-- Needed because other elements inside ProfilePage have floats
<div style="clear:both"></div>-->
</div>
</body>
</html>
Picture of database table:
https://imgur.com/a/XYTLY
Session.php:
<?php
include('config.php');
session_start();
$user_check = $_SESSION['login_user'];
$ses_sql = mysqli_query($conn,"select email_adress from customer where email_adress = '$user_check' ");
$row = mysqli_fetch_array($ses_sql,MYSQLI_ASSOC);
$login_session = $row['email_adress'];
if(!isset($_SESSION['login_user'])){
header("location:login.php");
}
?>
Login.php:
<?php
include("config.php");
session_start();
if($_SERVER["REQUEST_METHOD"] == "POST") {
// username and password sent from form
$myusername = mysqli_real_escape_string($conn,$_POST['username']);
$mypassword = mysqli_real_escape_string($conn,$_POST['password']);
$sql = "SELECT customer_id FROM customer WHERE email_adress = '$myusername' and password = '$mypassword'";
$result = mysqli_query($conn,$sql);
$row = mysqli_fetch_array($result,MYSQLI_ASSOC);
$active = $row['customer_id'];
$count = mysqli_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row
if($count == 1) {
$_SESSION['login_user'] = $myusername;
header("location: index2.php");
}else {
$error = "Your Login Name or Password is invalid";
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Login | Bid4MyJob</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Bid4MyJob">
<meta name="author" content="James Wood">
<!-- CSS -->
<link href="assets/css/bootstrap.css" rel="stylesheet" type="text/css">
<link href="assets/css/font-awesome.min.css" rel="stylesheet" type="text/css">
<link href="assets/css/main.css" rel="stylesheet" type="text/css">
<link href="assets/css/my-custom-styles.css" rel="stylesheet" type="text/css">
<!-- IE 9 Fallback-->
<!--[if IE 9]>
<link href="assets/css/ie.css" rel="stylesheet">
<![endif]-->
<!-- GOOGLE FONTS -->
<link href='http://fonts.googleapis.com/css?family=Open+Sans:300,400italic,400,600,700' rel='stylesheet' type='text/css'>
<link href='http://fonts.googleapis.com/css?family=Roboto+Condensed:300,300italic,400italic,700,400,300' rel='stylesheet' type='text/css'>
<!-- FAVICONS -->
<link rel="apple-touch-icon-precomposed" sizes="144x144" href="assets/ico/repute144x144.png">
<link rel="apple-touch-icon-precomposed" sizes="114x114" href="assets/ico/repute114x114.png">
<link rel="apple-touch-icon-precomposed" sizes="72x72" href="assets/ico/repute72x72.png">
<link rel="apple-touch-icon-precomposed" href="assets/ico/repute57x57.png">
<link rel="shortcut icon" href="assets/ico/favicon.png">
</head>
<body>
<!-- WRAPPER -->
<div class="wrapper">
<!-- NAVBAR -->
<nav class="navbar navbar-default " role="navigation">
<div class="container">
<!-- TOPBAR -->
<div class="topbar">
<ul class="list-inline top-nav">
<li>
<div class="btn-group">
<button type="button" class="btn btn-link dropdown-toggle btn-xs" data-toggle="dropdown"><img src="assets/img/flags/United-Kingdom.png" alt="United Kingdom"> United Kingdom <span class="caret"></span></button>
<ul class="dropdown-menu dropdown-menu-right country-selector" role="menu">
<li>
<img src="assets/img/flags/United-Kingdom.png" alt="United Kingdom"> United Kingdom
</li>
<li>
<img src="assets/img/flags/Japan.png" alt="Japan"> Japan
</li>
<li>
<img src="assets/img/flags/China.png" alt="China"> China
</li>
<li>
<img src="assets/img/flags/Germany.png" alt="Germany"> Germany
</li>
</ul>
</div>
</li>
<li>Help</li>
<li>Support</li>
</ul>
<div class="searchbox">
<form method="post">
<div class="input-group input-group-sm">
<input type="text" class="form-control" placeholder="search ...">
<span class="input-group-btn">
<button class="btn btn-default" type="button"><i class="fa fa-search"></i></button>
</span>
</div>
</form>
</div>
</div>
<!-- END TOPBAR -->
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#main-nav">
<span class="sr-only">Toggle Navigation</span>
<i class="fa fa-bars"></i>
</button>
<a href="index.html" class="navbar-brand navbar-logo navbar-logo-bigger">
</a>
</div>
<!-- MAIN NAVIGATION -->
<div id="main-nav" class="navbar-collapse collapse">
<ul class="nav navbar-nav navbar-right">
<li>
HOME
</li>
<li>
HOW IT WORKS
</li>
<li>
POST JOB
</li>
<li>
FIND JOB
</li>
<li>
SIGN UP
</li>
<li>
LOGIN
</li>
</ul>
</div>
<!-- END MAIN NAVIGATION -->
</div>
</nav>
<!-- END NAVBAR -->
<!-- BREADCRUMBS -->
<div class="page-header">
<div class="container">
<h1 class="page-title pull-left">Login</h1>
<ol class="breadcrumb">
<li>Home</li>
<li class="active">Login</li>
</ol>
</div>
</div>
<!-- END BREADCRUMBS -->
<!-- PAGE CONTENT -->
<div class="page-content">
<div class="col-md-6">
<!-- LOGIN FORM -->
<h2 class="section-heading">Login Form</h2>
<form class="form-horizontal" role="form" action = "" method = "post">
<div class="form-group">
<label for="username" class="control-label sr-only">Email</label>
<div class="col-sm-12">
<div class="input-group">
<input type="email" class="form-control" id="username" name = "username" placeholder="Email">
<span class="input-group-addon"><i class="fa fa-envelope"></i></span>
</div>
</div>
</div>
<div class="form-group">
<label for="password" class="control-label sr-only">Password</label>
<div class="col-sm-12">
<div class="input-group">
<input type="password" class="form-control" id="password" name="password" placeholder="Password">
<span class="input-group-addon"><i class="fa fa-lock"></i></span>
</div>
</div>
</div>
<div class="form-group">
<div class="col-sm-12">
<label class="fancy-checkbox">
<input type="checkbox">
<span>Remember me</span>
</label>
</div>
</div>
<div class="form-group">
<div class="col-sm-12">
<button type="submit" class="btn btn-primary"><i class="fa fa-sign-in"></i> Sign in</button>
</div>
</div>
</form>
<br>
<p><em>Don't have an account yet?</em> <strong>Sign Up</strong>
<br>
<em>Forgot your password?</em> Recover Password</p>
<!-- END LOGIN FORM -->
</div>
</div>
<br>
<br>
<div class="row">
<div class="col-md-6">
<!-- LOGIN FORM WITH LABEL -->
<!-- END LOGIN FORM WITH LABEL -->
</div>
<div class="col-md-6">
<!-- SIMPLE FORM -->
<!-- END SIMPLE FORM -->
</div>
</div>
<br>
<br>
<!-- INLINE FORM -->
<!-- END INLINE FORM -->
</div>
</div>
</div>
</div>
<!-- END PAGE CONTENT -->
<!-- FOOTER -->
<footer>
<div class="container">
<div class="row">
<div class="col-md-4">
<!-- COLUMN 1 -->
<h3 class="sr-only">ABOUT US</h3>
<img src="assets/img/logo/repute-logo-light.png" class="logo" alt="Repute">
<p>Proactively aggregate B2B initiatives before extensive channels. Monotonectally extend interactive methods of empowerment through excellent applications. Rapidiously synergize visionary products with sticky technology.</p>
<br>
<address class="margin-bottom-30px">
<ul class="list-unstyled">
<li>Unit 5, Block B Nesfield Road
<br/> Colchester, Essex CO4 3ZL 222222</li>
<li>Phone: 01206 588 000</li>
<li>Email: sales#universalwebdesign.co.uk</li>
</ul>
</address>
<!-- END COLUMN 1 -->
</div>
<div class="col-md-4">
<!-- COLUMN 2 -->
<h3 class="footer-heading">USEFUL LINKS</h3>
<div class="row margin-bottom-30px">
<div class="col-xs-6">
<ul class="list-unstyled footer-nav">
<li>About Us</li>
<li>News</li>
<li>Community</li>
<li>Career</li>
<li>Blog</li>
</ul>
</div>
<div class="col-xs-6">
<ul class="list-unstyled footer-nav">
<li>Press Kit</li>
<li>FAQ</li>
<li>Terms</li>
<li>Privacy Policy</li>
<li>Contact Us</li>
</ul>
</div>
</div>
<!-- END COLUMN 2 -->
</div>
<div class="col-md-4">
<!-- COLUMN 3 -->
<div class="newsletter">
<h3 class="footer-heading">NEWSLETTER</h3>
<p>Get the latest update from us by subscribing to our newsletter.</p>
<form class="newsletter-form" method="POST">
<div class="input-group input-group-lg">
<input type="email" class="form-control" name="email" placeholder="youremail#domain.com">
<span class="input-group-btn"><button class="btn btn-primary" type="button"><i class="fa fa-spinner fa-spin"></i><span>SUBSCRIBE</span></button>
</span>
</div>
<div class="alert"></div>
</form>
</div>
<div class="social-connect">
<h3 class="footer-heading">GET CONNECTED</h3>
<ul class="list-inline social-icons">
<li><i class="fa fa-facebook"></i></li>
<li><i class="fa fa-twitter"></i></li>
<li><i class="fa fa-google-plus"></i></li>
<li><i class="fa fa-rss"></i></li>
</ul>
</div>
<!-- END COLUMN 3 -->
</div>
</div>
</div>
<!-- COPYRIGHT -->
<div class="text-center copyright">
©2018 Bid4MyJob. All Rights Reserved.
</div>
<!-- END COPYRIGHT -->
</footer>
<!-- END FOOTER -->
</div>
<!-- END WRAPPER -->
<!-- JAVASCRIPTS -->
<script src="assets/js/jquery-2.1.1.min.js"></script>
<script src="assets/js/bootstrap.min.js"></script>
<script src="assets/js/plugins/autohidingnavbar/jquery.bootstrap-autohidingnavbar.min.js"></script>
<script src="assets/js/repute-scripts.js"></script>
</body>
</html>
You need a field on Profile that links to the customer table (or viceversa).
email can be in both tables, then you can link to your $login_session
SELECT * FROM profile WHERE email = '$login_session' LIMIT 1
In this case, take care and do not let users to change their email (without verification they own it) and do not let create a new account with the same credentials as another one.
But as a good practice on DB, it is better to have profile_id on customer
session.php
$ses_sql = mysqli_query($conn,"select email_adress, profile_id from customer where email_adress = '$user_check' ");
index.php
$sql = "SELECT * FROM profile WHERE profile_id = $profile_id LIMIT 1";
BTW:
- Try to separate concerns, between database and presentation layer. You should read more about MVC.

PHP PDO trying to get navbar to change when logged in

I am trying to get my navbar to change when logged in so that the register and sign in turn into the user's username I have my code for this and I am not understanding why it's not working...
signin.php
<?php
include 'header.php';
include 'ini.php';
$username = "";
$password = "";
if(isset($_POST['login'])) {
if ($userQuery->execute()) {
while ($row = $userQuery->fetch()) {
$username = $_POST["username"];
$password = $_POST["password"];
$userQuery = $pdo->prepare("SELECT * FROM users WHERE username LIKE :username");
$addUserQuery->bindParam(":username", $username);
if (
username == $row['User_Username'] && password == $row['User_Password']
) {
$_SESSION['loggedin'] = "true";
$_SESSION['username'] = $username;
}
}
}
}
?>
<div class="container">
<div class="row">
<div class='col-md-3'></div>
<div class="col-md-6">
<div class="login-box well">
<form action="index.php" id="login" name="login">
<legend>Sign In</legend>
<div class="form-group">
<label for="username">Username</label>
<input value='' id="username" placeholder="Username" type="text" class="form-control" />
</div>
<div class="form-group">
<label for="password">Password</label>
<input id="password" value='' placeholder="Password" type="text" class="form-control" />
</div>
<div class="input-group">
<div class="checkbox">
<label>
<input id="login-remember" type="checkbox" name="remember" value="1"> Remember me
</label>
</div>
</div>
<div class="form-group">
<input type="submit" id="login" name="login" class="btn btn-default btn-login-submit btn-block m-t-md" value="Login" />
</div>
<span class='text-center'>Forgot Password?</span>
<div class="form-group">
<p class="text-center m-t-xs text-sm">Do not have an account?</p>
Create an account
</div>
</form>
</div>
</div>
<div class='col-md-3'></div>
</div>
</div>
<?php
include 'footer.php';
?>
header.php
<!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">
<meta name="description" content="Web Development company providing HTML, CSS, PHP and JS in your local area">
<meta name="author" content="Kieran Brownfield">
<title>Web Development Penzance - Providing Web Development in your area</title>
<!-- Bootstrap Core CSS -->
<link href="css/bootstrap.min.css" rel="stylesheet"/>
<link href="css/main.css" rel="stylesheet"/>
<!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
<script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body>
<header class="header">Web Development Penzance</header>
<!-- Navigation -->
<nav class="navbar navbar-inverse navbar-static-top" role="navigation">
<div class="container">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li>
Home
</li>
<li>
About
</li>
<li>
Contact
</li>
<li>
Gallery
</li>
<li>
Meet the Team
</li>
</ul>
<ul class="nav navbar-nav pull-right">
<?php
if (isset($_SESSION['loggedin'])) { ?>
<li><p class="navbar-text navbar-right">Signed in as: <?php echo $_SESSION["username"] ?> </p></li>
<li>Logout</li>
<li>Account</li>
<?php } else { ?>
<li>Register</li>
<li>Login</li>
<?php } ?>
</ul>
</div>
<!-- /.navbar-collapse -->
</div>
<!-- /.container -->
</nav>
In order to access the $_SESSION variable across page loads, add session_start() to the top of both of these files. Docs.

Displaying different include file based on logged in status

I'm trying to include a different navigation menu for users that are logged in but seem to be having trouble. I'm currently setting a session when a user successfully authenticates, that session then sets a session variable so we know if the user is logged in or logged out.
If they are logged in, they should see the logged in menu, otherwise they should just see the logged out menu. For some reason i cannot get this to work through checking if the user is logged in - not sure what i'm doing wrong.
index.php
<?php
require_once("inc/config.php");
require(ROOT_PATH . "inc/database.php");
session_start();
?>
<!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>Property Rental</title>
<!-- CSS -->
<link href="css/bootstrap.min.css" rel="stylesheet">
<link href="css/style.css" rel="stylesheet">
<link href="css/responsive.css" rel="stylesheet">
<!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
<script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body>
<header role="banner" id="top" class="navbar navbar-static-top bs-docs-nav">
<div class="container">
<div class="navbar-header">
<button data-target=".bs-navbar-collapse" data-toggle="collapse" type="button" class="navbar-toggle">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
</div>
<nav role="navigation" class="collapse navbar-collapse bs-navbar-collapse">
<ul class="nav navbar-nav">
<li class="active">
Menu Item 1
</li>
<li>
Menu Item 2
</li>
<li>
Menu Item 3
</li>
<li>
Menu Item 4
</li>
<li>
Menu Item 5
</li>
<?php
if ( $_SESSION['loggedin'] == true) {
include("inc/logged.php");
} else {
include("inc/loggedOut.php");
}
print_r($_SESSION);
?>
</ul>
</nav>
</div>
</header>
<div class="container">
<h1>Hello, world!</h1>
</div>
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script src="/js/bootstrap.js"></script>
<script src="/js/core.js"></script>
</body>
</html>
users.database.php
<?php
$username = $_POST['username'];
$password = $_POST['password'];
try {
$result = $db->prepare("SELECT username, pass FROM user_info WHERE username = :user AND BINARY pass = :pass");
$result->bindParam(':user', $username);
$result->bindParam(':pass', $password);
$result->execute();
$rows = $result->fetch(PDO::FETCH_NUM);
}
catch (Exception $e) {
echo "Could not retrieve data from database";
exit();
}
if ($password = $rows) {
session_start();
$_SESSION['username'] = $_POST['username'];
$_SESSION['loggedin'] = true;
} else {
if (isset($_POST['login'])) {
echo "Username or password incorrect (passwords are case sensitive)";
}
}
?>
logged.php
<li class="login-register pull-right">
<ul>
<li class="login pull-left">
My Account
<div class="login-box">
<div class="login-box-inner">
Logout
</div>
</form>
</div>
</li>
</ul>
</li>
loggedOut.php
<li class="login-register pull-right">
<ul>
<li class="login pull-left">
Login
LOGOUT
<div class="login-box">
<div class="login-box-inner">
<div class="up-arrow"></div>
<form role="form" method="post">
<div class="form-group">
<label>Username or Email</label>
<input type="text" name="username">
</div>
<div class="form-group">
<label>Password</label>
<input type="password" name="password">
</div>
<?php require(ROOT_PATH . "inc/users.database.php"); ?>
<div class="form-group">
<div class="row">
<div class="col-xs-6">
<label class="remember-me">
<input type="checkbox" name="remember"> Remember me
</label>
</div>
<div class="col-xs-6">
<button type="submit" name="login" class="btn btn-small btn-primary pull-right">Login</button>
</div>
</div>
</div>
</form>
</div>
</form>
</div>
</li>
<span class="slash">/</span>
<li class="pull-right">
Register
</li>
</ul>
</li>
$loggedin is not being set in your index.php file.
Replace your if condition with this:
if ($_SESSION['loggedin'] == true) {
You can also use this which has the exact same meaning...
if ( $_SESSION['loggedin'] ) {
With the above code, you are going to need to start a session on that page with session_start();
If you are using javascript to go back in the history when a user logs in, change it to this...
<script>
window.location.href = document.referrer;
</script>
NOTE: This will only go back one page in the history.

Categories