0
   <?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
    **Please, [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php).** They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). **Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement)** instead, and **use [PDO](http://us1.php.net/pdo).** – Jay Blanchard Jan 21 '15 at 14:34
  • 1
    How is the `sched_exists` defined? – pavel Jan 21 '15 at 14:36
  • @panther 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()); } this is how it is defined – Albert Saludaga Jan 21 '15 at 14:47
  • @JayBlanchard sorry i dont know how to use PDO as it was not discussed in our PHP class. But i could change it to mysqli_ is it muc better? – Albert Saludaga Jan 21 '15 at 14:48
  • `mysqli_` is a good start. BTW, don't dump code in comments. Change your original post to include any new information @AlbertSaludaga. And tell your teacher to quit teaching the `mysql_` functions. – Jay Blanchard Jan 21 '15 at 14:49
  • @JayBlanchard alright thanks for the tips :) – Albert Saludaga Jan 21 '15 at 14:53
  • check with `if(isset($_POST))` and what is `$errors`? – Riad Jan 21 '15 at 15:00
  • where should i put that? $errors is where the error statements are stored. @Riad – Albert Saludaga Jan 21 '15 at 15:16
  • @Riad $_POST is always set, but its content might be empty. – Whirlwind Jan 21 '15 at 15:29

2 Answers2

0

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

0

mysql_result(result,0) returns the value of the first column of the first row of the result. This will probably not be 1 unless it happens that the first row is an ID column and the first result happens to have an ID of 1. You really should be doing something like:

$result = mysql_query(/*your query*/);
if ($result) return true;
else return false;
HamHamJ
  • 435
  • 2
  • 10