简体   繁体   中英

Value not getting updated while printing

Ok, so I have two users in the database with user id 1 and 2 respectively. I'n trying to fetch their user id, and then printing it at another page. But, everytime it shows the user ID as 1.

Here's the code : - The first code is where I'm printing

<?php
include('dbconnector.php');
$id=(isset($_REQUEST['user_id']));
echo $id;
?>

The second code is where I'm fetching these details :-

<?php
include('dbconnector.php');
//fetch the user information
$query = "select * from users";
$result=mysql_query($query, $db) or die (mysql_error($db));
echo "<table>";
echo "<tr>";
echo "<th> User ID</th>";
echo "<th> User Name</th>";
echo "</tr>";
while($row=mysql_fetch_assoc($result))
{
    echo "<tr>";
    echo "<td>" . $row['user_id'] . "</td>";
    echo '<td><a href="admin_view_users.php?user_id=' . $row['user_id'] . '">' . $row['user_name'] . '</td>';
    echo "</tr>";
}
echo "</table>";
?>

Any idea as to why this isn't working ? Thanks.

In this line:

$id=(isset($_REQUEST['user_id']));

You are not assigning the value of $_REQUEST['user_id'] to $id , but you are assigning the result of isset($_REQUEST['user_id']) , which obviously evaluates to TRUE and gets cast to 1 when you print it out.

So instead you could do:

$id = $_REQUEST['user_id'];

If you want to check if $_REQUEST['user_id'] exists before assigning, then I would suggest you restructure your whole program, as you don't want dependent code to execute without a necessary input value, so a simple ternary won't do you much good.

You didn't close the <a></a> tag. So your whole table is one big link of the first entry.

echo '<td><a href="admin_view_users.php?user_id=' . $row['user_id'] . '">' . $row['user_name'] . '</a></td>';

An addition to the RainFromHeaven's answer. isset($_REQUEST['user_id']) gives the idea of whether user_id was specified. And that said you need actually:

$id = isset($_REQUEST['user_id']) ? $_REQUEST['user_id'] : "";

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