no data outputted after login

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
ianhaney
New php-forum User
New php-forum User
Posts: 62
Joined: Tue May 01, 2012 12:30 pm

Fri May 18, 2018 7:31 am

I need some help with no data being outputted after logging in, I don't get any errors showing, it's just not showing any data for the specific user. My code is below

Code: Select all

<?php

ini_set('display_startup_errors',1);
ini_set('display_errors',1);
error_reporting(-1);

$connect = mysqli_connect("localhost", "username", "password", "dbname");
 
 //entry.php  
 session_start();  
 if(!isset($_SESSION["username"]))  
 {  
      header("location:login.php?action=login");  
 }
 
  //define page title
$title = 'Repair Job/Status';

//include header template
require('layout/header.php'); 
   
 ?>  
 
 <!-- Content  -->
	<div id="page-content">
		<!-- Breadcrumbs Block -->
		<div class="block">
			<div class="container">
				<div class="breadcrumbs">
					<ul class="breadcrumb">
						<li><a href="/">Home</a></li>
						<li class="active">Repair Job/Status</li>
					</ul>
				</div>
			</div>
		</div>
		<!-- //Breadcrumbs Block -->
		<!-- Category Block -->
		<div class="block">
			<div class="container">
				<h1 class="text-center">Repair <span class="color">Job/Status</span></h1>

	    <div class="col-xs-12 col-sm-8 col-md-6 col-sm-offset-2 col-md-offset-3">
 
                <?php

				
				$result = $connect->query("SELECT customer_ref FROM members WHERE username='{$_SESSION['username']}'");

				$ref = mysqli_fetch_assoc($result);
				
				foreach($connect->query("SELECT repairs.customer_ref, members.customer_ref, repairs.customer_name, repairs.customer_email, repairs.customer_phone, repairs.computer_make, repairs.computer_model, repairs.status, DATE_FORMAT(exrdate, '%d/%m/%Y') AS exrdate, repairs.exrtime, repairs.comments, repairs.job_cost				
FROM repairs
INNER JOIN members ON repairs.customer_ref=members.customer_ref WHERE id ='".$ref['customer_ref']."'") as $row) {
	
                echo "<p>"
				. 'Welcome ' . $row['customer_name'] . ', below is your repair job info and status';
				echo '<br><br>';
				echo '<strong>Your Customer Ref Number:</strong> ' . $row['customer_ref'];
				echo '<br>';
				echo '<strong>Name:</strong> ' . $row['customer_name'];
				echo '<br>';
				echo '<strong>Email:</strong> ' . $row['customer_email'];
				echo '<br>';
				echo '<strong>Phone Number:</strong> ' . $row['customer_phone'];
				echo '<br>';
				echo '<strong>Computer Make:</strong> ' . $row['computer_make'];
				echo '<br>';
				echo '<strong>Computer Model:</strong> ' . $row['computer_model'];
				echo '<br>';
				echo '<strong>Repair Status:</strong> ' . $row['status'];
				echo '<br>';
				echo '<strong>Expected Repair Date and Time:</strong> ' . $row['exrdate'] . ' estimated around ' . $row['exrtime'];
				echo '<br>';
				echo '<strong>Notes:</strong> ' . $row['comments'];
				echo '<br>';
				echo '<strong>Repair Cost:</strong> ' . '&#163;' . $row['job_cost'];
				echo "</p>";
                echo '<label><a href="logout.php">Logout</a></label>';
}
                ?>  
           </div>
           
           </div>
           </div>
           </div>

<?php 
//include footer template
require('layout/footer.php'); 
?>
Thank you in advance

element121
php-forum Fan User
php-forum Fan User
Posts: 628
Joined: Sat Jun 06, 2015 11:46 am
Contact:

Wed May 30, 2018 10:41 am

Hi,

I don't see anything obvious from a quick look at your code, but do you know how far the code is getting, what's being output to the browser when view the source of the page?

Good to throw in some var_dump() statements so you can step through and see what is being returned, for example:

Code: Select all

var_dump($result);
Hope that helps,
Jon

User avatar
benanamen
New php-forum User
New php-forum User
Posts: 45
Joined: Mon Oct 16, 2017 1:06 pm

Wed May 30, 2018 9:53 pm

The problem is glaringly obvious. You cannot use an alias in a WHERE clause.

Per the Manual
An alias can be used in a query select list to give a column a different name. You can use the alias in GROUP BY, ORDER BY, or HAVING clauses to refer to the column.
Standard SQL disallows references to column aliases in a WHERE clause. This restriction is imposed because when the WHERE clause is evaluated, the column value may not yet have been determined.



Additionally you are using two queries when all you need is one.
The XY Problem
The XY problem is asking about your attempted solution (X) rather than your actual problem (Y). This leads to enormous amounts of wasted time and energy, both on the part of people asking for help, and on the part of those providing help.

element121
php-forum Fan User
php-forum Fan User
Posts: 628
Joined: Sat Jun 06, 2015 11:46 am
Contact:

Fri Jun 01, 2018 8:07 am

I don't see the alias in the where clause... Are you looking at this?

Code: Select all

INNER JOIN members ON repairs.customer_ref=members.customer_ref WHERE id ='".$ref['customer_ref']."'") as $row) {

Code: Select all

as $row
isn't part of the SQL query, it's part of the foreach loop.

Post Reply