I am working on a School E-Learning Management System, Facing problem, that, on submit, the form values doesn't insert into table of Database, unless it display Success message "Classs Successfully Added" in form submit, but table remain empty, mean no data added in my table. Any suggestion? My codes are:
add_clas.php
<form method="post" id="add_class">
<label>Class Name:</label>
<input type="hidden" name="session_id" value="<?php echo $session_id; ?>">
<select name="class_id" class="" required>
<option></option>
<?php $query = mysql_query("select * from class order by class_name");
while($row = mysql_fetch_array($query)){
?>
<option value="<?php echo $row['class_id']; ?>"><?php echo $row['class_name']; ?></option>
<?php } ?>
</select>
<label>Subject:</label>
<select name="subject_id" class="" required>
<option></option>
<?php
$query = mysql_query("select * from subject order by subject_code");
while($row = mysql_fetch_array($query)){
?>
<option value="<?php echo $row['subject_id']; ?>"><?php echo $row['subject_code']; ?></option>
<?php } ?>
</select>
<label>School Year:</label>
<?php
$query = mysql_query("select * from school_year order by school_year DESC");
$row = mysql_fetch_array($query);
?>
<input id="" class="span5" type="text" class="" name="school_year" value="<?php echo $row['school_year']; ?>" >
<button name="save" class="btn btn-success"><i class="icon-save"></i> Save</button>
</form>
<script>
jQuery(document).ready(function($){
$("#add_class").submit(function(e){
e.preventDefault();
var _this = $(e.target);
var formData = $(this).serialize();
$.ajax({
type: "POST",
url: "add_class_action.php",
data: formData,
success: function(html){
if(html=="true")
{
$.jGrowl("Class Already Exist", { header: 'Add Class Failed' });
}else{
$.jGrowl("Classs Successfully Added", { header: 'Class Added' });
var delay = 1000;
setTimeout(function(){ window.location = 'dasboard_teacher.php' }, delay);
}
}
});
});
});
</script>
add_class_action.php
<?php
include('dbcon.php');
$session_id = $_POST['session_id'];
$subject_id = $_POST['subject_id'];
$class_id = $_POST['class_id'];
$school_year = $_POST['school_year'];
$query = mysql_query("select * from teacher_class where subject_id = '$subject_id' and class_id = '$class_id' and teacher_id = '$session_id' and school_year = '$school_year' ")or die(mysql_error());
$count = mysql_num_rows($query);
if ($count > 0){
echo 'true';
}else{
mysql_query("insert into teacher_class (teacher_id,class_id,subject_id,thumbnails,school_year) values('$session_id','$class_id','$subject_id','admin/uploads/thumbnails.jpg','$school_year')")or die(mysql_error());
$teacher_class = mysql_query("select * from teacher_class order by teacher_class_id DESC")or die(mysql_error());
$teacher_row = mysql_fetch_array($teacher_class);
$teacher_id = $teacher_row['teacher_class_id'];
$insert_query = mysql_query("select * from student where class_id = '$class_id'")or die(mysql_error());
while($row = mysql_fetch_array($insert_query)){
$id = $row['student_id'];
mysql_query("insert into teacher_class_student (teacher_id,student_id,teacher_class_id) value('$session_id','$id','$teacher_id')")or die(mysql_error());
echo "yes";
}
}
?>