donglu7998 2015-01-21 14:33
浏览 104

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

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

can you tell me whats wrong? The if statement with the query to database wont execute and if i remove the if statement of sched exist the query will execute. i dont know whats the problem. Help me please

  • 写回答

2条回答 默认 最新

  • dtp19819 2015-01-21 14:56
    关注

    empty($_POST) is never false. better ask for if ( count($_POST) == 0)

    评论

报告相同问题?

悬赏问题

  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 关于无人驾驶的航向角
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了