updating information Mysqli_php - php

i'm trying to make admin panel for simple website
now i'm trying to update the information
but i don't know why it don't update
on ControlPage
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
require_once("../guest.php");
require_once("../db.php");
global $tf_handle;
$gb = new guest();
$id = 0;
//get id from url
if(isset($_GET['id']))
{
$id = (int)$_GET['id'];
}
$message = $gb->getMessage($id);
if(isset($_POST['submit']))
{
$uid = $_POST['id'];
echo $uid;
$name = $_POST['name'];
echo $name;
$msg = $_POST['message'];
echo $msg;
$update = $gb->Update($id,$name,$msg);
if($update)
{
echo('updated');
}
else
{
echo("not updated");
}
}
?>
<!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>Guestbook control panel</title>
<!-- Bootstrap Core CSS -->
<link href="css/bootstrap.min.css" rel="stylesheet">
<!-- Custom CSS -->
<link href="css/sb-admin.css" rel="stylesheet">
<!-- Morris Charts CSS -->
<link href="css/plugins/morris.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.php">GuestBook Admin</a>
</div>
<!-- Top Menu Items -->
<ul class="nav navbar-right top-nav">
<li class="dropdown">
<i class="fa fa-user"></i> Ambaleh <b class="caret"></b>
<ul class="dropdown-menu">
<li>
<i class="fa fa-fw fa-gear"></i> Settings
</li>
<li class="divider"></li>
<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> Dashboard
</li>
<li>
<i class="fa fa-fw fa-bar-chart-o"></i>Messages
</li>
<li>
<i class="fa fa-fw fa-bar-chart-o"></i>Logout
</li>
</ul>
</div>
<!-- /.navbar-collapse -->
</nav>
<div id="page-wrapper">
<div class="container-fluid">
<!-- Page Heading -->
<!-- Page Heading -->
<div class="row">
<div class="col-lg-12">
<h1 class="page-header">
Admin Panel
</h1>
<ol class="breadcrumb">
<li>
<i class="fa fa-dashboard"></i> Dashboard
</li>
<li class="active">
<i class="fa fa-table"></i> Messages
</li>
</ol>
</div>
</div>
<div class="row">
<div class="col-lg-12">
<h2>Update Messages</h2>
<form role="form" action="update.php" method = "post">
<div class="form-group">
<label>Name</label>
<input type="text" name="name" value = '<?php echo $message['name'];?>' class="form-control">
</div>
<div class="form-group">
<label>Message</label>
<textarea name="message" class="form-control" rows="3"><?php echo $message['message'];?></textarea>
</div>
<input type="hidden" name="id" value='<?php echo $id;?>' >
<input type="submit" name="submit" value="save!">
</form>
</div>
</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>
<!-- Morris Charts JavaScript -->
<script src="js/plugins/morris/raphael.min.js"></script>
<script src="js/plugins/morris/morris.min.js"></script>
<script src="js/plugins/morris/morris-data.js"></script>
</body>
</html>
i tried to echo the variables to check
but it already echo & takes the information from the POST request so the problem from another thing
update.php
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
require_once('db.php');
class guest
{
//get all the messages
public function getMessage($id)
{
global $tf_handle;
$querycheck = mysqli_query($tf_handle,"SELECT * FROM `messages` WHERE `id` = $id");
if($querycheck)
{
$message = mysqli_fetch_assoc($querycheck);
return $message;
}
else
{
die('Failed');
}
tinyf_db_close() ;
}
//".$name."
public function Update($id,$name,$message)
{
global $tf_handle;
$query = mysqli_query($tf_handle,"UPDATE `guest`.`messages` SET `name` = ".$name.", `message` = ".$message." WHERE `messages`.`id` = ".$id);
if($query)
{
echo "query Works";
return TRUE;
}
else
{
tinyf_db_close() ;
return False;
}
tinyf_db_close() ;
}
}
?>
i think the problem in this line
$query = mysqli_query($tf_handle,"UPDATE `guest`.`messages` SET `name` = ".$name.", `message` = ".$message." WHERE `messages`.`id` = ".$id);

There may well be other issues but this is the first I see.
text fields in any query need to be wrapped in quotes, single quotes is my preference, so this update wont compile.
$query = mysqli_query($tf_handle,
"UPDATE `guest`.`messages`
SET `name` = ".$name.", `message` = ".$message."
WHERE `messages`.`id` = ".$id);
You can also simplify the building of the query if you remember that double quoted string literals will expand variables automatically
SO Change to
$query = mysqli_query($tf_handle,
"UPDATE `guest`.`messages`
SET `name` = '$name', `message` = '$message' WHERE
`messages`.`id` = $id" );
Also after any mysql calls you should really do something with the actual error message rather than just return false.
if($query) {
echo "query Works";
return true;
} else {
$this->LastError = mysqli_error($tf_handle);
tinyf_db_close() ;
return false;
}
Then in the calling code do
if(isset($_POST['submit']))
{
$uid = $_POST['id']; <-- also change this to
$id = $_POST['id']; <-- this so it matches Update() params
echo $uid;
$name = $_POST['name'];
echo $name;
$msg = $_POST['message'];
echo $msg;
$update = $gb->Update($id,$name,$msg);
if($update) {
echo('updated');
} else {
echo $gb->LastError;
}

Related

Live server takes too much time to fetch the result

Working since last a month, tried many solutions which I got from google, but not getting anything helpful. Code is working perfectly and fast on localhost. But not on the live server (VPS hosting). Please visit http://97.74.37.64/ link and see it takes forever to filter 14000 mobile numbers in DND and NON-DND numbers. But at my localhost it takes only about 20 seconds to complete the same process. I don't understand the reason and don't have time to waste more on this. Please provide your kind solution as earliest.
PS : This website is using GoDaddy VPS hosting with 4GB of RAM.
Below is the code..
Controller (filter.php)
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Filter extends MX_Controller
{
function __construct()
{
parent::__construct();
$this->load->model('Filter_model');
}
public function index()
{
$this->load->view('front_page');
}
public function numbers()
{
set_time_limit(2500);
ini_set('memory_limit', '-1');
//empty the existing table data first
$tru = $this->Filter_model->empty_data();
if (isset($tru))
$mobile_number = $this->input->post('numbers');
//adding comma after each mobile number
if (strpos($mobile_number, "\r\n") !== false) {
$mobile_number = str_replace("\r\n", ',', $mobile_number);
} elseif (strpos($mobile_number, "\n\r") !== false) {
$mobile_number = str_replace("\n\r", ',', $mobile_number);
} elseif (strpos($mobile_number, "\n") !== false) {
$mobile_number = str_replace("\n", ',', $mobile_number);
}
//convert comman seprate string to the array
$mobile_number = explode(",", $mobile_number);
$json_data['number'] = json_encode($mobile_number);
$j_conv = str_replace(']', '', str_replace('"', '', str_replace('[', '', $json_data['number'])));
$e_arr = explode(",", $j_conv);
$res = $this->Filter_model->compare_numbers($e_arr);
$res_nondnd = $this->Filter_model->compare_nondnd_numbers($e_arr);
$array = json_decode(json_encode($res), True);
$array_nondnd = json_decode(json_encode($res_nondnd), True);
//array common words to remove
$common_words = array("e_number]", "=", "n", "0[");
//now convert the data into string again
$url = preg_replace('/[a-z]/', '', str_replace($common_words, '', preg_replace('/&.....[a-z_=]/', ',', urldecode(http_build_query($array)))));
//working here
if (sizeof($mobile_number) <= 15000){
$data['dnd_numbers'] = $array;
$data['not_dnd_numbers'] = $array_nondnd;
$this->load->view('filtered_numbers', $data);
} else {
$this->session->set_flashdata('error', 'Error... Please enter max 15000 numbers at one time');
redirect('filter');
}
}
}
Model (Filter_model.php)
<?php
class Filter_model extends CI_Model {
function __construct() {
parent::__construct();
}
public function empty_data(){
return $this->db->truncate('srchlist');
}
public function compare_numbers($e_arr){
$stmt = "('" . implode("'), ('", $e_arr) . "')";
$ins_res = $this->db->query("INSERT INTO srchlist (number) VALUES $stmt" );
$join_res = $this->db->query("SELECT mobile.phone_number FROM mobile INNER JOIN srchlist ON mobile.phone_number = srchlist.number");
return $join_res->result();
}
public function compare_nondnd_numbers($e_arr){
$join_nondnd_res = $this->db->query("SELECT number FROM `srchlist` F WHERE NOT EXISTS (SELECT phone_number FROM mobile S WHERE F.number = S.phone_number)
");
return $join_nondnd_res->result();
}
public function check_dnd_number($phone_number) {
$this->db->where('phone_number', $phone_number);
$this->db->where('ops_type', 'A');
//$this->db->or_where('ops_type', 'a');
$query = $this->db->get('mobile');
return $query->row('phone_number');
}
public function database_numbers() {
$this->db->select('phone_number');
$this->db->where('ops_type', 'A');
$query = $this->db->get('mobile');
return $query->result();
}
public function scrub_numbers() {
$mobile_number = $this->input->post('numbers');
if (strpos($mobile_number, "\r\n") !== false) {
$mobile_number = str_replace("\r\n", ',', $mobile_number);
} elseif (strpos($mobile_number, "\n\r") !== false) {
$mobile_number = str_replace("\n\r", ',', $mobile_number);
} elseif (strpos($mobile_number, "\n") !== false) {
$mobile_number = str_replace("\n", ',', $mobile_number);
}
$pieces = explode(",", $mobile_number);
if (!empty($pieces) && $pieces[0] != '')
$pieces = array_map(function($v) {
return strlen($v) >= 10 ? substr($v, -10) : $v;
}, $pieces);
$pieces = array_unique($pieces);
$database = $this->database_numbers();
}
}
View (front_page.php)
<!DOCTYPE html>
<html lang="en">
<?php $general = $this->Common_model->get_home(); ?>
<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="<?= $general->metadescription ?>">
<meta name="keyword" content="<?= $general->metadescription ?>">
<meta name="author" content="Jay Chandra || www.shubhtech.in">
<title><?= $general->title ?></title>
<!-- Bootstrap Core CSS -->
<link href="<?= base_url() ?>assets/vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">
<!-- Custom Fonts -->
<link href="<?= base_url() ?>assets/vendor/font-awesome/css/font-awesome.min.css" rel="stylesheet" type="text/css">
<link href='https://fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,800italic,400,300,600,700,800' rel='stylesheet' type='text/css'>
<link href='https://fonts.googleapis.com/css?family=Merriweather:400,300,300italic,400italic,700,700italic,900,900italic' rel='stylesheet' type='text/css'>
<!-- Plugin CSS -->
<link href="<?= base_url() ?>assets/vendor/magnific-popup/magnific-popup.css" rel="stylesheet">
<!-- Theme CSS -->
<link href="<?= base_url() ?>assets/css/creative.min.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="<?= base_url() ?>assets/https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
<script src="<?= base_url() ?>assets/https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body id="page-top">
<nav id="mainNav" class="navbar navbar-default navbar-fixed-top">
<div class="container-fluid">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
<span class="sr-only">Toggle navigation</span> Menu <i class="fa fa-bars"></i>
</button>
<a class="navbar-brand page-scroll" href="<?= base_url() ?>#page-top"><?= $general->sitename ?></a>
</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 navbar-right">
<li>
<a class="page-scroll" href="<?= base_url() ?>#contact">Contact</a>
</li>
</ul>
</div>
<!-- /.navbar-collapse -->
</div>
<!-- /.container-fluid -->
</nav>
<header>
<div class="header-content">
<div class="header-content-inner">
<marquee direction="left" behavior="scroll" scrollamount="5" scrolldelay="100" onMouseOver="stop()" onMouseOut="start()">
<p><?= $general->marquee ?></p>
</marquee>
<p>To filter, You can enter multiple number by one number per line or one number by comma e.g: 8877665544, 9876543210.</p>
<div class="row">
<div class="col-lg-8 col-lg-offset-2">
<form method="post" action="<?= base_url() ?>filter/numbers/#result">
<div class="form-group">
<textarea name="numbers" class="form-control" rows="10" autofocus=""></textarea>
<p>(You can filter 15,000 mobile numbers at one time)</p>
</div>
<div class="form-group">
<input type="submit" class="btn btn-lg btn-success" value="SCRUB IT"/>
</div>
</form>
</div>
</div>
<div class="row">
<div class="col-lg-12">Space for Ad</div>
</div>
</div>
</div>
</header>
<section id="contact">
<div class="container">
<div class="row">
<div class="col-lg-8 col-lg-offset-2 text-center">
<h2 class="section-heading">Let's Get In Touch!</h2>
<hr class="primary">
</div>
<div class="col-lg-4 col-lg-offset-2 text-center">
<i class="fa fa-phone fa-3x sr-contact"></i>
<p><?= $general->contact ?></p>
</div>
<div class="col-lg-4 text-center">
<i class="fa fa-envelope-o fa-3x sr-contact"></i>
<p><?= $general->email ?></p>
</div>
</div>
</div>
</section>
<!-- jQuery -->
<script src="<?= base_url() ?>assets/vendor/jquery/jquery.min.js"></script>
<!-- Bootstrap Core JavaScript -->
<script src="<?= base_url() ?>assets/vendor/bootstrap/js/bootstrap.min.js"></script>
<!-- Plugin JavaScript -->
<script src="<?= base_url() ?>assets/https://cdnjs.cloudflare.com/ajax/libs/jquery-easing/1.3/jquery.easing.min.js"></script>
<script src="<?= base_url() ?>assets/vendor/scrollreveal/scrollreveal.min.js"></script>
<script src="<?= base_url() ?>assets/vendor/magnific-popup/jquery.magnific-popup.min.js"></script>
<!-- Theme JavaScript -->
<script src="<?= base_url() ?>assets/js/creative.min.js"></script>
</body>
</html>
filter number showing view (filtered_numbers.php)
<!DOCTYPE html>
<html lang="en">
<?php $general = $this->Common_model->get_home(); ?>
<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="<?= $general->metadescription ?>">
<meta name="keyword" content="<?= $general->metadescription ?>">
<meta name="author" content="Jay Chandra || www.shubhtech.in">
<title><?= $general->title ?></title>
<!-- Bootstrap Core CSS -->
<link href="<?= base_url() ?>assets/vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">
<!-- Custom Fonts -->
<link href="<?= base_url() ?>assets/vendor/font-awesome/css/font-awesome.min.css" rel="stylesheet" type="text/css">
<link href='https://fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,800italic,400,300,600,700,800' rel='stylesheet' type='text/css'>
<link href='https://fonts.googleapis.com/css?family=Merriweather:400,300,300italic,400italic,700,700italic,900,900italic' rel='stylesheet' type='text/css'>
<!-- Plugin CSS -->
<link href="<?= base_url() ?>assets/vendor/magnific-popup/magnific-popup.css" rel="stylesheet">
<!-- Theme CSS -->
<link href="<?= base_url() ?>assets/css/creative.min.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="<?= base_url() ?>assets/https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
<script src="<?= base_url() ?>assets/https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body id="page-top">
<nav id="mainNav" class="navbar navbar-default navbar-fixed-top">
<div class="container-fluid">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
<span class="sr-only">Toggle navigation</span> Menu <i class="fa fa-bars"></i>
</button>
<a class="navbar-brand page-scroll" href="<?= base_url() ?>#page-top"><?= $general->sitename ?></a>
</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 navbar-right">
<li>
<a class="page-scroll" href="<?= base_url() ?>#contact">Contact</a>
</li>
</ul>
</div>
<!-- /.navbar-collapse -->
</div>
<!-- /.container-fluid -->
</nav>
<header>
<div class="header-content">
<div class="header-content-inner">
<marquee direction="left" behavior="scroll" scrollamount="5" scrolldelay="100" onMouseOver="stop()" onMouseOut="start()">
<p><?= $general->marquee ?></p>
</marquee>
<p>To filter, You can enter multiple number by one number per line or one number by comma e.g: 8877665544, 9876543210.</p>
<div class="row">
<div class="col-lg-8 col-lg-offset-2">
<form method="post" action="<?= base_url() ?>filter/numbers/#result">
<div class="form-group">
<textarea name="numbers" class="form-control" rows="10" autofocus=""></textarea>
<p>(You can filter 15,000 mobile numbers at one time)</p>
</div>
<div class="form-group">
<input type="submit" class="btn btn-lg btn-success" value="SCRUB IT"/>
</div>
</form>
</div>
</div>
<div class="row">
<div class="col-lg-12">Space for Ad</div>
</div>
</div>
</div>
</header>
<section class="bg-primary" id="result">
<div class="container">
<div class="row">
<div class="col-lg-4 col-lg-offset-2 text-center">
<h2 class="section-heading">DND Numbers</h2>
<hr class="light">
<textarea class="form-control" rows="20"><?php
foreach ($dnd_numbers as $list) {
echo "$list[phone_number]\r\n";
}
?></textarea>
</div>
<div class="col-lg-4 text-center">
<h2 class="section-heading">NON DND Numbers</h2>
<hr class="light">
<textarea class="form-control" rows="20"><?php
foreach ($not_dnd_numbers as $list) {
echo "$list[number]\r\n";
}
?></textarea>
</div>
</div>
</div>
</section>
<section id="contact">
<div class="container">
<div class="row">
<div class="col-lg-8 col-lg-offset-2 text-center">
<h2 class="section-heading">Let's Get In Touch!</h2>
<hr class="primary">
</div>
<div class="col-lg-4 col-lg-offset-2 text-center">
<i class="fa fa-phone fa-3x sr-contact"></i>
<p><?= $general->contact ?></p>
</div>
<div class="col-lg-4 text-center">
<i class="fa fa-envelope-o fa-3x sr-contact"></i>
<p><?= $general->email ?></p>
</div>
</div>
</div>
</section>
<!-- jQuery -->
<script src="<?= base_url() ?>assets/vendor/jquery/jquery.min.js"></script>
<!-- Bootstrap Core JavaScript -->
<script src="<?= base_url() ?>assets/vendor/bootstrap/js/bootstrap.min.js"></script>
<!-- Plugin JavaScript -->
<script src="<?= base_url() ?>assets/https://cdnjs.cloudflare.com/ajax/libs/jquery-easing/1.3/jquery.easing.min.js"></script>
<script src="<?= base_url() ?>assets/vendor/scrollreveal/scrollreveal.min.js"></script>
<script src="<?= base_url() ?>assets/vendor/magnific-popup/jquery.magnific-popup.min.js"></script>
<!-- Theme JavaScript -->
<script src="<?= base_url() ?>assets/js/creative.min.js"></script>
</body>
</html>

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.

Update query won't update my product list

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'));

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.

Specifying output location of PHP queries

I'm working on a small PHP project as an introduction to the language and am having trouble with formatting database query results. My PHP functions are working correctly, but the results are being displayed above all of the HMTL, like this:
Here's my code, all in a file called index.php. Could somebody point out where I'm going wrong? I want it to display right below the navigation bar (Home, Friend List, etc), How do I specify exactly where I want he PHP to appear in the HTML?
#!/usr/local/bin/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">
<title>BumpIt</title>
<!-- Bootstrap -->
<link href="css/bootstrap.css" rel="stylesheet">
<!-- Custom CSS -->
<link href="css/custom.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>
<!-- Login PHP -->
<?php
ini_set("display_errors", true);
session_start();
// Store username and password in session variables
if (isset($_POST['signin_user']))
{
$_SESSION['signin_user']=$_POST['signin_user'];
$_SESSION['signin_pass']=$_POST['signin_pass'];
}
// Reset username and pass to that of the session variables
$signin_user = $_SESSION['signin_user'];
$signin_pass = $_SESSION['signin_pass'];
$isSigningIn = $_POST['isSigningIn'];
// Connect to the database, check if error happens
$conn = pg_connect(Redacted*);
$_SESSION['conn'] = $conn;
if (!$conn) {
echo "Connection failed";
exit;
}
/**************************************************************
**************************************************************
***************** FUNCTIONS ***************************
**************************************************************
***************************************************************/
// Load Friend List function
if($_GET['friendList']){friendList();}
function friendList(){
$userID = $_SESSION['userID'];
$query = sprintf("SELECT username FROM (SELECT friend_id FROM friends WHERE user_id = $userID) AS currFriends, users WHERE friend_id = user_id;");
$result = pg_query($_SESSION['conn'], $query);
echo "<table class='table table-striped table-bordered table-hover'>\n";
echo "<caption>Friend List</caption>\n";
while ($line=pg_fetch_array($result, null, PGSQL_ASSOC)) {
echo "\t<tr>\n";
foreach ($line as $col_value) {
echo "\t\t<td>$col_value</td>\n";
}
echo "\t</tr>\n";
}
echo "</table>\n";
}
// LOGIC NOT WORKING RIGHT YET (DISPLAYING MULTIPLES OF SAME NAME)
// Find Friends function
if($_POST['search']){searchForFriends();}
function searchForFriends() {
$search = $_POST['search'];
$userID = $_SESSION['userID'];
$query = sprintf("SELECT userName FROM (SELECT friend_id FROM friends WHERE user_id = $userID) AS currFriends, users WHERE friend_id <> users.user_id AND userName ILIKE ( '%%' || "."'".$search."'"." || '%%');");
echo "DEBUG PURPOSES-> SEARCHFORFRIENDS QUERY IS: ".$query;
$result = pg_query($_SESSION['conn'], $query);
echo "<table class='table table-striped table-bordered table-hover'>\n";
echo "<caption>Users</caption>\n";
while ($line=pg_fetch_array($result, null, PGSQL_ASSOC)) {
echo "\t<tr>\n";
foreach ($line as $col_value) {
echo "\t\t<td>$col_value</td>\n";
}
echo "\t</tr>\n";
}
echo "</table>\n";
}
?>
<img class="header-logo" src="img/bumpit.png"></img>
<!-- Navigation Bar -->
<nav class="navbar navbar-default" role="navigation">
<div class="container-fluid">
<!-- 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 class="active">Home</li>
<li><a id="friendList" href="index.php?friendList=true">Friend List</a></li>
<li>
<a id="logoutBtn" href="index.php?logoutBtn=true">Logout</a>
</li>
<li class="dropdown">
Dropdown <b class="caret"></b>
<ul class="dropdown-menu">
<li>Another action</li>
<li>Something else here</li>
<li class="divider"></li>
<li>Separated link</li>
<li class="divider"></li>
<li>One more separated link</li>
</ul>
</li>
</ul>
<!-- Search bar -->
<form class="navbar-form navbar-right" role="search" action="index.php?search=true" method="post">
<div class="form-group">
<input name="search" type="text" class="form-control" placeholder="Search">
<input name="isSigningIn" type="hidden" value="1">
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
</div><!-- /.navbar-collapse -->
</div><!-- /.container-fluid -->
</nav>
<!-- Main content -->
<div class="container">
<div class="starter-template">
<!-- WE WANT THE FRIENDS LIST HERE -->
</div>
</div><!-- /.container -->
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="js/bootstrap.min.js"></script>
</body>
I believe you want to move this:
// Load Friend List function
if($_GET['friendList']){friendList();}
To where you want the friendList printed:
<div class="container">
<div class="starter-template">
<!-- WE WANT THE FRIENDS LIST HERE -->
<?php
// Load Friend List function
if (isset($_GET['friendList'])) { friendList(); }
?>
</div>
</div><!-- /.container -->
You have to add it in <?php ?> and I have also altered if statement so it does not throw a notice if friendList wouldn't be set.

Categories