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

时间:2015-09-28 12:30:06

标签: php mysql

我正在尝试查询数据库以检查插入前数据库中是否已更新当前计划的标记,我的代码如下:

    <?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;
        }
        }
    }
}
?>

我的问题是我无法执行if和else部分代码。

1 个答案:

答案 0 :(得分:0)

修改你的代码如下:

<?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;
        }
        }
    }
    }
 }
}
?>
相关问题