3

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";
}
}
?>
Sun_saurya
  • 33
  • 10
  • try something like this $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')"; $result = mysql_query($query) or die(mysql_error()); – Shehary Jun 28 '15 at 12:47
  • 3
    1. Your code is **extremely vulnerable** as it open to `sql injections`. 2. You're using `mysql_` which is **deprecated**, consider using `pdo` or `mysqli` instead. – Ofir Baruch Jun 28 '15 at 12:47
  • 1
    It's now not a duplicate question of your own but a triplicate all in 24 hours – Drew Jun 28 '15 at 13:02
  • 1
    You said you rewrote it as suggested then abandoned those questions – Drew Jun 28 '15 at 13:03
  • @DrewPierce I closed this question as per the exact duplicate where answers were given, including one of yours. – Funk Forty Niner Jun 28 '15 at 19:03
  • 2
    @Fred-ii- at this point I am just marketing links to your answers ;) – Drew Jun 28 '15 at 19:08

1 Answers1

2
  1. Echo "yes" inside while loop, it should be outside
  2. Insert query inside while loop missing "s" in Values.

and if I'm guessing right this is what you are looking for; Try follwing code see if it resloves the issue.

else{
    $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')";
    $result = mysql_query($query) or die(mysql_error());

    $teacher_class = mysql_query("SELECT * FROM teacher_class ORDER BY teacher_class_id DESC LIMIT 1") 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'];
    $insertquery = "INSERT INTO teacher_class_student (teacher_id,student_id,teacher_class_id) VALUES ('$session_id','$id','$teacher_id')";
    $result = mysql_query($insertquery) or die (mysql_error());
    }
    echo "yes";
}
Shehary
  • 9,926
  • 10
  • 42
  • 71
  • I did what you have suggested. But, not works. Anything else suggestion to solve my problem? – Sun_saurya Jun 29 '15 at 02:50
  • not works is not enough for us to figure out what's not working, paste this `error_reporting(E_ALL); ini_set('display_errors',1);` in your add_class_action.php and and check error log file, see what error you are getting. – Shehary Jun 29 '15 at 03:03