关于数据库查询的IF语句不会执行

时间:2015-01-21 14:33:25

标签: php mysql

   <?php

if(empty($_POST) === false){
        $required_fields = array('teacher_id','class_id','startyr','endyr');
        foreach($_POST as $key=>$value){
            if(empty($value) && in_array($key, $required_fields)===true){
                $errors[]='Fields marked with an asterisk are required';
                break 1;
            }
        }if(empty($errors)===true){
            if(sched_exists($_POST['class_id'],$_POST['teacher_id'],$_POST['student_id']) === true){
                $errors[]='Schedule Already Exist!';

            }

        }   
    }
 ?>
<div class="container alert" id="showError">
 <?php


 if(empty($_POST) === false && empty($errors) === true){

 $cl_id=mysqli_real_escape_string($_POST['class_id']);
 $teacher_id=mysqli_real_escape_string($_POST['teacher_id']);
 $student_id=mysqli_real_escape_string($_POST['student_id']);
 $startyr=mysqli_real_escape_string($_POST['startyr']);
 $endyr=mysqli_real_escape_string($_POST['endyr']);

        $q="INSERT INTO sched(cl_id,teacher_id,student_id,startyr,endyr)
        values('{$cl_id}','{$teacher_id}','{$student_id}','{$startyr}','{$endyr}')";
    mysqli_query($q)or die(mysqli_error());

    mysqli_close($dbcon);
?>
<div class="alert alert-success">
<?php
    echo "<strong>Successfully Created!</strong> ";
?>
</div>
<?php 


    }else{
        echo output_errors($errors);
    }
?>
</div>
function sched_exists($cl_id,$teacher_id,$student_id){
    $cl_id;
    $teacher_id;
    $student_id;
    return (mysql_result(mysql_query("SELECT COUNT(`sched_id`) FROM `sched` WHERE `cl_id` = '$cl_id' AND `teacher_id` = '$teacher_id'"),0)==1) ? true : false or die(mysql_error());
    } 
你能告诉我什么错了吗?带有查询到数据库的if语句不会执行,如果我删除了sched的if语句,则查询将执行。我不知道这个问题是什么。请帮帮我

2 个答案:

答案 0 :(得分:0)

空($ _ POST)永远不会是假的。更好地要求if ( count($_POST) == 0)

答案 1 :(得分:0)

mysql_result(result,0)返回结果第一行第一列的值。这可能不是1,除非第一行是ID列并且第一个结果恰好是ID为1.你真的应该这样做:

$result = mysql_query(/*your query*/);
if ($result) return true;
else return false;