PHP和mysql:在提交后选择的选项保持在Combo中选中并验证数据已存在于数据库中

时间:2016-02-26 04:27:30

标签: php mysql

<div class="control-group">
<label class="control-label" for="inputPassword">School Year:</label>
<div class="controls">
<select name="year" required>
<option></option>
<?php
$query = mysql_query("select * from school_year");
while($row = mysql_fetch_array($query)){
?>
<option><?php echo $row['school_year']; ?></option>
<?php } ?>
</select>
</div>
</div>
<?php
if (isset($_POST['save'])){
$day = $_POST['day'];
$time = $_POST['time'];
    $room = $_POST['room'];
    $subj = $_POST['subj'];
    $sect = $_POST['sect'];
    $inst = $_POST['inst'];
    $sems = $_POST['sems'];
    $year = $_POST['year'];
    $notification  = 'New Proctoring Schedule on '." ".'<b>'.$day.'</b>'." - ".'<b>'.$time.'</b>'.' at '.$room.'<br>'.$subj. ' - '.$sect.' - '.$sems.' Semester.';
    $query = mysql_query("select * from schedule where day = '$day' and time = '$time' and room = '$room' ")or die(mysql_error());
    $count = mysql_num_rows($query);
if ($count > 0){ 
echo "Hello world!";
?>
<?php
}else{
mysql_query("insert into schedule (day,time,room,subject,section,proctor,semester, schoolyear) values('$day','$time','$room','$subj','$sect','$inst','$sems','$year')");
mysql_query("insert into notification (teacher_class_id,notification,date_of_notification,link) value('$inst','$notification',NOW(),'dasboard_teacher.php')");
mysql_query("insert into activity_log (date,username,action) values(NOW(),'$user_username','Add Exam Schedule $subj - $sect')")or die(mysql_error());
?>
<script>
window.location = "exam.php";
</script>
<?php
}
}
?>

1 个答案:

答案 0 :(得分:0)

试试这个

<?php
    $flag=0;

    if (isset($_POST['save'])){
        $day = $_POST['day'];
        $time = $_POST['time'];
        $room = $_POST['room'];
        $subj = $_POST['subj'];
        $sect = $_POST['sect'];
        $inst = $_POST['inst'];
        $sems = $_POST['sems'];
        $year = $_POST['year'];
        $notification  = 'New Proctoring Schedule on '." ".'<b>'.$day.'</b>'." - ".'<b>'.$time.'</b>'.' at '.$room.'<br>'.$subj. ' - '.$sect.' - '.$sems.' Semester.';
        $query = mysql_query("select * from schedule where day = '$day' and time = '$time' and room = '$room' ")or die(mysql_error());
        $count = mysql_num_rows($query);
        if ($count > 0){
            echo "Hello world!";
            $flag=1;
        }else{
            mysql_query("insert into schedule (day,time,room,subject,section,proctor,semester, schoolyear) values('$day','$time','$room','$subj','$sect','$inst','$sems','$year')");
            mysql_query("insert into notification (teacher_class_id,notification,date_of_notification,link) value('$inst','$notification',NOW(),'dasboard_teacher.php')");
            mysql_query("insert into activity_log (date,username,action) values(NOW(),'$user_username','Add Exam Schedule $subj - $sect')")or die(mysql_error());
            header("location:exam.php");
        }
    }
?>
<div class="control-group">
    <label class="control-label" for="inputPassword">School Year:</label>
    <div class="controls">
        <select name="year" required>
            <option></option>
            <?php
                $query = mysql_query("select * from school_year");
                while($row = mysql_fetch_array($query)){
            ?>
                    <option <?php if(isset($_POST['year']) && $row['school_year'] == $_POST['year']) { echo "selected='selected' "; } ?>  value="<?php echo $row['school_year'] ?>"> <?php echo $row['school_year']; ?></option>
            <?php } ?>
        </select>
    </div>
    <?php if($flag==1){ ?>
        <div class="error">Data already present in database</div>
    <?php } ?>
</div>