dongtong0796 2016-03-11 16:57
浏览 8
已采纳

根据选定的Id,通过PHP重定向用户

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>
  • 写回答

3条回答 默认 最新

  • doutao6380 2016-03-11 17:02
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事: