Click here to Skip to main content
14,330,041 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hello!

I am not getting any errors but when I run my code it doesn't pull anything from the server. It is just blank. I feel like there must be something really obvious I'm missing. 


What I have tried:

<?php
if(isset($_GET['id'])) {
	require_once  'login1.php';

	$id = null; 
	
	$conn = new mysqli($hn, $un, $pw, $db); 
	if($conn->connect_error) die($conn->connect_error); 
	$ID = mysqli_real_escape_string($conn, $_GET['id']);
	
	$sql = "SELECT * FROM freshwater WHERE id='$id' ";
	$result = mysqli_query($conn, $sql) or die("Bad Query: $sql");
	$row = mysqli_fetch_array($result);
	
}
	else {
		header('Location: freshwaterlist.php');
	}
?>
<html>
<head>
<title>Fish R Us - Saltwater Fish</title>
</head>
<body>
<div class="toptext"><center>
<img src="banner.png" height="300"></img></center>
</div>
<center>
<h2><?php echo $row["name"] ?></h2>

<img src="<?php echo $row["picture"] ?>"></img>
<table>
  <tr>
    <th>Name</th>
    <th>Type</th>
    <th>Quantity</th>
	<th>Price</th>
  </tr>
  <tr>
    <td><?php echo $row["name"] ?></td>
    <td><?php echo $row["type"] ?></td>
    <td><?php echo $row["qty"] ?></td>
	<td><?php echo $row["price"] ?></td>
  </tr>
</table><br>
<table class="fish-text">
<th><?php echo $row["description"] ?></th>
</center></table><br>
<button padding: 10px 24px;><a href="products.php">Return to Products List</a></button></body>
<button padding: 10px 24px;><a href="menu.php">Return to Menu</a></button>
</html>
Posted
Updated 19-Apr-19 10:05am
Rate this:
Please Sign up or sign in to vote.

Solution 2

I think the variable name in PHP is case sensitive, in the posted code, there are two id variables with different case

$id = null; 
$ID = mysqli_real_escape_string($conn, $_GET['id']);


The first one ($id) was being assigned a null value and being used in the query, which I think it should be $ID
$sql = "SELECT * FROM freshwater WHERE id='$id' ";


And the code might be vulnerable to SQL injection and XSS attacks.
PHP MySQLi Prepared Statements Tutorial to Prevent SQL Injection[^]

Example of SQL Injection and Cross site Scripting (XSS) attacks:
SQL Injection and Cross-Site Scripting[^]
   
v2
Rate this:
Please Sign up or sign in to vote.

Solution 1

Quote:
I am not getting any errors but when I run my code it doesn't pull anything from the server.

Advice: use the debugger to see exactly what your code is doing.
$sql = "SELECT * FROM freshwater WHERE id='$id' ";

Not necessary a solution to your question, but another problem you have.
Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
SQL Injection Prevention Cheat Sheet - OWASP[^]
How can I explain SQL injection without technical jargon? - Information Security Stack Exchange[^]
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100