Infinity
Infinity

Reputation: 898

Redirect users via PHP depending on selected Id

I have selecting data from MySQL table and returned data looks like:

Name    Rank    Vessel Type    Preview
John    1       ab             View
Lisa    1       cd             View
Carl    2       bd             View

As you see above View should be link to user's profiles depending on their Id.

For now I'm using table as you see below and this is line for View link:

<?php echo "<td> <a href='preview.php?Id='" . $Id . ">View</a></td>"; ?>

I don't know why, but It redirecting users like: www.etc.com/preview.php?Id= without provided Id.

If I'm using something like:

<?php echo "<td> <a href='preview.php?Id='" . $Id . ">View " . $Id . "</a></td>"; ?>

It returning View Id for example View 1, that's mean $Id variable is fine. Have you ideas what's wrong?

Also I want to ask you If it is correct way for redirecting? Or there is any other / better way to do that?

This is PHP:

<?php
if(isset($UserID)) {    

    $users = $con->prepare("
    SELECT DISTINCT Id, 
                    CONCAT(FirstName, ' ', LastName) AS FullName, 
                    RankApplied, 
                    VesselsType 
    FROM tbl
"); 
    $users->execute();

    $users->bind_result($Id, $FName, $RankApplied, $VesselsType);


} else {
    echo "There is no User ID detected, try to refresh browser.";   
}
?>

<table>
    <tr>
        <th>Name</th>
        <th>Rank</th>       
        <th>Vessel Type</th>
        <th>Preview</th>
    </tr>
    <?php 
    while ($users->fetch()) {
    ?>
    <tr>
        <td><?php echo $FName; ?></td>
        <td><?php echo $RankApplied; ?></td>        
        <td><?php echo $VesselsType; ?></td>
        <?php echo "<td> <a href='preview.php?Id='" . $Id . ">View</a></td>"; ?> 
    </tr>
    <?php 
    } 
    ?>
</table>

Upvotes: 1

Views: 794

Answers (3)

Lal
Lal

Reputation: 14820

Replace

<?php echo "<td> <a href='preview.php?Id='" . $Id . ">View</a></td>"; ?> 

with

<?php echo "<td> <a href='preview.php?Id=" . $Id . ">View</a></td>"; ?> 

You are having a ' after ?Id= which was creating the problem..

Instead of concatenating the $ID you could directly use

<?php echo "<td> <a href='preview.php?Id=$Id'>View</a></td>"; ?> 

As in the docs

Double quote strings will display a host of escaped characters (including some regexes), and variables in the strings will be evaluated. An important point here is that you can use curly braces to isolate the name of the variable you want evaluated. For example let's say you have the variable $type and you what to echo "The $types are" That will look for the variable $types. To get around this use echo "The {$type}s are" You can put the left brace before or after the dollar sign. Take a look at string parsing to see how to use array variables and such

Please see this answer

Upvotes: 2

Omari Victor Omosa
Omari Victor Omosa

Reputation: 2879

Solving your problem:

<?php echo "<td> <a href='preview.php?Id=".$Id . "'>View</a></td>"; ?>

Alternatively you may define before using it:

<?php $link = "preview.php?Id=$Id"; ?>
<?php echo "<a href='$link'>View</a>"; ?>

Also you may try PHP in HTML

<a href='<?php echo "preview.php?Id=$Id"?>'>View</a>  

Upvotes: 0

Richard Porter
Richard Porter

Reputation: 76

You are closing your href quote before including the id, change the line to the following to get it to work:

<?php echo "<td> <a href='preview.php?Id=" . $Id . "'>View</a></td>"; ?>

Upvotes: 3

Related Questions