【问题标题】:Arranging data from sql database with php用php从sql数据库中排列数据
【发布时间】:2019-04-29 20:59:18
【问题描述】:

我无法弄清楚如何将 sql 实现到我的网页的 php 代码中。不幸的是,这是一项任务。

如果有人可以提供帮助,那就太棒了,因为我今天有人要帮助我,但他们在最后一刻取消了,我仍然坚持这一点。

页面上的我的专业区域,每个专业(comp sci、eng、bus)都必须是可点击的,当它们被点击时,它会更新 index.php 页面并在表格中显示具有该专业的学生.我确实使用majorID 作为外键来识别专业。我不知道如何使用 php 来做到这一点。

我正在考虑使用 foreach 循环使用此代码,但当我遇到太多错误时,我遇到了太多错误,其中一个是未定义的索引和致命的未捕获 pdo。

<aside>
        <!-- display a list of majors-->
        <h2>Majors</h2>
        <nav>
        <ul>
        <?php foreach ($majors as $major) : ?>
            <li>
            <a href="?majorID=<?php 
                      echo $major['majorID']; ?>">
                <?php echo $major['majorname']; ?>
            </a>
            </li>
        <?php endforeach; ?>
        </ul>
        </nav>
    </aside>

这是我的 .sql 代码

DROP DATABASE IF EXISTS richard_ricardo_student_db;

CREATE DATABASE richard_ricardo_student_db;
DROP USER 'richardricardo1'@'localhost';
CREATE USER 'richardricardo1'@'localhost' IDENTIFIED BY 'richardisgreat';
GRANT SELECT, INSERT, UPDATE, DELETE ON `richard\_ricardo\_student\_db`.* TO 'richardricardo1'@'localhost';

CREATE table major (
 majorID int NOT NULL AUTO_INCREMENT,
 majorname varchar(255),
 PRIMARY KEY (majorID)
);

CREATE table student (
 studentID int NOT NULL AUTO_INCREMENT,
 majorID int,
 firstname varchar(255),
 lastname varchar(255),
 gender varchar(10),
 PRIMARY KEY (studentID),
 FOREIGN KEY (majorID) REFERENCES major (majorID)
);

INSERT INTO major (majorID, majorname)
VALUES (1, "Computer Science"),(2, "Electrical Engineering"),(3, "Business");

INSERT INTO student (studentID, majorID, firstname, lastname, gender)
VALUES (1, 1, "Po", "Black", "M"),(2, 1, "Shifu", "Hoffman", "M"),(3, 1, "Tigress", "Jolie", "F"),(4, 1, "Jennifer", "Yuh", "F"),(5, 1, "Ox", "Storming", "M"),(6, 2, "Monkey", "Chan", "M"),(7, 1, "Viper", "Liu", "F"),(8, 2, "Mantis", "Rogen", "M"),(9, 3, "Crane", "Cross", "M"),(10, 3, "Oogway", "Kim", "M"),(11, 3, "Ping", "Hong", "M");

这是我的 index.php 代码,它连接到 phpmyadmin 上的 sql 数据库和我的 database.php 文件。

<!DOCTYPE html>

<html lang="en">

<head>
    <meta charset="utf-8">
    <link rel="stylesheet" href="main.css?v=1">
    <title>richard ricardo kung fu school</title> 
</head>

<body>
<header>
<h1>Richard Ricardo Kung Fu School - Students</h1>
</header>

<section> 
<div id="MajorList">
<h2>Majors</h2>

<div id="MajorPadding">
<?php
include('richard_ricardo_database.php');

$stmt = $pdo->query('SELECT * FROM Major');
while ($row = $stmt->fetch())
{
    echo $row['majorname']. "<br><br>";
}
?>
</div>
</div> 
<div id="StudentList">
<h2>Student List</h2>

<table class="greyGridTable">
<thead>
<tr>
<th>Student ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>Gender</th>
<th></th>
</tr>
<?php
$stmt = $pdo->query('SELECT * FROM Student');
while ($row = $stmt->fetch())
{
    echo "<tr><td>".$row['studentID']."</td><td>".strtoupper($row['firstname'])."
    </td><td>".strtoupper($row['lastname'])."</td><td>".$row['gender']."
    </td><td><a href='richard_ricardo_delete_student.php?id=".$row['studentID']."'>
    <button>Delete</button></a></td></tr>";
}
?>
</table>

<br>
<a href="richard_ricardo_add_student_form.php">Add Student</a>
<br>

<br>
<a href="richard_ricardo_add_major_form.php">List / Add Major</a>
<br>
</div>
</section>

<br>
<footer>&copy; 2015 richard ricardo kung fu school</footer>

</body>

</html>

抱歉,页面太长了,我也不知道如何插入较小的图片。如果我点击计算机科学作为专业,这就是它应该看起来的样子。哦,database.php 文件也使用了一个 pdo 对象。

【问题讨论】:

    标签: php html css sql phpmyadmin


    【解决方案1】:

    如果我正确理解了您的问题,您希望能够看到所选专业的所有学生吗?您的查询将与您提供的架构类似:

    SELECT studentID, firstname, lastname, gender
    FROM   student
           JOIN major ON student.majorID = major.majorID
    WHERE  major.majorID = 2;
    

    这将显示电气工程专业的所有用户

    编辑:

    由于这是一项任务,我将尽我所能提供帮助,而无需为此部分编写确切的代码。因此,对于左侧的专业,您可以将每个专业与一个 ID 和send it to the page 相关联:

    http://localhost/studentmajors.php?major=1
    

    在 PHP 中,您将使用$_GET“major”,然后继续前面的查询 id 为 1,但在执行此类操作时要小心,因为它会打开 SQL 注入。

      $major_id = $_GET['major'];
    

    您需要验证输入。我认为这超出了这个范围。

    【讨论】:

    • 这确实让桌子只出现在那个专业的学生身上。我是否应该在 if else if 语句中这样做,所以如果我点击计算机科学链接,它会显示 majorID = 1 的学生?
    • 非常感谢 sqlsquirrel 的帮助。我相信我理解得更好,我仍然有点坚持获得 php 专业。我已经为每个链接分配了一个 ID,尽管我一直坚持让链接更新信息。
    • @Saiba Irevised the edit, but also check here: teamtreehouse.com/community/…
    • 哦,好吧,我现在肯定明白了。再次感谢您的帮助! :)
    • @Saiba 不客气。如果您认为这是一个可以接受的解决方案,如果您可以接受答案。 :)
    猜你喜欢
    • 2021-12-08
    • 2020-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多