简体   繁体   中英

Updating checkbox into mysql database

How do i update checkbox value into database

$fmcourse = $_POST['fm_courses'];
foreach($fmcourse as $fmc) {
    $studentid = mysqli_real_escape_string($con, $_POST["id"]);
    $sql = "UPDATE enrollments SET course_fk='$fmc' WHERE student_fk = '$studentid'";
}

var_dump(fmc) output this

string(2) "18" string(2) "20" string(2) "22" string(2) "24" string(2) "26" 

When i update the course_fk repeats the same id on all the rows

+-----+------------+-----------+
| eid | student_fk | course_fk |
+-----+------------+-----------+
|  1  |          1 |        17 |
|  2  |          1 |        17 |
|  3  |          1 |        17 |
+-----+------------+-----------+

I am looking for this

+-----+------------+-----------+
| eid | student_fk | course_fk |
+-----+------------+-----------+
|  1  |          1 |        17 |
|  2  |          1 |        20 |
|  3  |          1 |        22 |
+-----+------------+-----------+

The code below checks, using the function record_exists() , if the record has duplicates in enrollments table, if it has, then the code just updates the duplicates accordingly. If no duplicates, then it creates a new enrollment record for a student.

Updated Code.

$fmcourse = $_POST['fm_courses'];
    $student_id = $_POST["id"];
    $batch_id = $_POST["name"];
    delete_enrollments( $student_id, $batch_id );

    foreach($fmcourse as $fmc) {
        $course_id = $fmc;

        if( record_exists( $student_id, $course_id, $batch_id ) == FALSE ) {
            $stmt = mysqli_prepare($conn, "INSERT INTO enrollments (enrollment_id, student_id, course_id, batch_id, is_deleted, joining_date) VALUES( NULL, ?, ?, ?, 0, NOW() );");
            $is_binded = mysqli_stmt_bind_param($stmt, "iii", $student_id, $course_id, $batch_id);
            $is_exec = mysqli_stmt_execute($stmt);
            mysqli_stmt_close($stmt);
        }
        else {
            undelete_enrollment($student_id,  $course_id, $batch_id);
        }
    }

    function undelete_enrollment($student_id,  $course_id, $batch_id) {
        global $conn;
        $stmt = mysqli_prepare($conn, "UPDATE enrollments SET is_deleted = 0, joining_date = NOW() WHERE student_id= ? AND course_id = ? AND batch_id = ?");
        $is_binded = mysqli_stmt_bind_param($stmt, "iii", $student_id, $course_id,$batch_id);
        $is_exec = mysqli_stmt_execute($stmt);
        mysqli_stmt_close($stmt);
        return $is_exec;
    }

    function record_exists( $student_id,  $course_id, $batch_id ) {
        global $conn;

        $stmt = mysqli_prepare($conn, "SELECT COUNT(enrollment_id) as total FROM enrollments WHERE student_id= ? AND course_id = ? AND batch_id = ?");
        $is_binded = mysqli_stmt_bind_param($stmt, "iii", $student_id, $course_id, $batch_id);
        $is_exec = mysqli_stmt_execute($stmt);
        $result = mysqli_stmt_get_result($stmt);
        $record = mysqli_fetch_assoc($result);
        mysqli_stmt_close($stmt);
        return ( isset( $record['total'] ) AND $record['total'] > 0 );
    }

    function delete_enrollments( $student_id, $batch_id ) {
        global $conn;
        $stmt = mysqli_prepare($conn, "UPDATE enrollments SET is_deleted = 1 WHERE student_id = ? AND batch_id =?;");
        $is_binded = mysqli_stmt_bind_param($stmt, "ii", $student_id, $batch_id);
        $is_exec = mysqli_stmt_execute($stmt);
        mysqli_stmt_close($stmt);
        return $is_exec;
    }

You need to update your record according to eid not as student_fk

You also need eid . As your student_fk value is repeated. It always update last value

So your where condition have issue. You need to add condtion of eid instead of student_fk Also make sure $eid is difference for each row.

$sql = "UPDATE enrollments SET course_fk ='$fmc' WHERE eid = '$eid'";

Also your code is open for SQL injection. To prevent from this use PREPARED STATEMENTS

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM