美好的一天,
我在这方面还很陌生,任何建议将不胜感激。

我创建了一个体育队的球员数据库。现在,我想使用显示的表格为将与团队一起选择的球员创建视图以显示球队表。

我找不到找到解决办法。我想添加一个提交按钮或类似的按钮,以便将所有选中的玩家填充到视图中,然后显示以进行打印。
任何建议将不胜感激。

显示的表:

<thead>
    <tr>
        <th>Name</th>
        <th>Surname</th>
        <th>School</th>
        <th>Age</th>
        <th>Age Group</th>
        <th>Edit</th>
        <th>Delete</th>
        <th>Add to Team</th>

        <form action="" method="post">
    </tr>
</thead>
<!-- Table body -->
<tbody>
    <?php foreach($result as $index => $row) : ?>
    <tr>
        <td align="left"><?php echo $row['Name']; ?></td>
        <td align="left"><?php echo $row['Surname']; ?></td>
        <td align="left"><?php echo $row['School']; ?></td>
        <td align="left"><?php echo $row['Age']; ?></td>
        <td align="left"><?php echo $row['age_group']; ?></td>
        <td><a href="edit_player.php" title="Edit" onclick="return confirm('Confirm Player Delete ?')"></span> Edit</td>
        <td><a href="" title="Delete" onclick="return confirm('Confirm Player Delete ?')"></span> Delete</td>
        <td><input type="checkbox" name="add_to_team"/>Add to Team </span></td>
    </tr>
</tbody>


玩家的表格结构:

+---------------------------------------+
|   `Name` text NOT NULL,               |
|   `Surname` text NOT NULL,            |
|   `Id_num` varchar(13) NOT NULL,      |
|   `School` text NOT NULL,             |
|   `Age` varchar(2) NOT NULL,          |
|   `Address` varchar(200) NOT NULL,    |
|   `Address2` varchar(20) NOT NULL,    |
|   `City` varchar(20) NOT NULL,        |
|   `Postal_code` varchar(4) NOT NULL,  |
|   `player_pic` longblob NOT NULL,     |
|   `birth_cert` longblob NOT NULL,     |
|   `Club` varchar(30) NOT NULL,        |
|   `age_group` varchar(8) NOT NULL,    |
|   `DOB` varchar(11) NOT NULL,         |
|   `posistion` varchar(30) NOT NULL,   |
|   `contact` varchar(10) NOT NULL,     |
|   `f_name` varchar(30) NOT NULL,      |
|   `f_phone` varchar(30) NOT NULL,     |
|   `f_email` varchar(50) NOT NULL,     |
|   `m_name` varchar(30) NOT NULL,      |
|   `m_phone` varchar(30) NOT NULL,     |
|   `m_email` varchar(50) NOT NULL,     |
|   `medical_aid` varchar(30) NOT NULL, |
|   `med_num` varchar(15) NOT NULL,     |
|   `med_card` blob NOT NULL,           |
|   `ice_name` varchar(20) NOT NULL,    |
|   `ice_surname` varchar(20) NOT NULL, |
|   `ice_number` varchar(20) NOT NULL,  |
|   `jersey_size` varchar(5) NOT NULL,  |
|   `short_size` varchar(5) NOT NULL,   |
|   `golf_shirt` varchar(5) NOT NULL    |
+---------------------------------------+


团队的表结构:

+-------------------------------------+
| `Club` varchar(20) NOT NULL,        |
|   `Name` varchar(20) NOT NULL,      |
|   `Surname` varchar(20) NOT NULL,   |
|   `Posisiton` varchar(20) NOT NULL, |
|   `Picture` blob NOT NULL,          |
|   `Team` varchar(5) NOT NULL,       |
|   `date` date NOT NULL,             |
|   `Id_num` varchar(13) NOT NULL     |
+-------------------------------------+

最佳答案

我猜您在Id_numPlayers表中的Teams列是唯一的标识符。这是猜测,因为通常将此类唯一标识符定义为整数,以确保其唯一性。

    ...
    Id_Num INT NOT NULL AUTO_INCREMENT,
    ...
    PRIMARY KEY (Id_Num),
    ...


无论如何,如果要在球员和球队之间建立关系,则有两种选择。

首选:如果一个玩家只能加入一个团队:

这在我们的行业中被称为一对多关系。一队,很多球员。

Players表中添加一列,称为Team_Id_Num。当您将一个玩家分配到一个团队时,更新该玩家的行并将该团队的Id_Num值放入该玩家的列中。

然后,要让团队中的所有球员使用JOIN查询,如下所示:

   SELECT Teams.Name, Teams.Club, Players.Surname, Players.Name
     FROM Teams
     JOIN Players ON Players.Team_Id_Num = Teams.Id_Num
    WHERE Teams.Club = 'Manchester United'


最后,如果您真的想要一个SQL VIEW,可以执行以下操作

  CREATE VIEW Roster AS
       SELECT Teams.Name TeamName, Teams.Club, Players.Surname, Players.Name
         FROM Teams
         JOIN Players ON Players.Team_Id_Num = Teams.Id_Num


然后通过说来查询

      SELECT * FROM Roster WHERE Club = 'Arsenal'


第二选择:如果一名球员可以加入多个团队。

这在我们的行业中被称为多对多关系。很多球队,很多球员。

添加一个新表,其中包含两列,称为Players_Teams。该表定义了玩家和团队之间的关系。这些是列。有时称为联接表。

 Players_Id_Num
 Teams_Id_Num


将球员分配给球队时,请在此表中插入一行,其中包含该球员和所讨论球队的Id_Num值。

如果团队以后给麻袋装麻袋,则删除相应的行。

然后,要让团队中的所有球员使用JOIN查询,如下所示:

   SELECT Teams.Name, Teams.Club, Players.Surname, Players.Name
     FROM Teams
     JOIN Players_Teams ON Teams.Id_Num = Players_Teams.Teams_Id_Num
     JOIN Players ON Players_Teams.Player_Id_Num = Player.Id_Num
    WHERE Teams.Club = 'Manchester United'


最后,如果您确实想要SQL视图,则可以这样创建它

   CREATE VIEW Roster AS
        SELECT Teams.Name TeamName, Teams.Club, Players.Surname, Players.Name
          FROM Teams
          JOIN Players_Teams ON Teams.Id_Num = Players_Teams.Teams_Id_Num
          JOIN Players ON Players_Teams.Player_Id_Num = Player.Id_Num


并按照我之前显示的方式进行查询。

关于php - 如何在php和mysql中从已填充的表创建 View ?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43166402/

10-15 08:47