首页 > 解决方案 > 使用 SQL INSERT 的提交 codeignitor 表单没有响应

问题描述

我对使用 codeignitor 还很陌生,而且我已经被这个问题困扰了一段时间。我似乎无法找到我正在寻找的答案,或者我没有经验找到它。无论如何 - 我正在尝试创建一个将数据插入我的 sql 数据库的表单,表单没有提供任何类型的错误,但数据库中没有任何反应,我错过了什么?

检查错误代码甚至没有响应:

echo '<pre>';print_r($data); die;

我的控制器(Blog_add.php):

<?php

class blog_add extends CI_Controller {

function __construct() {
parent::__construct();

}
function index() {

    $this->load->model('blog_add_model');

    $this->load->library('form_validation');

    $this->form_validation->set_error_delimiters('<div class="error">', '</div>');

    $this->form_validation->set_rules('blogpostbildPathInput', 'Bild', 'required|min_length[1]|max_length[100]');

    $this->form_validation->set_rules('blogpostdateDayInput', 'Dag', 'required|regex_match[/^[0-9]{10}$/]');

    $this->form_validation->set_rules('blogpostdateMonthInput', 'M&aring;nad', 'required|min_length[1]|max_length[20]');

    $this->form_validation->set_rules('blogpostdateArInput', '&Aring;r', 'required|regex_match[/^[0-9]{10}$/]');

    $this->form_validation->set_rules('blogpostcategoryInput', 'Kategori', 'required|min_length[1]|max_length[35]');

    $this->form_validation->set_rules('blogpostRubrikInput', 'Rubrik', 'required|min_length[10]|max_length[100]');

    $this->form_validation->set_rules('blogpostbeskrivningInput', 'Beskrivning', 'required|min_length[10]|max_length[200]');

    $this->form_validation->set_rules('blogpoststyckeEttInput', 'Blogparagraph 1', 'required|min_length[10]|max_length[400]');

    $this->form_validation->set_rules('blogpoststyckeTvaInput', 'Blogparagraph 2', 'required|min_length[10]|max_length[400]');

    if ($this->form_validation->run() == FALSE) {
        $this->load->view('auth/blog_create');
    } else {

        $data = array(
            'blogpostbildPath' => $this->input->post('blogpostbildPathInput'),
            'blogpostdateDay' => $this->input->post('blogpostdateDayInput'),
            'blogpostdateMonth' => $this->input->post('blogpostdateMonthInput'),
            'blogpostdateAr' => $this->input->post('blogpostdateArInput'),
            'blogpostcategory' => $this->input->post('blogpostcategoryInput'),
            'blogpostRubrik' => $this->input->post('blogpostRubrikInput'),
            'blogpostbeskrivning' => $this->input->post('blogpostbeskrivningInput'),
            'blogpoststyckeEtt' => $this->input->post('blogpoststyckeEttInput'),
            'blogpoststyckeTva' => $this->input->post('blogpoststyckeTvaInput'),
        );

    // echo '<pre>';print_r($data); die;
    // is not working here

        $this->insert_model->form_insert($data); 
        $data['message'] = 'Data Inserted Successfully';
        $this->load->view('auth/blog_create', $data);
        }
    }  
}

?>

我的模型(Blog_add_model.php):

<?php
class blog_add_model extends CI_Model{
function __construct() {
parent::__construct();
}
function form_insert($data){
$pagaendeDb = $this->load->database('blog');
$this->$pagaendeDb->insert('projekt', $data);
}
}
?>

我的观点:(blog_create.php):

<?php
include('phpTemplates/head.php');
include('phpTemplates/header.php');
?>

<div class="container" style="margin-top: 20px">
    <h2>Lägg till nytt blogginlägg</h2>
    <div class="row" style="background: #fbf9ff; padding-top: 22px; margin-bottom: 60px; box-shadow: 0px 4px 4px 2px rgba(0,0,0,0.1)">

        <div class="col-lg-12">

            <?php echo form_open('Blog_add'); ?>

            <?php if (isset($message)) { ?>
            <CENTER>
                <h3 style="color:green;">Data inserted successfully</h3>
            </CENTER>
            <?php } ?>

            <div class="col-20">
                <div class="form-group" style="margin-left: 14px">
                    <?php echo form_input(array('id' => 'blogpostbildPath', 'name' => 'blogpostbildPathInput', 'type' => 'file')); ?>
                    <?php

                        $dayOptions = array('Dag');

                        for ($i = 1; $i < 32; $i++) {
                            $inputValue  = (string) $i;
                            $dayOptions[$i] = $inputValue;
                            $dayOptions[$i][0] = $inputValue;
                        }

                        echo form_dropdown('blogpostdateDayInput', $dayOptions, '');
                    ?>

                    <?php 

                        $monthOptions = array(
                            'Månad' => 'Månad',
                            'Jan'  => 'Januari',
                            'Feb'    => 'Februari',
                            'Mar'   => 'Mars',
                            'Apr' => 'April',
                            'Maj'  => 'Maj',
                            'Jun'    => 'Juni',
                            'Jul'   => 'Juli',
                            'Aug' => 'Augusti',
                            'Sep'  => 'September',
                            'Okt'    => 'Oktober',
                            'Nov'   => 'November',
                            'Dec' => 'December'
                        );

                        echo form_dropdown('blogpostdateMonthInput', $monthOptions, '');
                    ?>

                    <?php 

                        $yearOptions = array(
                            'År' => 'År',
                            '2016'  => '2016',
                            '2017'    => '2017',
                            '2018'   => '2018',
                            '2019' => '2019',
                            '2020' => '2020',
                            '2021' => '2021'
                        );

                        echo form_dropdown('blogpostdateArInput', $yearOptions, '');
                    ?>


                    <?php 

                        $statusOptions = array(
                            'Status' => 'Status',
                            'Pågående'  => 'Pågående',
                            'Avslutade'    => 'Avslutade',
                            'Övrigt' => 'Övrigt'
                        );

                        echo form_dropdown('blogpostStatusInput', $statusOptions, '');
                    ?>


                    <?php 

                        $categoryOptions = array(
                            'Kategori' => 'Kategori',
                            'Nybyggt'  => 'Nybyggt',
                            'Stambyte'    => 'Stambyte',
                            'Renovering'   => 'Renovering',
                            'Övrigt' => 'Övrigt',
                        );

                        echo form_dropdown('blogpostcategoryInput', $categoryOptions, '');
                    ?>


                </div>
            </div>

            <div class="col-6">

                <?php echo form_error('blogpostRubrik'); ?><br />
                <?php echo form_input(array('id' => 'blogpostRubrik', 'name' => 'blogpostRubrikInput', 'value' => '', 'placeholder' => 'Rubrik på inlägg', 'type' => 'text', 'class' => 'form-control w-100')); ?><br />

            </div>

            <div class="col-6">

                <?php echo form_error('blogpostbeskrivning'); ?><br />
                <?php echo form_textarea(array('id' => 'blogpostbeskrivning', 'name' => 'blogpostbeskrivningInput', 'value' => '', 'placeholder' => 'Beskrivning av inlägg', 'cols' => '30', 'rows' => '3', 'class' => 'form-control w-100', 'style' => 'height: 128px;')); ?><br />

            </div>

            <div class="col-10">

                <?php echo form_error('blogpoststyckeEtt'); ?><br />
                <?php echo form_textarea(array('id' => 'blogpoststyckeEtt', 'name' => 'blogpoststyckeEttInput', 'value' => '', 'placeholder' => 'Blogginlägg stycke 1', 'cols' => '30', 'rows' => '9', 'class' => 'form-control w-100', 'style' => 'height: 252px;')); ?><br />

            </div>

            <div class="col-10">

                <?php echo form_error('blogpoststyckeTva'); ?><br />
                <?php echo form_textarea(array('id' => 'blogpoststyckeTva', 'name' => 'blogpoststyckeTvaInput', 'value' => '', 'placeholder' => 'Blogginlägg stycke 2', 'class' => 'form-control w-100', 'cols' => '30', 'rows' => '9', 'style' => 'height: 252px;')); ?><br />

            </div>

            <div class="col-12">

                <?php echo form_submit(array('id' => 'submit', 'value' => 'Lägg till', 'class' => 'button button-contactForm btn_1')); ?>
            </div>


            <?php echo form_close(); ?><br />

        </div>
    </div>
</div>


<?php
include('phpBodyTemplates/importJs.php');
include('phpTemplates/footer.php');
?>

标签: phpcodeigniter

解决方案


控制器上加载的模型不同。在这里你加载$this->load->model('blog_add_model');。让你$config['log_threshold'] = 1;远离config.php,所以它在开发过程中出错logs或保留define('ENVIRONMENT', 'development');index.php

代替

$this->insert_model->form_insert($data); 

 $this->blog_add_model->form_insert($data); 

在控制器中


推荐阅读