首页 > 解决方案 > 如何在不同的页面上向用户显示他们提交的表单?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>

标签: phpmysqlsqlpdo

解决方案


Read up on entity-relationship data modeling. Seriously. Go and do it.

Your entities (tables) are, I believe,

  • account with one row per human user
  • critter with one row per creature to be adopted
  • request 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.


推荐阅读