我有一个表单,当用户做出选择并输入一些个人细节时,比如姓名和电子邮件,这些数据就应该插入mysql数据库表中。然后,通过显示表的最近输入的记录作为确认,将输入的数据显示回用户。
我遇到的问题是显示的数据库表是空的,当我检查数据库时只输入了空条目?
(表单在两个单独的页面上,这样我以后就可以使下拉列表相互依赖,而无需jquery等)
表单1.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>
表单2.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
}
?>
动作表单.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条答案
按热度按时间6rqinv9w1#
发送form2.php时。。。它被发送到booksphp.php(而不是action\u form.php)
代码
应更换为
mzaanser2#
您已经在action\u form.php上初始化并填充了变量
所以就用它们吧。
更改此代码
为了这个
为什么把事情弄得比必要的复杂?
快乐编码!!!