值不会插入数据库表中

时间:2015-06-28 12:34:00

标签: php jquery mysql

我正在研究学校电子学习管理系统,面对问题,在提交时,表格值不会插入数据库表格,除非它显示成功消息"成功添加类别&# 34;在表单提交,但表保持为空,表示我的表中没有添加数据。有什么建议吗?我的代码是:

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

1 个答案:

答案 0 :(得分:2)

  1. 回声&#34;是&#34;在while循环中,它应该在外面
  2. 在循环丢失的情况下插入查询&#34; s&#34;在价值观中。
  3. 如果我猜对了,这就是你要找的东西; 尝试下面的代码,看看是否重新解决了这个问题。

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