How should i insert this without inserting the duplicates datas ? here is the logic: if row is greater than 0 check weather there is a duplicate. if there is a duplicate check whether there are duplicates then get the unique datas and insert it on the table ese if datas are unique insert it on the table
if(isset($_POST['addenrolment']))
{
$courseid = $_POST['course'];
$schoolid = $_POST['school'];
if(mysqli_num_rows(mysqli_query($conn, "SELECT * FROM enrolments WHERE userid = userid AND courseid ='$courseid' AND schoolid ='$schoolid' ")) > 0){
header("Location: ../admin/enrolment.php?fail=duplicate");
}else{
mysqli_query($conn, "INSERT INTO enrolments (userid, schoolid, courseid, status) SELECT id, schoolid, '$courseid', 1 FROM users WHERE status = 1 AND schoolid = '$schoolid'");
header("Location: ../admin/enrolment.php?success=add");
}
}
i have tried this but all the data regardless if it is on the table or not (using phpmyadmin)
INSERT INTO enrolments (userid, schoolid, courseid, status) SELECT t1.id, t1.schoolid, '18', '1' FROM users t1 WHERE EXISTS(SELECT userid FROM enrolments t2 WHERE t2.userid = t1.id AND t2.schoolid = t1.schoolid AND t2.courseid = courseid )