How to check if multiple records exists php mysql pdo

I am designing a school timetable and this is my first time. So I send multiple records for multiple classes from HTML to php.For instance teacher1 geography Monday Start 11:35 grade 7and then techer2 geography Monday Start 12:15 grade 4. If the data am sending times for same class same day or same teacher exists then the system should skip that and insert only that which does not exist. but now my code checks and if it finds one of the records exists , it does not proceed. I want it to process with the one which does not exist and ignore the one which exits. I need help and below is my code .

$usercode1 = "7892Hga";
$recieved_by = ($_SESSION['username']);
$errors = [];  // Array to hold validation errors
$data = [];   // array to pass back data
$teacherid = $_POST['teacherid'];
$subjectid = $_POST['subjectid'];
$locationid = $_POST['locationid'];
$start_time = $_POST['start_time'];
$end_time = $_POST['end_time'];
$courseid = $_POST['courseid'];
$dayid = $_POST['dayid'];
var_dump($teacherid);
/*Check if the record exits or techer was booked before 15-10-2021 21:00 Musa Capsicum */
foreach ($teacherid as $teacherid2) {
    foreach ($start_time as $start_time2) {
        foreach ($dayid as $dayid2) {
            foreach ($courseid as $courseid2) {
                // echeck if exist
                $check_exists = "SELECT 
    subject_timetable.teacherid,
    subject_timetable.subjectid,
    subject_timetable.locationid,
    subject_timetable.start_time,
    subject_timetable.end_time,
    subject_timetable.courseid
    FROM subject_timetable 
    WHERE subject_timetable.usercode = '$usercode'
    && subject_timetable.teacherid = '$teacherid2'
    && DATE_FORMAT(`start_time`, '%H:%i')= '$start_time2'
    && subject_timetable.dayid = '$dayid2'
    OR (
        subject_timetable.usercode = '$usercode' 
        && DATE_FORMAT(`start_time`, '%H:%i')= '$start_time2'
        && subject_timetable.courseid = '$courseid2'
        && subject_timetable.dayid = '$dayid2'
    )";
                $exist_check = $con->prepare($check_exists);
                $exist_check->execute();
                $counterRow = $exist_check->rowCount();
                if ($counterRow > 0) {
                    echo("$counterRow $usercode $teacherid2 We have this ");
                    exit();
                } else {
                    echo("$counterRow $usercode $teacherid2 No record Exists ");
                }
                while ($row_exist = $exist_check->fetch(PDO::FETCH_ASSOC)) {

                }
            }
        }
    }
}
//end  exist
?>
<?php
if (empty($teacherid)) {
    $errors['teacherid'] = 'Teacher is required.';
}
if (empty($_POST['subjectid'])) {
    $errors['subjectid'] = 'Subject for is required.';
}
if (empty($_POST['dayid'])) {
    $errors['dayid'] = 'Day for is required.';
}
//$date_start_time = date_create_from_format('Y-m-d',$start_time);
if (empty($_POST['start_time'])) {
    $errors['start_time'] = 'Start date required.';
}
//Check date  to
//$date_end_time = date_create_from_format('Y-m-d',$end_time);
if (empty($_POST['start_time'])) {
    $errors['start_time'] = 'End time required.';
}
if (!empty ($errors)) {
    // if there are items in our errors array, return those errors
    $data['success'] = false;
    $data['errors'] = $errors;
    exit();
} else {
    // if all is good process the form
    /////////
    $count = count($_POST['teacherid']);
    if (is_array($teacherid)
        && is_array($subjectid)
        && is_array($locationid)
        && is_array($start_time)
        && is_array($courseid)
        && is_array($dayid)
        && is_array($end_time)) {
        for ($i = 0; $i < $count; $i++) {
            $teacherid1 = $teacherid[$i];
            $subjectid1 = $subjectid[$i];
            $locationid1 = $locationid[$i];
            $start_time1 = $start_time[$i];
            $end_time1 = $end_time[$i];
            $dayid1 = $dayid[$i];
            $courseid1 = $courseid[$i];
            #########################################################
            $query = 'INSERT INTO subject_timetable(teacherid,subjectid,locationid,start_time,end_time,dayid,courseid,usercode)
    VALUES(:teacherid,:subjectid,:locationid,:start_time,:end_time,:dayid,:courseid,:usercode)';
            $insert = $con->prepare($query);
            $insert->execute([
                ':teacherid' => $teacherid1,
                ':subjectid' => $subjectid1,
                ':locationid' => $locationid1,
                ':start_time' => $start_time1,
                ':end_time' => $end_time1,
                ':dayid' => $dayid1,
                ':courseid' => $courseid1,
                ':usercode' => $usercode,
            ]);
            $data['success'] = true;
            $data['message'] = 'Success!';
        }
    }
}


from Recent Questions - Stack Overflow https://ift.tt/3Bt0lks
https://ift.tt/eA8V8J

Comments

Popular posts from this blog

Today Walkin 14th-Sept

Spring Elasticsearch Operations

Hibernate Search - Elasticsearch with JSON manipulation