dougua3705 2013-04-02 20:34
浏览 71
已采纳

PHP表单提交后重定向到上传的页面?

In this program, a user is uploading a photo into MYSQL. Photoid is auto-incremented. Once the user submits the form, the code below takes action. I want the user to be redirected to the page the photo was just uploaded to. How can I redirect him to photopage.php?photoid=???.

 $addaphoto_query = "INSERT INTO `photosite`.`photos` (id, photoid, title, description) VALUES ('$id', '', '$title', '$description')";

    if ($run_addaphoto = mysqli_query($connect, $addaphoto_query){
         header('Location: photopage.php?photoid=???');
    }
  • 写回答

2条回答 默认 最新

  • dswm97353 2013-04-02 20:37
    关注

    You'll want to use the PHP function mysqli_insert_id(). This gets you the Photo ID.

    if ($run_addaphoto = mysqli_query($connect, $addaphoto_query){
        header('Location: photopage.php?photoid=' . mysqli_insert_id());
    }
    

    In case you are wondering, mysqli_insert_id() is safe no matter how much traffic you get. More reading on this SO question.

    From the MySQL manual

    For LAST_INSERT_ID(), the most recently generated ID is maintained in the server on a per-connection basis. It is not changed by another client. It is not even changed if you update another AUTO_INCREMENT column with a nonmagic value (that is, a value that is not NULL and not 0). Using LAST_INSERT_ID() and AUTO_INCREMENT columns simultaneously from multiple clients is perfectly valid. Each client will receive the last inserted ID for the last statement that client executed.

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

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号