Reputation: 15
I'm trying to achieve something with php. I have two address on my admin table I'll like to display to my users. I want to display one of the address (id equals 3) to some specific user ids and the other address (id equals 4) to every other users id. Check my code below
<?php
$servername = "localhost";
$username = "user";
$password = "password";
$dbname = "user";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$id = "SELECT * FROM users WHERE id = '".$_SESSION['user']."'";
if ( ($id != 11303) && ($id != 27)) {
$sql = "SELECT address FROM admin WHERE id='3'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "" . $row["address"]. "";
}
} else {
echo "0 results";
}
}
else {
$sql = "SELECT address FROM admin WHERE id='4'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "" . $row["address"]. "";
}
} else {
echo "0 results";
}
}
$conn->close();
?>
I tried getting user id from their session then i used "if" to check if their session id equals to some specific ids then select the address with id 3 from the admin table and display it "else" it displays the address with id 4. But it's displaying the address with id 3 to everyone. Please can anyone help out?
Upvotes: 0
Views: 1181
Reputation: 122
Before use $_SESSION
write start_session()
at the top of your code.
then try:
$sql = mysqli_query($conn,"SELECT * FROM users WHERE id = '".$_SESSION['user']."'");
$result = mysqli_fetch_assoc($sql);
$id = $result['id'];
Now, Put $id
value in your condition
if ( ($id != 11303) && ($id != 27))
{
//do something
}
Upvotes: 2