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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…