如何在同一页面的 URL 中传递最后插入的 ID


how to pass the last inserted id in the url in same page

<h2><a style="margin-left:370px;" class="btnAction" href="<?php echo getResult.php?id=mysqli_insert_id($link)"><span>Click to view results </span></a><br/> <br/> 

<?php 
          if(!empty($_POST))
          { //here post values are inserted
            $userId     = $_SESSION['user_id']; 
            $setId      = $_SESSION['rand_set'];
            $testDate   = date( 'Y-m-d H:i:s' );
            $timeSpent  = (!empty($_POST['timeTaken']))?$_POST['timeTaken']:$_SESSION['time']; 
            $userTable  = "user_tests";
            $fields = array("user_id", "set_id", "test_taken", "time_spent", "score", "status");
            $values = array($userId, $setId, $testDate, $timeSpent, $rightAnswer, "1");
            $result = insert($userTable, $fields, $values); 
            $lastId = mysqli_insert_id($link);//want to pass this id in the url
           }
           ?>

我正在尝试通过 url 传递上次插入的 id $lastId,但我没有得到它只是打印在 url 中的 id,例如(getResult.php?id=mysqli_insert_id($link))谁能帮我

您没有在 <a 中关闭 ?> 标签。 并且没有将字符串与您的最后一个 id 连接起来。

所以改变

href="<?php echo getResult.php?id=mysqli_insert_id($link)"

href="<?php echo 'getResult.php?id='.mysqli_insert_id($link); ?>"

最终代码是

<h2><a style="margin-left:370px;" class="btnAction" href="<?php echo 'getResult.php?id='.mysqli_insert_id($link); ?>"><span>Click to view results </span></a><br/> <br/> 
    <?php 
              if(!empty($_POST))
              { //here post values are inserted
                $userId     = $_SESSION['user_id']; 
                $setId      = $_SESSION['rand_set'];
                $testDate   = date( 'Y-m-d H:i:s' );
                $timeSpent  = (!empty($_POST['timeTaken']))?$_POST['timeTaken']:$_SESSION['time']; 
                $userTable  = "user_tests";
                $fields = array("user_id", "set_id", "test_taken", "time_spent", "score", "status");
                $values = array($userId, $setId, $testDate, $timeSpent, $rightAnswer, "1");
                $result = insert($userTable, $fields, $values); 
                $lastId = mysqli_insert_id($link);//want to pass this id in the url
               }
               ?>

 <h2><a style="margin-left:370px;" class="btnAction" href="<?php echo getResult.php?id='.$lastId'"?><span>Click to view results </span></a><br/> <br/> 

像这样尝试

 <h2><a style="margin-left:370px;" class="btnAction" href="<?php echo 'getResult.php?id='.mysqli_insert_id($link); ?>"><span>Click to view results </span></a>