如何在同一页面的url中传递最后一个插入的id

时间:2016-03-04 10:28:06

标签: php html

<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
           }
           ?>

我试图通过网址传递最后一次插入内容ID的$ lastId,但是我没有得到它只是打印在网址中的ID (getResult.php?id=mysqli_insert_id($link)) 任何人都可以帮助我

3 个答案:

答案 0 :(得分:2)

您尚未关闭?>中的<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/> 

答案 1 :(得分:1)

    <?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/> 

答案 2 :(得分:0)

试试这个

 <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>