How to prevent $_SESSION from passing variable to another form even if the value doesn't exist in mysql?
<p>I have two forms. The first form has two inputs: date and id. The id should be in the database otherwise, it will not save. What the $_SESSION does is that it passes the value from ID when I submit the form to the second form which is in the same page too. <strong>How can I prevent this from passing the value if the user has put a value that is not in the database?</strong> My php code issues a modal which says that the ID the the user has put is invalid however, it still passes to the other form...</p>

<p>Here is my php code:</p>

<pre><code>&lt;?php
    include 'config.php';

    if (isset($_POST['documentRequest1']))
    {

        $chkbox = $_POST['docs'];
        $id = $_POST['a'];

        $totalPrice = 0;
        $total_cedula = 0;
        $isPartOfTotal = 0;


         foreach($chkbox as $chk1)  
           { 
                    if($chk1=='Certificate of Residency') 
                    {
                      $chek_val=$_POST['d1'];
                      $result = mysqli_query($conn, "SELECT price FROM document WHERE typeOfDoc = '$chk1';");
                      $row = mysqli_fetch_assoc($result);                               
                      $isPartOfTotal = $chek_val * $row["price"];
                      $sql = mysqli_query($conn, "INSERT into requestitem (DocumentRequest_idDocumentRequest, Document_idDocument, quantity, isPartOfTotal, price, paymentStatus) VALUES ((SELECT idDocumentRequest FROM documentrequest WHERE Person_idPerson = '$id'), (SELECT idDocument FROM document WHERE typeOfDoc = '$chk1'), '$chek_val', '$isPartOfTotal',' $total_cedula', 'Unpaid');");
                    }
                    else if($chk1=='Barangay Clearance') 
                    {
                      $chek_val=$_POST['d2'];
                      $result = mysqli_query($conn, "SELECT price FROM document WHERE typeOfDoc = '$chk1';");
                      $row = mysqli_fetch_assoc($result);                               
                      $isPartOfTotal = $chek_val * $row["price"];
                      $sql = mysqli_query($conn, "INSERT into requestitem (DocumentRequest_idDocumentRequest, Document_idDocument, quantity, isPartOfTotal, price, paymentStatus) VALUES ((SELECT idDocumentRequest FROM documentrequest WHERE Person_idPerson = '$id'), (SELECT idDocument FROM document WHERE typeOfDoc = '$chk1'), '$chek_val', '$isPartOfTotal',' $total_cedula', 'Unpaid');");
                    } 
                    else if ($chk1=='Cedula')
                    {
                      $chek_val=$_POST['d3'];
                      //$income=$_POST['income'];
                      $result = mysqli_query($conn, "SELECT income FROM person WHERE idPerson = '$id';");
                      $row = mysqli_fetch_assoc($result);
                      $income = $row['income'];
                      $total_cedula = ($income * 12 * .001) + 5;

                      $isPartOfTotal = $chek_val * $total_cedula;
                      $sql = mysqli_query($conn, "INSERT into requestitem (DocumentRequest_idDocumentRequest, Document_idDocument, quantity, isPartOfTotal, price, paymentStatus) VALUES ((SELECT idDocumentRequest FROM documentrequest WHERE Person_idPerson = '$id'), (SELECT idDocument FROM document WHERE typeOfDoc = '$chk1'), '$chek_val', '$isPartOfTotal',' $total_cedula', 'Unpaid');");
                    }

                    // partial total
                    $result = mysqli_query($conn, "SELECT price FROM document WHERE typeOfDoc = '$chk1';");
                    $row = mysqli_fetch_assoc($result);                             
                    $isPartOfTotal = $chek_val * $row["price"];

           } 

        ?&gt;
        &lt;!-- Modal --&gt;
    &lt;div class="modal fade" id="step2" tabindex="-1" role="dialog" aria-labelledby="memberModalLabel" aria-hidden="true"&gt;
        &lt;div class="modal-dialog"&gt;
            &lt;div class="modal-content"&gt;
                &lt;div class="modal-header" style = "background-color: #5bc0de; color: white;"&gt;
                    &lt;button type="button" class="close" data-dismiss="modal" aria-label="Close"&gt;&lt;span aria-hidden="true"&gt;&amp;times;&lt;/span&gt;
                    &lt;/button&gt;
                     &lt;h4 class="modal-title" id="memberModalLabel"&gt;Request added.&lt;/h4&gt;
                &lt;/div&gt;
                &lt;div class="modal-body"&gt;
                    &lt;h5&gt;You have successfully added a new request. However, it is still tagged as unpaid.&lt;br&gt; This will redirect you to the invoice page.&lt;/h5&gt;
                &lt;/div&gt;
                &lt;div class="modal-footer"&gt;
                    &lt;button type="button" class="btn btn-primary" data-dismiss="modal"&gt;Close&lt;/button&gt;
                &lt;/div&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    &lt;/div&gt;
        &lt;?php

        //header("Refresh: 5;url=all_docRequest.php");
        mysqli_close($conn);    
    }                               
?&gt;
</code></pre>

<p>First form html code:</p>

<pre><code>&lt;form class="form-horizontal form-label-left" name = "documentRequest" enctype="multipart/form-data" role="form" method="post" novalidate&gt;

    &lt;div class = "first"&gt;

    &lt;div class="item form-group"&gt;
        &lt;label class="control-label col-md-3 col-sm-3 col-xs-12"&gt;Date of Request &lt;span class="required"&gt;*&lt;/span&gt;&lt;/label&gt;
        &lt;div class="col-md-6 col-sm-6 col-xs-12"&gt;
            &lt;input id="reqDate" class="form-control col-md-7 col-xs-12" name="reqDate" required="required" type="date" &gt;
        &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class="item form-group"&gt;
        &lt;label class="control-label col-md-3 col-sm-3 col-xs-12"&gt;Resident's Name &lt;span class="required"&gt;*&lt;/span&gt;&lt;/label&gt;
    &lt;div class="col-md-6 col-sm-6 col-xs-12"&gt;
        &lt;input name="person_id" id="search-box" class="form-control col-md-7 col-xs-12" placeholder ="Type in ID number..." required="required" type="text"&gt;
    &lt;/div&gt;
    &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class="ln_solid"&gt;&lt;/div&gt;
    &lt;div class="form-group"&gt;
    &lt;div class="col-md-6 col-md-offset-3"&gt;
    &lt;button id="requestDiv" type="submit" class="btn btn-success" name="documentRequest" &gt;Proceed&lt;/button&gt;
    &lt;button type="submit" class="btn btn-primary"&gt;Cancel&lt;/button&gt;
    &lt;/div&gt;
    &lt;/div&gt;
    &lt;/form&gt;
</code></pre>

<p>Second form:</p>

<pre><code>&lt;form class="form-horizontal form-label-left" name = "documentRequest1" enctype="multipart/form-data" role="form" method="post" novalidate&gt;
    &lt;div class = "second"&gt;
    &lt;div class="item form-group"&gt;
        &lt;label class="control-label col-md-3 col-sm-3 col-xs-12"&gt;ID &lt;span class="required"&gt;*&lt;/span&gt;&lt;/label&gt;
        &lt;div class="col-md-6 col-sm-6 col-xs-12"&gt;
            &lt;input name="a" id="search" class="form-control col-md-7 col-xs-12" value = "&lt;?php echo $_SESSION['person_id']; ?&gt;" required="required" type="text" readonly="readonly"&gt;
        &lt;/div&gt;
    &lt;/div&gt;

    &lt;div class="item form-group"&gt;
        &lt;label class="control-label col-md-3 col-sm-3 col-xs-12"&gt; Document Request(s)  &lt;span class="required"&gt;*&lt;/span&gt;&lt;/label&gt;
        &lt;div class="col-md-6 col-sm-6 col-xs-12"&gt;

        &lt;input type="checkbox" name="docs[]" id="doc1" value="Certificate of Residency"/&gt; Certificate of Residency
        &lt;div class = "clearfix"&gt;&lt;/div&gt;
        &lt;div class = "col-xs-3"&gt;
            &lt;input name="d1" class="form-control col-md-7 col-xs-12" required="required" type="number"&gt;
        &lt;/div&gt;
        &lt;div class = "clearfix"&gt;&lt;/div&gt;

        &lt;input type="checkbox" name="docs[]" id="doc2" value="Barangay Clearance"/&gt; Barangay Clearance                                                  
        &lt;div class = "clearfix"&gt;&lt;/div&gt;
        &lt;div class = "col-xs-3"&gt;
            &lt;input name="d2" class="form-control col-md-7 col-xs-12" required="required" type="number"/&gt;
        &lt;/div&gt;

        &lt;div class = "clearfix"&gt;&lt;/div&gt;
        &lt;input type="checkbox" name="docs[]" id="doc3" value="Cedula"/&gt; Cedula                                                  
        &lt;div class = "clearfix"&gt;&lt;/div&gt;
        &lt;div class = "col-xs-3"&gt;
            &lt;input name="d3" class="form-control col-md-7 col-xs-12" required="required" value = "1" type="number" readonly = "readonly"/&gt;
        &lt;/div&gt;
        &lt;/div&gt;
        &lt;/div&gt;
        &lt;/div&gt;
        &lt;div class="ln_solid"&gt;&lt;/div&gt;
        &lt;div class="form-group"&gt;
            &lt;div class="col-md-6 col-md-offset-3"&gt;
                &lt;button id="requestDiv1" type="submit" class="btn btn-success" name="documentRequest1" &gt;Proceed&lt;/button&gt;
                &lt;button type="submit" class="btn btn-primary"&gt;Cancel&lt;/button&gt;
    &lt;/div&gt;
    &lt;/div&gt;
    &lt;/form&gt;
</code></pre>

<p>This is the $_SESSION code:</p>

<pre><code>&lt;?php
session_start();
$_SESSION['person_id']=isset($_POST['person_id']) ? $_POST['person_id'] :'';
?&gt;
</code></pre>

<p>I really need your help on this one... Your help will be much appreciated. Thank you.</p>