0 votes
in Databases by (1.9k points)

I am working on the following project : 
Basically a user are able to add a book into the database. The thing that I am trying to achieve is displaying the image of the book that the particular user has added on their user profile. So that when the user logs in they are able to see the images of all of the books that they added.

I have 2 tables called book & user where the field, id is connected.

if (isset($_SESSION['user_id'])) {
$msg = "You are already logged in.<br/><a href='index.php'>Home</a>";
$msg = "<a href ='logout.php'>logout</a>";
} else { //user is not logged in
//check whether form input 'username' contains value
if (isset($_POST['username'])) {
    //retrieve form data
    $entered_username = $_POST['username'];
    $entered_password = $_POST['password'];
    //connect to database
    include ("dbfunctions.php");
    //match the username and password entered with database record
    $query = "SELECT *from role,user
              WHERE user_name='$entered_username' AND 
              PASSWORD = SHA1('$entered_password') AND user.role_id = role.role_id";
    $result = mysqli_query($link, $query) or die(mysqli_error($link));
    $query2 = "SELECT * FROM user,country where user.country_id=country.country_id ORDER BY `user`.`id` ASC";
    $result2 = mysqli_query($link, $query2) or die(mysqli_error($link));
    $query3 = "SELECT * FROM user, book WHERE book.id = user.id ";
    $result3 = mysqli_query($link, $query3) or die(mysqli_error($link));

    if (mysqli_num_rows($result) == 1) {
        $update = "UPDATE `user` SET last_login = NOW() WHERE user_name='$entered_username' ";
        $resultupdate = mysqli_query($link, $update);
        $row = mysqli_fetch_array($result);
        $_SESSION['user_id'] = $row['id'];
        $_SESSION['username'] = $row['user_name'];
        $_SESSION['email'] = $row['email_address'];
        $_SESSION['gender'] = $row['gender_id'];
        $_SESSION['role_id'] = $row['role_type'];
        $_SESSION['lastlog'] = $row['last_login'];
        $msg1 = $_SESSION['username'];
        $msg2 = "<b>Gender: </b> " . $_SESSION['gender'] . "<br/>";
        $msg3 = "<b>Email: </b>" . $_SESSION['email'] . "<br/>";
        $msg4 = "<b>Your last visit on this site: </b>" .  $_SESSION['lastlog'];


    } else { //record not found
       echo "No record found";

    }
}
}

   if (mysqli_num_rows($result3) == 1) {

   $rowz = mysqli_fetch_array($result3);
   $_SESSION['img'] = $rowz['image'];
   $image= $_SESSION['img'];

    } else { //record not found
        echo "No record found";

    }
?>
......................................................................................

The html:

  <?php
        } elseif ($_SESSION['role_id'] == "Member") {
            ?>
 <div class="panel panel-default">
 <div class="panel-heading"><i>Books</i> you add</div>
 <div class="panel-body"><?php echo $image;?></div></div>
 }

After doing this, when I tried logging in for every sample user, the same image appeared even though the image was only added by 1 user. How do I fix this ?

1 Answer

0 votes
by (2.4k points)

Check the below code (from the sample you had given)

if (mysqli_num_rows($result3) == 1) {

}

Write the code between the brackets.

...