0

I have a form which when the user makes a selection and inputs some personal details, such as name and email, this data is then supposed to insert itself into a MySQL database table. The inputted data is then displayed back to the user by displaying the most recently entered record of the table as a confirmation.

The problem I am having is that the database table displayed is then empty, and when I check the database only null entries have been input?

(form is on the two separate pages so I can make the dropdown lists dependent on each other later without JQuery etc)

form1.php

    <html>
       <head>
          <title>Library System</title>
       </head>
    <body>
       <h1> <center> Library Book Reservation System </center></h1>
       <h2> <small><center>Use the form below to search for available books </center></small> </h2>
       <?php
       ?>
    <form class="submission-form"action="form2.php" method="post">
       <center>
          Select Book:
          <select name="books" id="books">
             <option value="catcher in the rye.">catcher in the rye.</option>
             <option value="to kill a mocking bird">to kill a mocking bird</option>
             <option value="1984">1984</option>
             <option value="Ulysses ">Ulysses</option>
             <option value="East of Eden">East of Eden</option>
             <option value="Animal Farm">Animal Farm </option>
             <option value="Of mice and men">Of mice and me.</option>
          </select>
          <p></p>
          <input type="submit" value="Next" id="sendBtn">
          <input type="reset" value="Reset" id="resetBtn">
          <input type="button" value="Help" id="helpBtn">
       </center>
    </form>
    </body>
    </html>

form2.php

<?php session_start();?>
<?php if($_POST) {
   $_SESSION['books'] = $_POST['books'];
   echo 'the book selected is : '.$_SESSION['books'].'';
?>
<html>
   <head>
      <title>Library System</title>
   </head>
<body>
   <h1> <center> Library Book Reservation System </center></h1>
   <h2> <small><center>Use the form below to search for available books </center></small> </h2>
<form class="submission-form"action="booksphp.php" method="post">
   <center>
      Select Researcher:
      <select name="owner">
         <option value="Roger">Roger</option>
         <option value="Catherine">Catherine</option>
         <option value="Deborah">Deborah</option>
         <option value="Frank">Frank</option>
         <option value="Paul">Paul</option>
      </select>
      <p></p>
      Full name:<br>
      <input type="text" name="fullname" size="45"><br>
      Email:<br>
      <input type="email" name="email" size="45"><br><br>
      <input type="submit" value="Submit" id="sendBtn">
      <input type="reset" value="Reset" id="resetBtn">
      <input type="button" value="Help" id="helpBtn">
   </center>
</form>
<?php 
} 
?>

action_form.php

<?php session_start(); ?>

 <!DOCTYPE html>
  <html>
    <head>
    <title> PHP + MySQL</title>
  </head>
  <body>
  <h1> <center> Library Book Reservation System </center></h1>
   <?php

    $name =  $_POST['fullname'];
    $email =  $_POST['email'];
    $book =   $_POST['books'];
    $owner = $_POST['owner'];

    if (!empty($name)){
    if (!empty($email)){

    }
    else{
      echo "Email field cannot be left blank";
      die();
    }

    }
    else{
      echo "Name field cannot be left blank";
      die();
    }



  $db_hostname = "mysql";
    $db_database = "****";
    $db_username = "******";
    $db_password = "**********";
    $db_charset  = "utf8mb4";

    $dsn = "mysql:host=$db_hostname;dbname=$db_database;charset=$db_charset";
    $opt = array(
       PDO::ATTR_ERRMODE         =>PDO::ERRMODE_EXCEPTION,
       PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
       PDO::ATTR_EMULATE_PREPARES => false
    );

    try{
        $pdo = new PDO($dsn,$db_username,$db_password,$opt);

        $stmt = $pdo->prepare(
                      "INSERT INTO  Borrowtest 
 (Book_Description,Owner,Fullname,Email) VALUES(?,?,?,?)");
         $success = $stmt->execute(





  array($_REQUEST['Book_Description'],
   $_REQUEST['Owner'],$_REQUEST['Fullname'],$_REQUEST['Email']));



       echo '<table width="70%" border="1" cellpadding="5" 
        cellspacing="5">
             <tr>
                <th>ID</th>
                <th>Book Title</th>
                <th>Owner</th>
                <th>Fullname</th>
                <th>Email</th>
                </tr>';

        echo "<h2><center>Data in table</center></h2>\n";
        $stmt = $pdo->query("SELECT MAX(Id) FROM Borrowtest");
        echo "Rows retrieved: ".$stmt->rowcount()."<br><br>\n";
        while ($row = $stmt->fetch()) {
            echo '<tr>
                  <td>'.$row["Id"].'</td>
                  <td>'.$row["Book_Description"].'</td>
                  <td>'.$row["Owner"].'</td>
                  <td>'.$row["Fullname"].'</td>
                  <td>'.$row["Email"].'</td>
                  </tr>';
        }

     // Prints the users input
     //  must change to print most recent table entry
       echo "Name: $name <br /> Email: $email <br /> Book: $book <br /> 
    Owner: $owner <br />";

    $pdo = NULL;
    } catch (PDOException $e) {
        exit("PDO Error: ".$e->getMessage()."<br>");
    }
     ?>
    </body>
    </html>
8
  • Where is booksphp.php? Where's the form posting to action_form.php? Commented Aug 9, 2018 at 23:45
  • 1
    @Phil we did exactly the same edit 1 second apart haha. Commented Aug 9, 2018 at 23:45
  • I guess the issue is $_REQUEST['Book_Description'], $_REQUEST['Owner'],$_REQUEST['Fullname'],$_REQUEST['Email']. Why are you using values from $_REQUEST with capitalised keys? You're already checking the values in $_POST with lowercase keys so I assume those are what you actually want to insert in your DB. Commented Aug 9, 2018 at 23:49
  • In any case, when developing, always set your PHP environment's error reporting to E_ALL and display errors for quick feedback, ie in your php.ini file, set error_reporting = E_ALL and display_errors = On. You would then see a warning for any undefined array indexes. Voting to close as a typo Commented Aug 9, 2018 at 23:51
  • Also, next time you want to ask a question, please see How to create a Minimal, Complete, and Verifiable example with focus on the minimal Commented Aug 9, 2018 at 23:52

2 Answers 2

1

when the form2.php is sent... it is sent to booksphp.php (instead of action_form.php)

the code

$book =   $_POST['books'];

should be replaced to

$book =   $_SESSION['books'];
Sign up to request clarification or add additional context in comments.

Comments

1

You have already initialized and populated your variables here on action_form.php

$name =  $_POST['fullname'];
$email =  $_POST['email'];
$book =   $_POST['books'];
$owner = $_POST['owner'];

so just go ahead and use them.

change this code

array($_REQUEST['Book_Description'],
$_REQUEST['Owner'],$_REQUEST['Fullname'],$_REQUEST['Email']));

to this

array($book, $owner, $name, $email));

Why make things more complicated than necessary?

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.