php - 如何在不同的页面上向用户显示他们提交的表单?PHP SQL PDO
问题描述
我正在尝试制作一个采用表单,用户可以在其中看到提交的表单。
在导航栏中有一个帐户页面。当您单击帐户页面时,我希望它列出用户提交的所有请求。
只想查看用户帐户信息,但我在请求表中获取每个人的信息。我认为这SELECT * FROM request
是需要改变的路线,但我也不知道是什么。我不知道 WHERE 之后是什么。
我希望已登录的用户能够看到列出的请求,而不是看到我目前得到的每个用户发出的所有请求。
SQL 数据库
---login details---
CREATE TABLE `account` (
`id` int(11) unsigned NOT NULL,
`email` varchar(100) NOT NULL,
`password` varchar(250) NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
---adoption Request---
CREATE TABLE `request` (
`id` int(11) unsigned NOT NULL,
`fullname` varchar(30) DEFAULT NULL,
`email` varchar(120) DEFAULT NULL,
`address` varchar(120) DEFAULT NULL,
`postcode` varchar(30) DEFAULT NULL,
`present` varchar(120) DEFAULT NULL,
`category` enum('Snake','Rabbit', 'Hamster', 'Dog', 'Kitten') NOT NULL DEFAULT 'Snake',
PRIMARY KEY (`id`),
userId INT NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
帐户列表代码
<div class="wrapper row3">
<main class="hoc container clear">
<!-- main body -->
<div class="sectiontitle">
<h6 class="heading">Account Information</h6>
<p>Adoption Request Form</p>
</div>
<?php
include("connectdb.php");
?>
<table cellspacing="0" cellpadding="10">
<tr><th>fullname</th> <th >email</th><th >address</th> <th >postcode</th><th >present</th><th >category</tr></tr>
<?php
try{
// Run a SQL query
$sqlstr = "SELECT * FROM request";
$rows=$db->query($sqlstr);
//loop through all the returned records and display them in a table
foreach ($rows as $row) {
echo "<tr><td >" . $row['fullname'] . "</td><td >" . $row['email'] . "</td><td >" . $row['address'];
echo "</td><td >" . $row['postcode'] . "</td><td >" . $row['present'] . "<tr><td >" . $row['category']."</td></tr>\n";
}
echo "</table> <br>";
} catch (PDOException $ex){
//this catches the exception when the query is thrown
echo "Sorry, a database error occurred when querying the vehicle records. Please try again.<br> ";
echo "Error details:". $ex->getMessage();
}
?>
</div>
解决方案
Read up on entity-relationship data modeling. Seriously. Go and do it.
Your entities (tables) are, I believe,
account
with one row per human usercritter
with one row per creature to be adoptedrequest
with one row per adoption request
Each of these tables contains attributes of the entity. account.email
and account.firstname
for example. critter.species
and critter.is_adopted_yet
for example.
Each table will have an autoincrementing primary key account_id
, critter_id
, request_id
.
request
will contain a critter_id
and an account_id
as well as the attributes describing the request. That is, each row of request
represents a relationship between an account
and a critter
.
Then, when looking for information about a single account's requests, you'll do something like this.
SELECT request.account_id, critter.*
FROM request
JOIN critter ON request.critter_id = critter.critter_id
WHERE request.account_id = ?
Using a WHERE
filter to pull out just the account you want.
推荐阅读
- javascript - 如何在 react.js 中显示不同日期的相同项目的值
- buildroot - 如何在 raspberrypi 上的 WAYLAND 中更改 HDMI 输出的方向(旋转)
- python - 在linux ubuntu中的selenium python中以正常模式(无隐身模式)打开chromedriver?
- sql - 将驱动器映射到您的 SQL 脚本 NET USE
- android - Kotlin 多平台配置问题
- javascript - JavaScript:关于传入 `addEventListener` 的回调的混淆
- c++ - 如何让 clangd 完成器与 Unrealengine-4 一起工作
- c++ - 在 wxEntry(argc, argv) 之前来自 wxWIdgets (3.1.4) 的 wxMessageBox() 类;警告使用缺少的 CPU 指令集
- c++ - 使用 const 实现 unordered_map 迭代器的问题
- python - 在 python3 中使用 re.sub() 从字符串中删除除给定字母后面的数字之外的所有数字