Values ​​are not inserted into the database table

I am working on a school eLearning management system, facing an issue that when submitting the form values ​​are not inserted into the database table, unless it displays the "Successfully added classes" message on the submit form, but the table remains empty does not mean the data not added to my table. Any suggestion? My codes:

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";
}
}
?>

      

+3


source to share


1 answer


  • Echo yes inside the while loop, it should be outside
  • Insert a query inside a loop that has no "s" values ​​in the values.

and if I am guessing, this is what you are looking for; Try the following code to solve the problem.



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";
}

      

+2


source







All Articles