php - 显示具有特定登录 ID 的数据库中的数据

标签 php html mysql database

首先,讲师将登录。然后讲师将被重定向到仪表板页面。它将显示讲师教授的类(class)。 下面是数据库: enter image description here

登录代码:

<?php
session_start(); // Starting Session
$error=''; // Variable To Store Error Message
if (isset($_POST['submit'])) {
if (empty($_POST['login_id']) || empty($_POST['login_password'])) {
$error = "ID or Password is invalid";
}
else
{
// Define $login_id and $login_password
$login_id=$_POST['login_id'];
$login_password=$_POST['login_password'];

// Establishing Connection with Server by passing server_name, user_id and password as a parameter
$connection = mysql_connect("localhost", "root", "");

// To protect MySQL injection for Security purpose
$login_id = stripslashes($login_id);
$login_password = stripslashes($login_password);
$login_id = mysql_real_escape_string($login_id);
$login_password = mysql_real_escape_string($login_password);

// Selecting Database
$db = mysql_select_db("attendance_system", $connection);
// SQL query to fetch information of registerd users and finds user match.
$query = mysql_query("select * from login where login_id='$login_id' AND login_password='$login_password'", $connection);
$rows = mysql_num_rows($query);
if ($rows == 1) {
    if (strlen($login_id) == 10) {
    $_SESSION['login_user']=$login_id; // Initializing Session
    header("location: dashboard-lecturer.php"); // Redirecting To Other Page
    }
    if (strlen($login_id) == 6) {
    $_SESSION['login_user']=$login_id; // Initializing Session
    header("location: dashboard-HEA.php"); // Redirecting To Other Page
    }
    if (strlen($login_id) == 5) {
    $_SESSION['login_user']=$login_id; // Initializing Session
    header("location: dashboard-prog_coordinator.php"); // Redirecting To Other Page
    }
} else {
$error = "ID or Password is invalid";
}
mysql_close($connection); // Closing Connection
}
}
?>

我用于显示数据的草稿代码:

<?php
$counter = 1;

$data = "SELECT * FROM course";         
                    $result = mysql_query($data) or die(mysql_error());                         

                        while($info = mysql_fetch_array( $result )) 
                        {

                        $course_code = $info['course_code'] ;
                        $course_name = $info['course_name'] ;
?>
                                                    <tr>
                                                        <td><?php echo $counter; 
                                                        $counter++; ?>
                                                        </td>
                                                        <td><?php echo $course_code; ?>
                                                            <ul class="table-mobile-ul visible-xs list-unstyled">
                                                                <li>Course Name: <?php echo $course_name; ?></li>
                                                            </ul>
                                                        </td>
                                                        <td class="hidden-xs"><?php echo $course_name; ?></td>
                                                        <td class='col-medium center'><a href='lec/stud_att_record.php'><button type='button' class='btn btn-sm btn-primary'><span class='glyphicon glyphicon-search'></span>View Students Attendance</button></a></td>
                                                    </tr>                                                   
<?php
}
?>

我的问题是我不知道如何为 SELECT 查询编写代码,以便显示的类(class)仅显示讲师只教授的类(class)。

最佳答案

So, I want to avoid redundant data in the course table

如果我错了,请纠正我。

每次您想避免类(class)表中出现冗余数据时。 这些数据将进入学生出勤表。

但是nb_students > nb_teachers(我相信):

因此,您将在学生在校表(教室、类(class)、学期)中获得大量信息,这些信息对于我属于类(class)表,并且是冗余人员.

无论如何,我认为获得讲师所做的所有类(class)的唯一方法是:

- JOIN the StudentAttendance with login_id of the current teacher
Result : returns all the student present in ALL the teacher's courses.

-  + GROUP BY id_course 
Result : returns all the teacher's course_id

- + JOIN with course_table 
Result : so we'll able to display the name of the course instead of id.

EDIT :
- JOIN staff table with login table 
Result : so u target the teacher related to the $_SESSION('login_id').

查询将如下所示:

SELECT course_name, staffAttendance.course_course_id
FROM login, course, staff, staffAttendance
WHERE staffAttendance.staff_staff_id = staff.staff_id AND
      staffAttendance.course_course_code = course.course_code AND
      staff.login_login_id = login.login_id AND 
      login.login_id = $_SESSION['login_user'] 

GROUP BY staffAttendance.course_course_id

顺便说一句,它非常丑陋。

关于php - 显示具有特定登录 ID 的数据库中的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30392621/

相关文章:

php - PHP中使用循环计算时间

根据更新请求插入 Java

java - 分析单词文本的算法

php - 什么是客户端准备好的语句?

javascript - 搜索栏在几个 Accordion 中查找行

jquery - javascript 没有在 flask 中加载管理模板

java - Java 中未加载 MySql 驱动程序

php - PostgreSQL 和 PHP 连接问题

php - 尝试复制时在路径中找不到文件

javascript - Angular 保存编辑的内容