I am making a page where it displays a table with staffID and staffName using PHP. When the user clicks the staffID it should then display a new table using the code from another file with extra details about that staffID such as shippingDate, OrderID, etc.
task9.php file below:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>PHP Task 9</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<?php
$sID= isset($_GET['staffID']) ? $_GET['staffID'] : '';
$conn = mysqli_connect('localhost', 'TWA', 'TWA_test', 'factory');
if ( !$conn ) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT s.staffID,s.staffName
FROM staff s";
$results = mysqli_query($conn, $sql)
or die ('Problem with query' . mysqli_error($conn));
?>
<h1> Staff Table </h1>
<table>
<tr>
<th>Staff ID</th>
<th>Staff Name</th>
</tr>
<?php while($row = mysqli_fetch_array($results)) { ?>
<tr>
<td> <a href = "task8.php"><?php echo $row[0] ?></a></td>
<td><?php echo $row[1] ?></td>
</tr>
<?php } ?>
<?php mysqli_close($conn); ?>
</table>
</body>
</html>
task8.php asks a user to enter a staffID and it displays all of the details, if the staffID does not exist then it displays an error. This file works fine and displays everything correctly. task8.php file below:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>PHP Task 8</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<?php
$conn = mysqli_connect('localhost', 'TWA', 'TWA_test', 'factory');
if ( !$conn ) {
die("Connection failed: " . mysqli_connect_error());
}
//obtain the staff ID input from the $_GET array
$sID= isset($_GET['staffID']) ? $_GET['staffID'] : '';
$sql = "SELECT s.staffID, p.orderID, p.orderDate, p.shippingDate,s.staffName
FROM purchase p
INNER JOIN staff s
ON p.staffID = s.staffID
WHERE p.staffID = '$sID'";
$results = mysqli_query($conn, $sql)
or die ('Problem with query' . mysqli_error($conn));
?>
<h1> Order Details </h1>
<?php $rows = mysqli_num_rows($results); ?>
<?php if($rows <= 0){ ?>
<p><?php echo "The staff ID entered is invalid"; ?></p>
<?php } else { ?>
<table>
<tr>
<th>Staff ID</th>
<th>Order ID</th>
<th>Order Date</th>
<th>Shipping Date</th>
<th>Staff Name</th>
</tr>
<?php while ($row = mysqli_fetch_array($results)) { ?>
<tr>
<?php if($row[0] != ""): ?>
<td><?php echo $row[0]; ?></td>
<?php else: ?>
<td><?php echo "N/A"; ?></td>
<?php endif; ?>
<?php if($row[1] != ""): ?>
<td><?php echo $row[1]; ?></td>
<?php else: ?>
<td><?php echo "N/A"; ?></td>
<?php endif; ?>
<?php if($row[2] != ""): ?>
<td><?php echo $row[2]; ?></td>
<?php else: ?>
<td><?php echo "N/A"; ?></td>
<?php endif; ?>
<?php if($row[3] != ""): ?>
<td><?php echo $row[3]; ?></td>
<?php else: ?>
<td><?php echo "N/A"; ?></td>
<?php endif; ?>
<?php if($row[4] != ""): ?>
<td><?php echo $row[4]; ?></td>
<?php else: ?>
<td><?php echo "N/A"; ?></td>
<?php endif; ?>
</tr>
<?php } ?>
<?php } ?>
<?php mysqli_close($conn); ?>
</table>
</body>
</html>
The problem I am facing is that when I click the staffID from the table on task9.php it just shows the error I included in task8.php "The staff ID entered is invalid". I don't know why it isn't displaying the details from task8.php