表单提交后如何保留上一页的php变量 [英] How to keep php variables from previous page after form submit

查看:32
本文介绍了表单提交后如何保留上一页的php变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个 php 网站,人们可以在其中获得任务帮助.第一步,用户注册自己,或者如果他们已经有一个帐户,他们将登录并被重定向到另一个页面,在那里他们可以创建关于分配的订单.数据库中有两个表.一种是userinfo,其中保存注册信息用户ID(自动递增)、姓名、电子邮件、密码等.

I am creating a php website where people can get help for their assignments. In first step, the users register themselves or if they have an account already, they will login and will be redirected to another page where they can create an order about assignment. There are two table in database. One is userinfo where registration information is saved UserID(auto incremented), Name, Email, Password etc.

另一个表格是订单信息,其中包括有关分配的详细信息.我已将这些表与 userid 链接为订单信息表中的外键.我通过 post 将登录信息带到下一页,用户输入订单信息,然后我想提交订单信息表中的数据以及外键用户 ID.

The other table is of Order info which includes the details about assignment. I have linked these tables with userid as a foreign key in order info table. I take login info to the next page through post, where user enters order info and then I want to submit the data in order info table along with the foreign key userid.

<?php
$conn = new mysqli('localhost', 'root', '', 'db' ); 
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
$email=$_POST['email'] ; //from previous login page

$getid="SELECT UserID,Name from userinfo WHERE email = '$email' ";
$result = $conn->query($getid);

if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        $id= $row["UserID"];
    }
}

if(isset($_POST['submit'])){ 
    $id= $_SESSION['id'];
    $type= $_POST['type'];
    $level= $_POST['level'];
    $subject= $_POST['subject'];
    $topic= $_POST['topic'];
    $page= $_POST['page'];
    $detail= $_POST['detail'];
    $day= $_POST['day'];
    $refer= $_POST['refer'];
    $referstyle= $_POST['referstyle'];
    $sql="INSERT INTO OrderInfo (UserID, PaperType, AcademicLevel, Subject, Topic, Detail, Pages, Referrence, ReferrenceStyle, Urgency) 
    VALUES ('$id','$type', '$level', '$subject','$topic','$detail', '$page','$refer', '$referstyle',  '$day' )";

    if ($conn->query($sql) === TRUE ) {
        echo   "";
    } 
    else {
        echo " <br>Error: "  . "<br>" . $conn->error;
    }  
}
?>

但问题是当我点击提交时,以前的值被删除并显示未定义的索引电子邮件"错误,并且数据未插入到 orderinfo 表中.表单提交后如何保留 $email 和 $id 变量?我曾尝试使用 Sessions,但它也不起作用.

But the problem is when I click on submit, the previous values get removed and undefined index "email" wrror is shown and the data is not inserted in the orderinfo table. How can i keep $email and $id variables after form submit? I have tried using Sessions but it didn't work too.

推荐答案

您可以使用 session

1) 在第一页中,提交第一个表单后,将 email 值存储在 session 中,如下所示

1) In first page, after submitting first form store the email value in session like below

<?php session_start();
$_SESSION['email'] = $_POST['email'];
?>

2) 在第二页中,您必须像这样启动会话并访问会话变量

2) In second page, you have to start the session and access the session variable like this

<?php session_start();

$email = $_SESSION['email']; //from previous login page
// And remaining code here ....
?>

这篇关于表单提交后如何保留上一页的php变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆