doukuang6795 2016-02-05 06:53
浏览 60
已采纳

PHP:无法从动态创建的URL中提取ID

A landing page on my website displays all the links relating to that section (recipes), using a for loop. This works fine. But I then need to extract the id generated in that for loop, to populate the page being generated.

I can't figure how to extract that id value (neither $_REQUEST['id'] nor $_REQUEST['id=?']; i suspect both are definitely wrong). Is it through manipulating the URL string or is there another way? Following is the related code:

<?php 
                $id = intval($_REQUEST['id']);

                if ($id) {// user gets here by clicking on link with id
                $query  = "SELECT * FROM recipes WHERE uniqno = $id";
                $result = $conn->query($query);
                $row = mysqli_fetch_all($result, MYSQLI_ASSOC);
                $data = $row // fetch data from database

                ?>
                <p><?php echo $data['ingredcontent']; ?></p>

                <?php }
                else {
                foreach ($reciperow as $recipe) { ?>

                <a href="?id=<?php echo $recipe['uniqno'];?>"><h2><?php echo $recipe['rectitle'];?></h2></a>

                <p class="subhead"><?php echo $recipe['recsummary']; ?></p>

<?php }} ?>

Would appreciate if anyone could advise what I should pass to $_REQUEST to ensure it pulls out the 'id' defined in the foreach loop. (note: I'm intentionally staying away from PHP routing since this is a very basic project).

  • 写回答

1条回答 默认 最新

  • douyaju4749 2016-02-05 07:55
    关注

    I tried to modify your code as below, please check if this is what you want

    <?php 
    $conn=new mysqli($hostname, $dbuser, $dbpassword, $dbname);//i assume you have this somewhere
    
                    if (isset($_REQUEST['id'])) {// user gets here by clicking on link with id
                    $query  = "SELECT * FROM recipes WHERE uniqno = ".$_REQUEST['id']; //not necessary to convert $_REQUEST to integer
                    $result = $conn->query($query);
                    $row = mysqli_fetch_all($result, MYSQLI_ASSOC);
                    $data = $row // fetch data from database
    
                    ?>
                    <p><?php echo $data[0]['ingredcontent']; //you fetch all... need to set index(row) ?></p>
    
                    <?php }
                    else {
                    //so i assume your $reciperow is fetching all data:
                    $query  = "SELECT * FROM recipes"; //not necessary to convert $_REQUEST to integer
                    $result = $conn->query($query);
                    $reciperow= mysqli_fetch_all($result, MYSQLI_ASSOC);    
                    
                    foreach ($reciperow as $recipe) { ?>
    
                    <a href="?id=<?php echo $recipe['uniqno'];?>"><h2><?php echo $recipe['rectitle'];?></h2></a>
    
                    <p class="subhead"><?php echo $recipe['recsummary']; ?></p>
    
    <?php }} ?>

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥100 H5网页如何调用微信扫一扫功能?
  • ¥15 讲解电路图,付费求解