drnrxv9383 2018-01-19 22:15
浏览 48
已采纳

会话数据以两页形式覆盖PHP [关闭]

To register a user, I am using a form that consists of two pages. the first form(addUser.php) sends the data over post to the second form(plz.php). In the second form I save the post values from the previous page in session variables, for example

     $_SESSION['phone1'] = $_POST['telnr'];

I have checked and the data is sent correctly to the second form. The second form has only one dropdown and after an option is selected, it is supposed to save all data in the database. The problem is that when I send the second form, all session variables are overwritten and I get a error message "email cannot be null". I am assuming the other variables are overwritten too. I have tried so far saving the session data in an array and then extracting it but no luck. The second form looks like this. And yes, sessions are started at the top of each file.

    $_SESSION['email1'] = $_POST['email'];
    $_SESSION['passwort1'] = $_POST['passwort'];
    $_SESSION['vname1'] = $_POST['vname'];
    $_SESSION['fname1'] = $_POST['fname'];
    $_SESSION['phone1'] = $_POST['telnr'];
    $_SESSION['firma1'] = $_POST['firmen_name'];
    $_SESSION['ad11'] = $_POST['adresse1'];
    $_SESSION['ad21'] = $_POST['adresse2'];
    $_SESSION['kr1'] = $_POST['kreis'];
    //$_SESSION["plz1"] = $_POST['plz'];
    if(isset($_POST['submit'])){


    // Passwort wird gehasht und in der Datenbank gespeichert, bcrypt 
    algorithm, cost = 10, salt wurde automatisch generiert und ist unique
    $hash = password_hash($_SESSION['passwort1'], PASSWORD_DEFAULT);

                $insert = "INSERT INTO users 
    (email,passwort,vname,fname,firmen_name,telnr, userlevel, active, 
    adresse1, adresse2, kreis, plz) VALUES 
    (:email,:hash,:vname,:fname,:firma,:phone,1,0, :adresse1, :adresse2, 
    :kreis, :plz)";
                $prepStmt = $pdo->prepare($insert);
                $prepStmt ->bindParam("email", $_SESSION['email1']);    
                $prepStmt ->bindParam("hash", $hash);
                $prepStmt ->bindParam("vname", $_SESSION['vname1']);
                $prepStmt ->bindParam("fname", $_SESSION['fname1']);
                $prepStmt ->bindParam("firma", $_SESSION['firma1']);
                $prepStmt ->bindParam("phone", $_SESSION['phone1']);
                $prepStmt ->bindParam("adresse1", $_SESSION['ad11']);
                $prepStmt ->bindParam("adresse2", $_SESSION['ad21']);
                $prepStmt ->bindParam("kreis", $_SESSION['kreis1']);
                $prepStmt ->bindParam("plz", $_POST["plz1"]);
                $prepStmt ->execute();
                header("Location: users.php");
                exit();
  }else{
  ?>
  <div class="container">
            <div class="col-md-offset-3 col-md-7">
            <form action="plz.php" method="post" class="text-center col-md-
  offset-0.5 ">
            <!-- more form -->
  • 写回答

2条回答 默认 最新

  • dongxun5349 2018-01-19 22:22
    关注

    Use third page to save plz.php contents (the list) and values in the session.

    It is clear that your session data is rewritten by null values when you submit the second form elements to the same page.

    So you may change the form action like the following:

    <form action="third.php" method="post" class="text-center col-md-
      offset-0.5 ">
    

    Or you can send the second form plz using the GET method and change its requesting value from $_POST to $_GET. But Keep in mind to check if the $_POST empty to not set session!

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

报告相同问题?

悬赏问题

  • ¥20 易康econgnition精度验证
  • ¥15 线程问题判断多次进入
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致