Issue
I am trying to to display this table information from the database based on the location but no matter what email id i enetr its only displaying data from lacation oxford. Can anyone please let me know what is wrong with the code i am new to php and trying to get some hands on expirience Thanks in advance Here is a snippet of my code
<?php
if (isset($_POST['submitbutton']))
{
$email = $_POST['email'];
$password = $_POST['password'];
$sql = "SELECT email,password from users WHERE email = '".$email."' AND password ='".$password."'";
$result = $conn->query($sql);
if ($email = "[email protected]")
{
echo 'The email is'. $email;
$query = "Select * from stock WHERE location = 'oxford'";
$result1 = $conn->query($query);
if ($result1->num_rows > 0){
?>
<table class="reports">>
<thead>
<tr>
<th> Item name </th>
<th> Price </th>
<th> Quantity </th>
<th> Quantity Damaged </th>
</tr> </thead> <tbody>
<?php
while( $row = $result1->fetch_assoc() ){
echo
"<tr>
<td>{$row['item_name']}</td>
<td>{$row['price']}</td>
<td>{$row['quantity']}</td>
<td>{$row['quantity_damaged']}</td>
</tr>\n";
} ?> </tbody>
</table>
<?php
}
}
elseif ($email = "[email protected]")
{
echo 'The email is'. $email;
$query1 = "Select * from stock WHERE location = 'bridgeport'";
$resut2 = $conn->query($query1);
if ($result2->num_rows > 0){
?>
<table class="reports">
<thead>
<tr>
<th> Item name </th>
<th> Price </th>
<th> Quantity </th>
<th> Quantity Damaged </th>
</tr> </thead> <tbody>
<?php
while( $row = $result2->fetch_assoc() ){
echo
"<tr>
<td>{$row['item_name']}</td>
<td>{$row['price']}</td>
<td>{$row['quantity']}</td>
<td>{$row['quantity_damaged']}</td>
</tr>\n";
} ?>
Solution
You are assigning values in your if statements:
if ($email = "[email protected]")
When you assign values, the value of the assignment is returned ("[email protected]"
). This string is "truthy" (evaluates as true
) so the first if
statement runs and all the else
's are ignored.
You should be using a comparison operator - ==
or ===
.
if ($email === "[email protected]") { //... }
elseif ($email === "[email protected]") { //... }
Answered By - Scopey