使用PHP表单将数据输入MySQL数据库

时间:2018-08-09 23:38:39

标签: php mysql pdo

我有一个表格,当用户进行选择并输入一些个人详细信息(例如姓名和电子邮件)时,该数据将被插入到MySQL数据库表中。然后,通过显示表中最近输入的记录作为确认,将输入的数据显示回给用户。

我遇到的问题是所显示的数据库表随后为空,并且当我检查数据库时,仅输入了空条目?

(表单位于两个单独的页面上,因此以后可以使下拉列表相互依赖,而无需JQuery等)

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>

2 个答案:

答案 0 :(得分:1)

发送form2.php时... 它被发送到booksphp.php(而不是action_form.php)

代码

$book =   $_POST['books'];

应替换为

$book =   $_SESSION['books'];

答案 1 :(得分:1)

您已经在action_form.php上初始化并填充了变量

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

所以就继续使用它们吧。

更改此代码

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

对此

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

为什么使事情变得不必要的复杂?

快乐编码!