准备好的声明不将数据插入数据库

时间:2018-03-20 21:35:11

标签: php html mysqli prepared-statement

我准备好的将数据插入数据库的语句不起作用。我已经遇到了这些问题,但我只是包含一个例子,因为我犯了一个简单的错误。查询运行正常,因为我收到一条消息,我将自己置于代码中,但是没有任何内容输入到实际数据库中。到目前为止,我的准备陈述中的问题是,当某些东西不起作用时,你会得到反馈。任何帮助将不胜感激。

 <?php
 if(isset($_POST['newsubject'])){
 include('../connection/conn.php');

 //Prepare the insert statement
 $insertquery = "INSERT INTO miiLearning_Tutors(tutor_id,subject_level, 
  price, subjects) VALUES (?,?,?,?)";

if($stmt = mysqli_prepare($conn, $insertquery)){
//bind variable to the prepared statement as parameters
mysqli_stmt_bind_param($stmt, "iidi", $newtutor, $newsubject, 
$newlevel, $newprice);

//Set Values
$newtutor = $_POST["tutorId"];
$newsubject = $_POST["subjects"];
$newlevel = $_POST["subjectlevel"];
$newprice = $_POST["price"];

mysqli_stmt_execute($stmt);

 echo"<p>Query Ran</p>";
} else{
 echo "ERROR: Could not prepare query: $query . " .mysqli_error($conn);
}

}
?>

表单的HTML:

<form enctype="multipart/form-data" action='updatesubjects.php' method="post" id="update-subjects-form" name="new-subject" >
                                <fieldset>
                                    <!--Tutor ID (Posted from previous page) -->
                                    <input type="hidden" name="tutorId" value='<?php echo "$userarray[0]";?>'>
                                    <!-- Subject -->
                                    <div class="form-group">
                                        <label for="subjects">Subject</label>
                                        <select name="subjects" type="text" class="form-control">
                                            <?php
                                            if(mysqli_num_rows($subjectsresult)>0){

                                                while($row = mysqli_fetch_assoc($subjectsresult)){
                                                    $get_subjectid = $row['subject_id'];
                                                    $get_subjectname = $row['subject'];

                                                    echo "<option value='$get_subjectid'>$get_subjectname</option>";
                                                }
                                            }
                                            ?>   
                                        </select>
                                    </div>
                                    <!-- Level -->
                                    <div class="form-group">
                                        <label for="subjectlevel">Subject Level</label>
                                        <select name="subjectlevel" type="text" class="form-control">
                                            <?php 
                                            if(mysqli_num_rows($levelresult) > 0){
                                                while($row = mysqli_fetch_assoc($levelresult)){
                                                    $get_levelid = $row['level_id'];
                                                    $get_namelevel = $row['level'];
                                                    echo "<option value='$get_levelid'>$get_namelevel</option>";

                                                }
                                            }
                                            ?>
                                        </select>
                                    </div>
                                    <div class="form-group">
                                        <label for="subjectlevel">Price</label>
                                        <input type='number' step='0.01' min='0' name='price'>
                                    </div>
                                    <button class="btn btn-primary" type="submit" name="newsubject" id="bookingsform">Submit form</button>
                                </fieldset>
                            </form>

我为任何糟糕的缩进道歉

1 个答案:

答案 0 :(得分:0)

您需要在绑定之前声明变量并为它们赋值。目前你应该有未定义的变量。

在开发环境中,确保启用错误报告。

 <?php
 error_reporting(-1);
 ini_set('display_errors', 1);
 if(isset($_POST['newsubject'])){
 include('../connection/conn.php');

//Set Values
$newtutor = $_POST["tutorId"];
$newsubject = $_POST["subjects"];
$newlevel = $_POST["subjectlevel"];
$newprice = $_POST["price"];
 //Prepare the insert statement
$insertquery = "INSERT INTO miiLearning_Tutors(tutor_id,subject_level, price, subjects) VALUES (?,?,?,?)";

if($stmt = mysqli_prepare($conn, $insertquery)){
//bind variable to the prepared statement as parameters
mysqli_stmt_bind_param($stmt, "iidi", $newtutor, $newsubject, $newlevel, $newprice);

mysqli_stmt_execute($stmt);

echo"<p>Query Ran</p>";
} else{
   echo "ERROR: Could not prepare query: $query . " .mysqli_error($conn);
}

}
 ?>