Astiop
Astiop

Reputation: 21

Inputting data into MySQL database using PHP form

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>

Upvotes: 0

Views: 168

Answers (2)

Kuya
Kuya

Reputation: 7310

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?

Upvotes: 1

user4347792
user4347792

Reputation:

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'];

Upvotes: 1

Related Questions