Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 数据库查询上的IF语句无法执行_Php_Mysql - Fatal编程技术网

Php 数据库查询上的IF语句无法执行

Php 数据库查询上的IF语句无法执行,php,mysql,Php,Mysql,你能告诉我怎么了吗?带有对数据库的查询的if语句不会执行,如果我删除sched exist的if语句,查询将执行。我不知道是什么问题。请帮帮我空$\u帖子从来都不是假的。如果count$\u POST==0,则最好询问,0返回结果第一行第一列的值。这可能不是1,除非第一行是ID列,并且第一个结果的ID恰好为1。你真的应该这样做: <?php if(empty($_POST) === false){ $required_fields = array('teacher_

你能告诉我怎么了吗?带有对数据库的查询的if语句不会执行,如果我删除sched exist的if语句,查询将执行。我不知道是什么问题。请帮帮我

空$\u帖子从来都不是假的。如果count$\u POST==0,则最好询问,0返回结果第一行第一列的值。这可能不是1,除非第一行是ID列,并且第一个结果的ID恰好为1。你真的应该这样做:

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

请它们不再得到维护,而是在使用。了解并使用。sched_exists是如何定义的?@panther函数sched_exists$cl_id、$teacher_id、$student_id{$cl_id;$teacher_id;$student_id;返回mysql_resultmysql_querySELECT COUNTsched_id,其中cl_id='$cl_id'和teacher_id='$teacher_id',0==1?true:false或diemysql_错误;}事情就是这样defined@JayBlanchard抱歉,我不知道如何使用PDO,因为我们的PHP课程中没有讨论过它。但是我可以把它改成mysqli_uu。它更好吗?mysqli_u是一个好的开始。顺便说一句,不要在注释中转储代码。更改您原来的帖子,以包含任何新信息@AlbertSaludaga。并告诉您的老师停止教授mysql_u函数。将此添加到函数中后,即使数据库中没有该计划,sched_u仍然会执行。
$result = mysql_query(/*your query*/);
if ($result) return true;
else return false;