dongshie8450 2015-07-06 08:44
浏览 50

后退按钮背后的逻辑

I am workng on a project in which I have to put back button functionality. Unfortunatly I can't seem to work it out. I have created a back button and it works. But if the user wants to edit the previous form and then submit data again, so it edits the query. I have tried it in my project but it adds another record in the database and doesn't update the previous one. I know I am not using any update query here. Now I am stuck how would it works I can't think of a good logic. I have made a simple form for example. It is written below.

<?php
    $con = mysqli_connect('localhost','root','','test1');
    if (isset($_POST['sub'])) {
        $first_name = $_POST['fname'];
        $second_name = $_POST['lname'];
        $sql = "INSERT INTO `tbl`(`first_name`, `last_name`) VALUES
               ('$first_name', '$second_name')";
        $run = mysqli_query($con, $sql);
     }
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Untitled Document</title>
    </head>
    <body>
        <form method="POST" action="nextpage.php">
            First_name: <input type="text" name="fname">
            Second_name: <input type="text" name="lname">
            <input type="submit" name="sub" value="submit">
        </form>
    </body>
</html>

nextpage.php

<?php
    $con = mysqli_connect('localhost','root','','test1');
?>
<html>
    <body>
        <h1>Next Page</h1>
        <p>The query is submitted press ok to forward and back if you want to go back</p>
        <button>OK</button>
        <button onclick="history.go(-1);">Back</button>
    </body>
</html>

Thanks for your expert advice in advance.

  • 写回答

3条回答 默认 最新

  • douyulv6921 2015-07-06 08:56
    关注

    If I understand correctly.. ..you have a lot of work to do to be able to to 'UPDATE' a record that you have just created.

    Dont bother trying to go 'back' to the original form, go forwards to a new 'edit' form, passing the ID from the record that you have just created.

    Use the ID to SELECT that record from the DB and pre-populate the 'edit' form. Add a hidden ID field to the form. Then on submission of the 'edit' form, UPDATE the existing record, using the ID.

    评论

报告相同问题?

悬赏问题

  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?