简体   繁体   中英

Show different data in webpage when clicked depending on the ID

I am trying to show user data when clicking link depending on a story ID. However, the page is showing the same data from one row in the database every time (web link is showing a different ID 'php?story_id=10' etc. but it's still showing the same data.

I have this function:

function displayStories(){
include('conn/conn.php');

$queryread = "SELECT Users.ID, Users.FirstName, Stories.Age, Stories.Story, 
Stories.Image FROM `Stories` INNER JOIN `Users` ON Users.ID = Stories.User_ID";


$result = mysqli_query($conn, $queryread) or die(mysqli_error($conn));

if(mysqli_num_rows($result) > 0 ){

while($row = mysqli_fetch_assoc($result)){

$name = $row["FirstName"];
$idData = $row["ID"];
$age = $row["Age"];
$story = $row["Story"];
$limit = mb_strimwidth($story, 0, 300, "...");


echo 


"<div class='stories'>
          <a href='storyDis.php?story_id=$idData'>
          <h5><b>$name</b></h5></a>
          <p><b>$age years old</b></p>
          <p>$limit</p>

</div>


";

}
}
mysqli_close($conn); 
}

And this to query to display the data(variables called in HTML):

include('conn/conn.php');

$str = htmlentities($_GET["story_id"]);

$query = "SELECT * "
           . "FROM Stories "
           . "WHERE ID='$str'";

$result = mysqli_query($conn, $query) or die(mysqli_error($conn));

$queryread = "SELECT Users.FirstName, Stories.Age, Stories.Story, 
Stories.Image FROM `Stories` INNER JOIN `Users` ON Users.ID = Stories.User_ID";


$result1 = mysqli_query($conn, $queryread) or die(mysqli_error($conn));

if(mysqli_num_rows($result1) > 0 ){

    while($row = mysqli_fetch_assoc($result1)){

        $name = $row["FirstName"];
        $age = $row["Age"];
        $story = $row["Story"];
        $image = $row["Image"];

}
}
mysqli_close($conn); 
?>

EDIT

Had two queries when I only needed one.

$str = htmlentities($_GET["story_id"]);


$queryread = "SELECT Stories.ID, Users.FirstName, Stories.Age, Stories.Story, Stories.Image FROM `Stories` INNER JOIN `Users` ON Users.ID = Stories.User_ID 
WHERE Stories.ID='$str'";


$result1 = mysqli_query($conn, $queryread) or die(mysqli_error($conn));

if(mysqli_num_rows($result1) > 0 ){

    while($row = mysqli_fetch_assoc($result1)){

        $name = $row["FirstName"];
        $age = $row["Age"];
        $story = $row["Story"];
        $image = $row["Image"];

}
}
mysqli_close($conn); 
?>       

you are reading the data from (result1) not from (result).

Change this one while($row = mysqli_fetch_assoc($result1)){

to

while($row = mysqli_fetch_assoc($result)){

and then it will works fine with you .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM