C
C
coldunox2018-06-14 06:27:11
PHP
coldunox, 2018-06-14 06:27:11

Why doesn't it load values ​​into the database from the modal window?

Modal window - View

<div class="modal" id="modal-2">
    <div class="modal-dialog">
       <div class="modal-content">
         <form class="form-horizontal"  name="taskInfo" data-ng-submit="addQuestion()">
           <div class="modal-header">
               <h4 class="modal-title">Добавление вопроса</h4>
           </div>
       
         <div class="modal-body">
                        <div class="form-group">
                            <label for="category" class="col-sm-4">Номер вопроса</label>
                            <div class="col-sm-6">
                            <input type="text" data-ng-model="id" id="id" class="form-control">
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="category" class="col-sm-4">Текст вопроса</label>
                            <div class="col-sm-6">
                            <input type="text" data-ng-model="text" id="text" class="form-control">
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="content" class="col-sm-4">Вариант ответа 1</label>
                            <div class="col-sm-6">
                            <input type="text" data-ng-model="var_a" id="var_a" class="form-control">
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="category" class="col-sm-4">Вариант ответа 2</label>
                            <div class="col-sm-6">
                            <input type="text" data-ng-model="var_b" id="var_b" class="form-control">
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="content" class="col-sm-4">Вариант ответа 3</label>
                            <div class="col-sm-6">
                            <input type="text" data-ng-model="var_c" id="var_c" class="form-control">
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="content" class="col-sm-4">Вариант ответа 4</label>
                            <div class="col-sm-6">
                            <input type="text" data-ng-model="var_d" id="var_d" class="form-control">
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="content" class="col-sm-4">Верный ответ</label>
                            <div class="col-sm-6">
                            <input type="text" data-ng-model="corr_id" id="corr_id" class="form-control">
                            </div>
                        </div>
          </div>
                    <div class="modal-footer">
                        <button name="submit" type="submit" class="btn btn-success">Добавить</button>
                    </div>
   
</form>
        </div>
      </div>
</div>

Angular
$scope.addQuestion = function() {
        $scope.id = angular.element("#id").val();
        $scope.text = angular.element("#text").val();
        $scope.var_a = angular.element("#var_a").val();
        $scope.var_b = angular.element("#var_b").val();
        $scope.var_c = angular.element("#var_c").val();
        $scope.var_d = angular.element("#var_d").val();
        $scope.corr_id = angular.element("#corr_id").val();

        $http({
            method: "POST",
            url: "localhost/cabinet/tasks/addQuestion",
            data: $.param({ id: $scope.id,  
                            text: $scope.text,
                            var_a: $scope.var_a,  
                            var_b: $scope.var_b,
                            var_c: $scope.var_c,  
                            var_d: $scope.var_d,
                            corr_id: $scope.corr_id}),
            
            headers: {'Content-Type': 'application/x-www-form-urlencoded'}
        }).then(function(result){
            			if(result.data.success) {
                        swal({
                    title: "Отлично!",
                    text: "Вопрос добавлен",
                    icon: "success"},
                        function(){ 
                            location.reload();
                        }

                ); 
            }
        })

    }

Controller
public function addQuestion() {
        if(!$_SESSION['user']) {
            header("Location: /");
            return;
        }

        if(empty($_POST) || !isset($_POST['id'])  || trim($_POST['text']) == '' || trim($_POST['var_a']) == '' ||  trim($_POST['var_b']) == '' || trim($_POST['var_c']) == '' || trim($_POST['var_d']) == '' || !isset($_POST['corr_id'])) 
        {
            echo json_encode(array("success" => false));
        } else {

            $id = $_POST['id'];
            $text = $_POST['text'];
            $var_a = $_POST['var_a'];
            $var_b = $_POST['var_b'];
            $var_c = $_POST['var_c'];
            $var_d = $_POST['var_d'];
            $corr_id = $_POST['corr_id'];
            
           

            if($this->model->addQuestion($id, $text, $var_a, $var_b, $var_c, $var_d, $corr_id)) {
                echo json_encode(array("success" => true));
            } else {
                echo json_encode(array("success" => false));
            }
        }
    }

Model
public function addQuestion($id, $text, $var_a, $var_b, $var_c, $var_d, $corr_id) {
        $sql = "INSERT INTO questions (id_tests, text, variant_a, variant_b	, variant_c, variant_d	, id_answers)
                            VALUES(:id_tests, :text, :variant_a, :variant_b, :variant_c, :variant_d, :id_answers)
                ";
        $stmt = $this->db->prepare($sql);
        print_r($stmt);
        $stmt->bindValue(":id_tests", $id, PDO::PARAM_INT);
        $stmt->bindValue(":text", $text, PDO::PARAM_STR);
        $stmt->bindValue(":variant_a", $var_a, PDO::PARAM_STR);
        $stmt->bindValue(":variant_b", $var_b, PDO::PARAM_STR);
        $stmt->bindValue(":variant_c", $var_c, PDO::PARAM_STR);
        $stmt->bindValue(":variant_d", $var_d, PDO::PARAM_STR);
         $stmt->bindValue(":id_answers", $corr_id, PDO::PARAM_INT);
        $stmt->execute();
        return true;
    }

Data
5b21dfd53ed83829320450.png
entry Record in the database
5b21dff8b0704478714318.png
We managed to add only 1 record
Why is not added yet? I don’t understand what kind of glitch
The console is empty

Answer the question

In order to leave comments, you need to log in

2 answer(s)
C
coldunox, 2018-06-14
@coldunox

It's simple - I had UNIQUE
index types. Therefore, the form was not sent
. Thank you all!

A
abdujabbor1987, 2018-06-14
@abdujabbor1987

try to make an insert in php through try{}catch{} and catch the exception, and there you will already see what error

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question