SQL和PHP-努力显示给定学生的详细结果,以便在另一个PHP页面上查看

问题描述 投票:0回答:2

我正在努力实现并理解以下任务:我此任务的目标是创建一个页面,该页面允许用户显示上一页中给定学生的以下结果。

在此示例中,我创建了以下PhP页面,以创建一个for-each循环,以循环显示每个学生的ID,姓名和姓氏,用户可以通过简单地滚动按“提交”按钮来选择,以描绘以下详细信息将该学生转到下一页,如下所示(有效)。

这里是第一页:

<?php
   // Connect to database, and print error message if it fails
   try {
      $dbhandle = new PDO('mysql:host=dragon.kent.ac.uk; dbname=co323',
                          'co323', 'h@v3fun');
   } 
   catch (PDOException $e) {
      // The PDO constructor throws an exception if it fails
      die('Error Connecting to Database: ' . $e->getMessage());
   }

   // Run the SQL query, and print error message if it fails.
   $sql = "SELECT * FROM Student";

   $query = $dbhandle->prepare($sql);

   if ( $query->execute() === FALSE ) {
      die('Error Running Query: ' . implode($query->errorInfo(),' ')); 
   }

   // Put the results into a nice big associative array
   $results = $query->fetchAll();
     ?> 

 <form action="task7.php" method="GET" style="text-align: center;">
    <label for="Student">Select a student: </label>
        <select>
            <?php
                foreach ($results as $row) {
                    echo "<option>".$row['sid']." - ".$row['forename']." ".$row['surname']." - ".$row['gender']."</option>";
                }
            ?>
        </select>
    <input type="submit" name="student" id="student">
 </form>

主要问题是,基于我必须显示下一个学生的查询,基于主要目的是了解如何从第一页显示选定的学生以显示在下一页,并知道如何使用给定的SQL语句构造它。在ID上:

要使用的SQL代码

1)
  SELECT c.cid, title, name, weighting, mark
  FROM Grade g JOIN Assessment a ON g.aid = a.aid
  JOIN Course c ON a.cid = c.cid
  WHERE sid = 'S0001';

2)
  SELECT cid,
  SUM(mark*weighting)/100 AS Final
  FROM Grade g JOIN Assessment a ON g.aid = a.aid
  WHERE sid = 'S0001'
  GROUP BY cid;

第二页-看起来未完成

<?php
   // Connect to database, and print error message if it fails
   try {
      $dbhandle = new PDO('mysql:host=dragon.kent.ac.uk; dbname=co323',
                          'co323', 'h@v3fun');
   } 
   catch (PDOException $e) {
      // The PDO constructor throws an exception if it fails
      die('Error Connecting to Database: ' . $e->getMessage());
   }

   // Retrieving Student ID from task 6
   $studentID = $_GET['student'];

   // Run the SQL query, and print error message if it fails.
   $sql = "SELECT c.cid, title, name, weighting, mark
           FROM Grade g JOIN Assessment a ON g.aid = a.aid
           JOIN Course c ON a.cid = c.cid
           WHERE sid = :Student";

   $query = $dbhandle->prepare($sql);

   if ( $query->execute() === FALSE ) {
      die('Error Running Query: ' . implode($query->errorInfo(),' ')); 
   }

   // Put the results into a nice big associative array
   $results = $query->fetchAll();

?>

[请记住,我目前是初学者,正在学习PHP,因此希望您能从中获得任何线索或见识,因为这将有助于我了解如何处理PhP上的SQL数据并学习PhP本身的语言。

谢谢。

php html sql pdo
2个回答
0
投票

我很困惑!你有什么愿望但是也许如果您想在另一页上显示结果,则可以使用简单的提交按钮在foreach循环中为每个学生制作一个表格!然后将secondpagename.php放入表单操作中!

示例:

     <?php
                foreach ($results as $row) {
                    echo "<option>".$row['sid']." - ".$row['forename']." ".$row['surname']." - ".$row['gender']."</option>";
               ?>
//end php tag but for each not end yet
      <form method="post" action="secondpage.php">
                <input type="hidden" name="student" value="<?php echo $row['sid'] ?>">
                <input type="submit" name="student_profile" value="Profile">
            </form>
 <?php } ?>
//end for each

然后在第二页中,确保使用if(isset($_GET['student_profile'])){ $studentID = $_GET['student']; //write your fetch statement/query! }获得此值


0
投票

首先:不要在您的列表中包含数据库(或其他私人信息)。而是像$dbhandle = new PDO('dbConnect; dbname=xxx', 'xxxx', 'xxx');一样将它们清空第二:您的表单没有向PHP发送任何信息。改为将其更改为

 <form action="task7.php" method="GET" style="text-align: center;">
    <label for="Student">Select a student: </label>
        <select name='student'>
            <?php
                foreach ($results as $row) {
                    echo "<option>".$row['sid']." - ".$row['forename']." ".$row['surname']." - ".$row['gender']."</option>";
                }
            ?>
        </select>
    <input type="submit" name="submit"> <!-- note changes here -->
 </form>

您的PHP将以S001-Fred Smith的身份接收数据,因此您需要使用$temp = explode("-",$_GET['student']); $studentID = $tmp[0];删除信息>

© www.soinside.com 2019 - 2024. All rights reserved.