Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
181 views
in Technique[技术] by (71.8m points)

mysql - How to keep php variables from previous page after form submit

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.

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can do this using 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) 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 ....
?>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...