3

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?

1
  • miss, add you php scripts to your forms Commented Aug 3, 2016 at 4:09

3 Answers 3

4

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

Sign up to request clarification or add additional context in comments.

Comments

1

Add the php to you form action to call them when you submit the form

<form action="first.php" method="post">

<form action="second.php" method="post">

11 Comments

what do you mean by first.php and second.php ?? am I supposed to be directing it to my second form page or a separate page that has code to insert my info into my database??
first.php and second.php are your php scripts i named them first and second
thanks, but my inputted information still won't go into my database. on my second form page, when i press the submit button, it just refreshes the page and clears the form??
you added "second.php" to that page form action yes?
yes, i directed it to my php page that has code to put info in my database
|
0

Create some hidden fields in the second form and echo the values in the input boxes. So you will get all the values in second form on submit.

1 Comment

Welcome to Stack Overflow! This is really a comment, not an answer. With a bit more rep, you will be able to post comments.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.