Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Submit form into MySQL table over 2 pages

I'm having trouble with the following code. I need my form to insert information into my MySQL table across 2 pages. I've done some research and with help, I've come up with the following.

First page PHP

<?php
 ob_start();
 session_start();
 require_once 'dbconnect.php';

 if( !isset($_SESSION['client']) ) {
  header("Location: homepage_login.php");
  exit;
 }
 // select loggedin users detail
 $res=mysql_query("SELECT * FROM clients WHERE client_id=".$_SESSION['client']);
 $userRow=mysql_fetch_array($res);


 if( isset($_POST['btn-nxt-page']) ) { 

  $client_student_id = trim($_POST['client_student_id']);
  $ss_name = trim($_POST['ss_name']);

  $client_student_id = strip_tags($client_student_id);
  $ss_name = strip_tags($ss_name);


 if ($_SELF) {
  $query = "";
  $res = mysql_query($query);

  if ($res) {
   $errTyp = "success";
  } else {
   $errTyp = "danger";
  } 
 }

     function redirect($where){      
       header("Location: $where");
    }
    if ($_REQUEST['ss_name'] == 'John') {
    header("Location: http://localhost/homepage_loggedin_book_john.php");
} else if ($_REQUEST['ss_name'] == 'Smith') {
    header("Location: http://localhost/homepage_loggedin_book_smith.php");
} else if ($_REQUEST['ss_name'] == 'Greg') {
    header("Location: http://localhost/homepage_loggedin_book_greg.php");
 } else if ($_REQUEST['ss_name'] == 'Jess') {
    header("Location: http://localhost/homepage_loggedin_book_jess.php");
 }}
?>

First page HTML

<form action"" method="post">

      <div class="form-group">
             <div class="input-group">
             <input type="text" name="client_student_id" class="form-control" placeholder="Enter your Student ID" required />
                </div>
            </div>
            <br>
            <br>

       <label for="ss_name" id="menu">Select a teacher</label>
            <select name="ss_name" id="#menu">
               <option value="John">John</option>
               <option value="Smith">Smith</option>
               <option value="Greg">Greg</option>
               <option value="Jess">Jess</option>
            </select>
            <br>
            <br>

        <div class="form-group">
             <button type="submit" class="btn btn-block btn-primary" name="btn-nxt-page">Next Page</button>
            </div>

         </form>

This all works fine, but the information the user puts in (student ID and the teacher) doesn't go into my database.

This is my second page PHP:

<?php
 ob_start();
 session_start();
 require_once 'dbconnect.php';

 if( !isset($_SESSION['client']) ) {
  header("Location: homepage_login.php");
  exit;
 }
 // select loggedin users detail
 $res=mysql_query("SELECT * FROM clients WHERE client_id=".$_SESSION['client']);
 $userRow=mysql_fetch_array($res);

$_SESSION['client_student_id'] = $_POST['client_student_id'];
$_SESSION['ss_name'] = $_POST['ss_name'];

if( isset($_POST['btn-book']) ) { 

 if ($_SELF) {
  $query = "";
  $res = mysql_query($query);

  if ($res) {
   $errTyp = "success";
  } else {
   $errTyp = "danger";
  } 
 }
}

?>
<?php
$ss_name = $_SESSION['ss_name'];
$client_student_id  = $_SESSION['client_student_id'];
$reason      = $_SESSION['reason'];
$slot_date = $_SESSION['slot_date'] ;
$slot_line    =$_SESSION['slot_line'];
?>
<?php
    $query="INSERT INTO appointments (,
    ss_name,
    client_student_id,
    reason,
    slot_date,
    slot_line,
     )
    VALUES('NULL',
    '".mysql_real_escape_string($ss_name)."',
    '".mysql_real_escape_string($client_student_id)."',
    '".mysql_real_escape_string($reason)."',
    '".mysql_real_escape_string($slot_date)."',
    '".mysql_real_escape_string($slot_line)."','
    )";
    echo $query;
    mysql_query($query) or die ('Error updating database');
    ?>

And my second page HTML:

<form action"" method="post">
    <p>Date: <input type="text" id="datepicker" name="slot_date"></p>

       <br>
       <br>
       Select a line:
       <ol id="selectable" name="slot_line">
  <li class="ui-widget-content">Line 1</li>
  <li class="ui-widget-content">Line 2</li>
  <li class="ui-widget-content">Line 3</li>
  <li class="ui-widget-content">Line 4</li>
  <li class="ui-widget-content">Line 5</li>
  <li class="ui-widget-content">Line 6</li>
  <li class="ui-widget-content">Line 7</li>
</ol>
<br>
<br>

<p>Reason for appointment: <input type="text" name="reason"></p>

        <div class="form-group">
             <button type="submit" class="btn btn-block btn-primary" name="btn-book">Book</button>
                </div>
</form>

I've been advised to put the second page PHP code to put the information into a separate page however I can't connect the pages.

So, my question is:

How do I get the information to insert into my database table (called appointments) across my two form pages?

like image 266
Isabella Avatar asked Apr 09 '26 23:04

Isabella


1 Answers

Please follow Following steps

  1. Save All your First page values in session when your submitting first page don't fire insert query here

  2. Fill all second page form values ,on second page submit event fire sql insert query and insert all first(session values) and second page values in this query

like image 195
rohitr Avatar answered Apr 11 '26 13:04

rohitr



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!