Error saving data in MYSQL Database through php

Viewed 25

I am a beginner in PHP and am trying to save data to MySql database using HTML form and PHP but I get the following error: error_image

code for my HTML Form:

    <form method="post" action="http://localhost:63342/sqlvali-master111/publicRoot/sqlvali/index.php?action=lec_hub/pages" enctype="multipart/form-data">
                        <div class="form-group">
                            <label for="description" class="control-label">Description:</label>
                            <input type="text" class="form-control" id="description" name="description">
                        </div>
                        <div class="form-group">
                            <label for="external_link" class="control-label">External Link:</label>
                            <input type="text" class="form-control" id="external_link" name="external_link">
                        </div>
                        <div class="form-group">
                            <label for="query_structure" class="control-label">Query Structure:</label>
                            <input type="file" class="form-control" id="query_structure" name="query_structure">
                        </div>
                        <div class="form-group">
                            <label for="feedback" class="control-label">Feedback:</label>
                            <input type="text" class="form-control" id="feedback" name="feedback">
                        </div>
                        <div class="form-group">
                            <label for="likert" class="control-label">Likert-type Scale:</label>
                            <input type="text" class="form-control" id="likert" name="likert">
                        </div>
                        <div class="form-group">
                            <label for="query_example" class="control-label">Query Example:</label>
                            <input type="text" class="form-control" id="query_example" name="query_example">
                        </div>
                        <div class="form-group">
                            <!--                        <label for="chapter_id_fk" class="control-label">Chapter Foreign Key:</label>-->
                            <input type="hidden" class="form-control" id="chapter_id_fk" name="chapter_id_fk" value="<?php echo $chapter_id?>">
                        </div>
                        <div class="form-group">
                            <!--                        <label for="page_id_fk" class="control-label">Page Foreign Key:</label>-->
                            <input type="hidden" class="form-control" id="page_id_fk" name="page_id_fk" value="<?php echo $page_id?>">
                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                            <input name = "add" type = "submit" id = "add" value = "Add">
                        </div>
                    </form>

This is how I am trying to save the form data and handle the POST request:

    <?php
    $con = mysqli_connect("localhost", "root", "", "lectureHub");
    if(isset($_POST['add'])) {
        $description = $_POST['description'];
        $external_link = $_POST['external_link'];
        $chapter_id_fk = $_POST['chapter_id_fk'];
        $page_id_fk = $_POST['page_id_fk'];
        $targetDir = "img/";    //path where image is saved -> sqlvali-master111/publicRoot/sqlvali/img/
        $fileName = basename($_FILES["query_structure"]["name"]);
        $targetFilePath = $targetDir . $fileName;
        $feedback = $_POST['feedback'];
        $likert = $_POST['likert'];
        $query_example = $_POST['query_example'];
        if(move_uploaded_file($_FILES["query_structure"]["tmp_name"], $targetFilePath)){
            $sql = "INSERT INTO pages (description, external_link, query_structure, chapter_id_fk, feedback, likert, query_example, page_id_fk) VALUES ('$description','$external_link','$fileName', '$chapter_id_fk', '$feedback', '$likert', '$query_example', '$page_id_fk')";
        }
            $retval = mysqli_query($con,$sql);
        if(! $retval ) {
            die('Could not enter data: ' . mysqli_error());
        }
        echo "<script> window.location = 'index.php?action=lec_hub/pages&page_id=$page_id_fk&chapter_id=$chapter_id_fk'; </script>";
    }
    ?>

I've looked through the internet and also StackOverflow but I cant't get my head around it Any help would be much appreciated!

0 Answers
Related