如何在插入之前查询数据库

I'm trying to query the data base to check whether the marks for current schedule is already updated in the data base before inserting, My code as follows:

    <?php 
include 'config.php';
if(isset($_POST['submit'])){
    $schedule = $_POST['hischedule'];
    $array = $_POST['marks'];
    //Qry to check Duplicate values Againest Database 
    $marksupdqry = "select distinct scheduleName from marks";
    $mks = mysqli_query($link, $marksupdqry);
    while($mksresult = mysqli_fetch_array($mks))
    foreach($mksresult as $sdlnme)
    if($sdlnme = $schedule){
        echo "<script> alert('Marks for this schedule already exist in db');</script>";
        echo "<script> window.location.href='marks.php';</script>";
    }else{  
        foreach ($array as $reg_num=>$score)
        {
        $ins = "INSERT INTO marks (sl_no, scheduleName, obtainedMarks) VALUES ('$reg_num', '$schedule', '$score')";
        $res = mysqli_query($link, $ins);
        if($res){
         //echo "Success";
         $Message = urlencode("Marks updated successfully ");
         header("Location:marks.php?Message=".$Message);
         die;
        }
        }
    }
}
?>

My problem is I'm unable to execute both if and else part of my code.

Modify your code like bellow:

<?php 
include 'config.php';
if(isset($_POST['submit'])){
    $schedule = $_POST['hischedule'];
    $array = $_POST['marks'];
    $marksupdqry = "select distinct scheduleName from marks";
    $mks = mysqli_query($link, $marksupdqry);
    while($mksresult = mysqli_fetch_array($mks)){
    foreach($mksresult as $sdlnme){
    if($sdlnme == $schedule){
        echo "<script> alert('Marks for this schedule already exist in db');</script>";
        echo "<script> window.location.href='marks.php';</script>";
    }else{  
        foreach ($array as $reg_num=>$score)
        {
        $ins = "INSERT INTO marks (sl_no, scheduleName, obtainedMarks) VALUES ('$reg_num', '$schedule', '$score')";
        $res = mysqli_query($link, $ins);
        if($res){
         //echo "Success";
         $Message = urlencode("Marks updated successfully ");
         header("Location:marks.php?Message=".$Message);
         die;
        }
        }
    }
    }
 }
}
?>