0

I want to save date and time separately via HTML form to my table using prepared statements. However it is giving me error :

Fatal error: Uncaught Error: Call to a member function bind_param() on bool in Stack trace: #0 {main} thrown in on line 33.

Here is my Code: The Time entries are at the bottom. I am trying to save them as varchar in my DB

if(isset($_POST['CateSubmit'])){

    $test_type = $mysqli->real_escape_string($_POST['type2']);
    $course_id = $mysqli->real_escape_string($_POST['course_id']);
    $stream_id = $mysqli->real_escape_string($_POST['stream_id']);
    $subject_id = $mysqli->real_escape_string($_POST['subject_id']);
    $subject_id1 = $mysqli->real_escape_string(empty($_POST['subject_id1'])?'0':$_POST['subject_id1']);
    $chapter_id = $mysqli->real_escape_string(empty($_POST['chapter_id'])?'0':$_POST['chapter_id']);
    $exame_year = $mysqli->real_escape_string($_POST['exame_year']);
    $duration = $mysqli->real_escape_string($_POST['duration']);
    $nag_marks = $mysqli->real_escape_string($_POST['nag_marks']);
    $topic = $mysqli->real_escape_string(empty($_POST['topic'])?'':$_POST['topic']);
    $ap_qty = json_encode($_POST['ap_qty'],JSON_FORCE_OBJECT);
    $s_qty = json_encode($_POST['s_qty'],JSON_FORCE_OBJECT);
    $uniq_id = uniqid();
    $subject_name = $mysqli->real_escape_string($_POST['subject_name']);
    $exam_start_date = $mysqli->real_escape_string($_POST['exam_start_date']);
    $exam_end_date = $mysqli->real_escape_string($_POST['exam_end_date']);
    $exam_start_time = $mysqli->real_escape_string($_POST['exam_start_time']);
    $exam_end_time = $mysqli->real_escape_string($_POST['exam_end_time']);

    $query2= $mysqli->query("INSERT INTO digi_test_exame SET test_id='".$uniq_id."'");
    
    $query = "INSERT INTO digi_mock_test(test_type,course_id,stream_id,subject_id,subject_id1,chapter_id,exame_year,duration,nag_marks,ap_qty,s_qty,uniq_id,subject_name,exam_start_date,exam_end_date,topic,exam_start_time,exam_end_time)VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
    $statement = $mysqli->prepare($query);
    $statement->bind_param('ssssssssssssssssss', $test_type,$course_id,$stream_id,$subject_id,$subject_id1, $chapter_id, $exame_year,$duration,$nag_marks,$ap_qty,$s_qty,$uniq_id,$subject_name,$exam_start_date,$exam_end_date,$topic,$exam_start_time,$exam_end_time);

Here is The Form: Skipping the rest of the form for sake of saving time.

 <hr>
                        Exam Schedule
                        <hr>
                        <div class="form-row">
                            <div class="form-group col">
                          <label for="exam_start_date">Test Start Date</label>
                          <input type="date" class="form-control" name="exam_start_date">
                        </div>
                        
                        <div class="form-group col">
                          <label for="exam_end_date">Test End Date</label>
                          <input type="date" class="form-control" name="exam_end_date">
                        </div>
                        <div class="form-group col">
                          <label for="exam_end_date">Test Start At</label>
                          <input type="time" class="form-control" name="exam_start_time">
                        </div>
                        <div class="form-group col">
                          <label for="exam_end_date">Test End At</label>
                          <input type="time" class="form-control" name="exam_end_time">
                        </div>
                            </div>
                        <?php    
                        }
                        ?>
                        <button type="submit" class="btn btn-success mr-2" name="CateSubmit">Submit</button>
                      </form> 

Kindly Guide me what am i doing wrong. My Column names are correct and the rest of the query was working before adding up the time fields.

ADyson
  • 57,178
  • 14
  • 51
  • 63
  • The error message points to a problem in your query - it hasn't prepared, and therefore you cannot bind parameters to it. Look at that in more detail. Your `$statement` is almost certainly `false`. – droopsnoot Jun 02 '21 at 09:34
  • 1
    You should not escape the data when using prepared statement, so remove all `->real_escape_string(...)` calls. You should also use prepared statements on _all_ queries (including the first insert) – M. Eriksson Jun 02 '21 at 09:35
  • 1
    You should not use `$mysqli->real_escape_string` against fields which you are then passing as parameters. It's not necessary and it can corrupt the data. – ADyson Jun 02 '21 at 09:35

0 Answers0