php - Getting data based on which row chosen using session

882
651

Answer

Solution:

You can send record id as request parameter in your edit/show/delete. Make the changes in your code as Follow:

<?php
while ($rows = mysqli_fetch_array($result)){
$_SESSION["shorturl"]=$rows['shorturl];
?>
<tr>
<td><?php echo $rows['shorturl']; ?></td>
<td><?php echo $rows['phonenumber']?></td>
<td><button onclick="location.href = 'edit.php?id=' . echo $rows['id'] . ';">Edit</button><button onclick="location.href = 'show.php?id=' . echo $rows['id'] . ';">Show</button><button onclick="location.href = 'delete.php?id=' . echo $rows['id'] . ';">Delete</button></td>
</tr>
<?php }?>

While fetching data in particular file, just check ifid is set on that file. IfID is set, then do your operations on recordID which we have sent in request parameter.

Note: Session will not work for your question as it holds data till session ends.

629

Answer

Solution:

You are overwriting the session-variable every time you run through the loop here:

$_SESSION["shorturl"]=$rows['shorturl];

Maybe u can try to use a GET-Param instead

<button onclick="location.href = 'edit.php?shorturl=<?php echo urlencode($rows['shorturl']); ?>';">Edit</button>

Then retrive the shorturl on the edit.php site like this:

$shorturl = $_GET['shorturl'];

People are also looking for solutions to the problem: php - $_GET/$_POST array elements ordinal

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.