0

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.

iamatstackoverflow
  • 402
  • 2
  • 4
  • 13
  • Use sessions, store the values in session – Danyal Sandeelo Nov 14 '19 at 07:59
  • **Warning:** You are wide open to [SQL Injections](https://php.net/manual/en/security.database.sql-injection.php) and should use parameterized **prepared statements** instead of manually building your queries. They are provided by [PDO](https://php.net/manual/pdo.prepared-statements.php) or by [MySQLi](https://php.net/manual/mysqli.quickstart.prepared-statements.php). Never trust any kind of input! Even when your queries are executed only by trusted users, [you are still in risk of corrupting your data](http://bobby-tables.com/). [Escaping is not enough!](https://stackoverflow.com/q/5741187) – Dharman Nov 14 '19 at 09:11
  • [How to get the error message in MySQLi?](https://stackoverflow.com/a/22662582/1839439) – Dharman Nov 14 '19 at 09:11

1 Answers1

1

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 ....
?>
iamatstackoverflow
  • 402
  • 2
  • 4
  • 13