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

时间:2015-05-22 09:06:23

标签: php html mysql database

首先,讲师将登录。然后讲师将重定向到仪表板页面。它将显示讲师教授的课程。 以下是数据库: 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查询编码,以便显示的课程只显示讲师教授的课程。

1 个答案:

答案 0 :(得分:1)

  

所以,我想避免课程表中的冗余数据

如果我错了,请纠正我。

每次你想避免课程表中的冗余数据。 这些数据将进入 studentAttendance表。

但是nb_students&gt; nb_teachers(我相信):

因此,您将在学生在场表(教室,课程,学期)中获得大量信息,其中 FOR ME 属于课程表,而是减少者

无论如何,我认为获得讲师所有课程的唯一方法是:

- 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').

QUERY将如下所示:

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
是的,它非常难看。