-7

i want this code to show me a error message when student already register a course

 $reds = mysqli_query($db,"SELECT count(*) FROM `course` " );
    $row = mysqli_fetch_row($reds);
    $num = $row[0];

echo $num>0 ; die();
   var_dump($num) ;die();
    if ($num>0){
   // echo "record already exits";
}

else {


    $sd = "INSERT INTO `users_course`(users_id,course_id) VALUES ( ' {$_SESSION['users_id']}', '$course_id')";
    $r = mysqli_query($db, $sd);
        if ($r) {

        $m = ' register';
        header('location: registercourse.php'); //redirect to index page after inserting
splash58
  • 26,043
  • 3
  • 22
  • 34
dannycool
  • 17
  • 5

3 Answers3

0

You just need to pass users_id in your query to check if a particular student (user id) has registered for the course or not.

$reds = mysqli_query($db,"SELECT count(*) FROM `users_course` where users_id = `".$_SESSION['users_id']."` and course_id='".$course_id."'" );

Because with current SELECT query, you are checking only if there is any course present in the course table. So as there would be some rows, it is giving your error message that course is already there.

Hope this is the clear explaination for you.

Himanshu Upadhyay
  • 6,558
  • 1
  • 20
  • 33
0

I don't know what is the structure of your database but i think you have to change this query 'SELECT count() FROM course ' to "SELECT count() FROM users_course WHERE course_id = '{$_SESSION['users_id']}' AND user_id = '$course_id'"

Momen Shaker
  • 141
  • 1
  • 9
0

Do like below:-

<?php
    session_start(); //start session to use session variable
    $user_id = $_SESSION['users_id'];
    $reds = mysqli_query($db,"SELECT * FROM `users_course` WHERE users_id = $user_id AND course_id= $course_id " );

    if (mysqli_num_rows($reds)>0){
        echo "record already exits";exit;
}else {
    $sd = "INSERT INTO `users_course`(users_id,course_id) VALUES ( $user_id, $course_id)";
    $r = mysqli_query($db, $sd);
    if ($r) {
        $m = ' register';
        header('location: registercourse.php'); //redirect to index page after inserting
    }else{
        echo "error:-".mysqli_error($db);
    }
}

Note:- May be above code worked for you currently, but it is wide open for SQL Injection, so try to read prepared statements and use them.

mysqli.prepare

So check this for an example:-

<?php
    session_start(); //start session to use session variable
    $user_id = $_SESSION['users_id'];
    $stmt = mysqli_prepare($db, "SELECT * FROM `users_course` WHERE users_id = ? AND course_id= ?");
    mysqli_stmt_bind_param($stmt, 'ii', $user_id, $course_id);

    /* execute query */
    mysqli_stmt_execute($stmt);

    /* store result */
    mysqli_stmt_store_result($stmt);

    if(mysqli_stmt_num_rows($stmt)>0){
        /* close statement */
        mysqli_stmt_close($stmt);
        echo "record already exits";exit;
    }else{
        $stmt = mysqli_prepare($db,"INSERT INTO `users_course`(users_id,course_id) VALUES (?, ?)";
        mysqli_stmt_bind_param($stmt, 'ii', $user_id, $course_id);
        if ( mysqli_stmt_execute($stmt)) {
            $m = ' register';
            header('location: registercourse.php'); //redirect to index page after inserting
        }else{
            echo "error:-".mysqli_error($db);
        }
    }

/* close connection */
mysqli_close($db);
?>
Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98