无法将数据插入mysql但未显示错误消息

时间:2017-03-19 07:26:52

标签: php mysql

试图创建学生注册页面,但数据不会插入到数据库中,并且没有显示错误消息,所以我不确定问题出在哪里。任何帮助表示赞赏! 这是代码:

    <form onsubmit="return Add_Validate()" class="form-horizontal"  action="AddStudent.php" method="POST">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title"><i class="fa fa-plus"></i> Add Student</h4>
      </div> 
      <div class="modal-body">


        <div class="form-group">
            <label class="col-sm-3 control-label">Student Name </label>
            <label class="col-sm-1 control-label">: </label>
                <div class="col-sm-8">
                  <input type="text" class="form-control" id="Add_Student_Name" placeholder="Student Name" name="Student_Name1" autocomplete="off" required>
                </div>
        </div>  

        <div class="form-group">
            <label  class="col-sm-3 control-label">Gender </label>
            <label class="col-sm-1 control-label">: </label>
                <div class="col-sm-8">
                  <select class="form-control" id="Add_Student_Gender" name="Student_Gender1" required>
                    <option value="">~~SELECT~~</option>
                    <option value="Male">Male</option>
                    <option value="Female">Female</option>
                  </select>
                </div></div>

        <div class="form-group">
            <label class="col-sm-3 control-label">Address </label>
            <label class="col-sm-1 control-label">: </label>
                <div class="col-sm-8">
                  <input type="text" class="form-control" id="Add_Student_Address" placeholder="Address" name="Student_Address1" autocomplete="off" required>
                </div>
        </div>          

        <div class="form-group">
            <label class="col-sm-3 control-label">Contact Number </label>
            <label class="col-sm-1 control-label">: </label>
                <div class="col-sm-8">
                  <input type="text" class="form-control" id="Add_Contact_Number" placeholder="Contact Number" name="Contact_Number1" autocomplete="off" required>
                </div>
        </div>

        <div class="form-group">
            <label class="col-sm-3 control-label">Email </label>
            <label class="col-sm-1 control-label">: </label>
                <div class="col-sm-8">
                  <input type="text" class="form-control" id="Add_Student_Email" placeholder="Email" name="Student_Email1" autocomplete="off" required>
                </div>
        </div>

        <div class="form-group">
            <label class="col-sm-3 control-label">Faculty </label>
            <label class="col-sm-1 control-label">: </label>
                <div class="col-sm-8">
                <select class="form-control" name="Add_Faculty1" id="FacultyName" required>
                    <option value="">~~SELECT~~</option>
                    <?php $query_faculty="SELECT FacultyName FROM `faculty`";
                    $result_faculty = mysqli_query($connect, $query_faculty) or die(mysqli_error($connect));
                    while($row_faculty=mysqli_fetch_array($result_faculty)) { ?>
                    <option value="<?php echo $row_faculty['FacultyName'] ?>"><?php echo $row_faculty['FacultyName'] ?></option>
                <?php } ?></select></div></div>

        <div class="form-group">
            <label  class="col-sm-3 control-label">Username </label>
            <label class="col-sm-1 control-label">: </label>
                <div class="col-sm-8">
                    <input type="text" class="form-control" id="Add_Student_Username" placeholder="Username" name="Student_Username1" autocomplete="off" required>
                </div></div>

        <div class="form-group">
            <label class="col-sm-3 control-label">Password </label>
            <label class="col-sm-1 control-label">: </label>
                <div class="col-sm-8">
                  <input type="password" class="form-control" id="Add_Password" placeholder="Password" name="Student_Password1" autocomplete="off" required>
                </div>
        </div>

和SQL命令:

<?php require 'php_action/db_connect.php'; 

if($_POST) {    

$Student_Name= $_POST['Student_Name1'];
$Student_Gender= $_POST['Student_Gender1'];
$Student_Address= $_POST['Student_Address1'];
$Contact_Number= $_POST['Contact_Number1'];
$Student_Email= $_POST['Student_Email1'];
$FacultyName= $_POST['Add_Faculty1'];
$Student_Username= $_POST['Student_Username1'];
$Password= $_POST['Student_Password1'];

$sql = "INSERT INTO student (Student_Name,Student_Gender,Student_Address,Contact_Number,Student_Email,FacultyName,Student_Username,Password) VALUES ('$Student_Name', '$Student_Gender','$Student_Address','$Contact_Number','$Student_Email','$FacultyName','$Student_Username','$Password')";



if($connect->query($sql) === TRUE) {
    echo "<SCRIPT>alert('Student successfully added!');document.location='Student_Register.php'</SCRIPT>";
} 
else {
    echo "<SCRIPT>alert('Student add unsuccessful!');document.location='Student_Register.php'</SCRIPT>";
}


}
$connect->close();
 ?>

2 个答案:

答案 0 :(得分:0)

您没有收到错误消息,因为您从未收到来自MySQL的错误消息并在警报中显示它。 $connect->error将包含错误消息,您可以将其添加到警报中,如下所示:

else {
    echo "<SCRIPT>alert('Student add unsuccessful! Reason: ' + " . json_encode($connect->error) . ");document.location='Student_Register.php'</SCRIPT>";
}

答案 1 :(得分:0)

发现错误,列名似乎错了,对不起麻烦..... 管理员请关闭我的问题。