刷新页面空记录自动插入数据库

时间:2015-09-23 09:59:51

标签: php mysql codeigniter

我正在使用codeigniter MVC模式。每当我刷新页面时,Null记录会自动插入数据库。在数据库中,我将all列设置为null,除了主键。 请帮帮我。

查看页面代码(index.php)

 <div class="title">
        <p><strong>ADD USER</strong></p>
        </div>
    <?php echo form_open("Welcome/index"); ?>
        <input type="text" name="userId" placeholder="USER ID" style="width:500px">&nbsp;
        <input type="date" name="date" style="width:500px">


        <input type="text" name="firtsname" placeholder="FIRST NAME" style="width:330px">&nbsp;
        <input type="text" name="middlename" placeholder="MIDDLE NAME" style="width:330px">&nbsp;
        <input type="text" name="lastname" placeholder="LAST NAME" style="width:330px">&nbsp;

        <input type="text" name="mobileno" placeholder="ENTER MOBILE NO." style="width:500px">&nbsp;
        <input type="text" name="landline" placeholder="LANDLINE No." style="width:500px">
        <textarea name="address" placeholder="ENTER ADDRESS"></textarea>

        <input type="text" name="city" placeholder="CITY" style="width:500px">&nbsp;
        <input type="text" name="locality" placeholder="LOCALITY" style="width:500px">
        <input type="text" name="email" placeholder="ENTER EMAIL" style="width:1010px"></br>

      <input type="submit" class="submit" name="SUBMIT">
    <?php echo form_close(); ?>
</div>

模型页面代码(model_add_user.php)

<?php

defined('BASEPATH') OR exit('No direct script access allowed');

    class Model_add_user extends CI_Model {

        public function __construct() {
            parent:: __construct();
            $this->load->database();
        }

        public function add_user() {


            $data = array(
                'userId' => $this->input->post('userId'),
                'date' => $this->input->post('date'),
                'firtsname' => $this->input->post('firtsname'),
                'middlename' => $this->input->post('middlename'),
                'lastname' => $this->input->post('lastname'),
                'mobileno' => $this->input->post('mobileno'),
                'landline' => $this->input->post('landline'),
                'address' => $this->input->post('address'),
                'city' => $this->input->post('city'),
                'locality' => $this->input->post('locality'),
                'email' => $this->input->post('email'),
            );

            $this->db->insert('add_user', $data);
        }

    }
    ?>

控制器页面代码(Welcome.php)

            <?php
            defined('BASEPATH') OR exit('No direct script access allowed');

            class Welcome extends CI_Controller {

                /**
                 * Index Page for this controller.
                 *
                 * Maps to the following URL
                 *      http://example.com/index.php/welcome
                 *  - or -
                 *      http://example.com/index.php/welcome/index
                 *  - or -
                 * Since this controller is set as the default controller in
                 * config/routes.php, it's displayed at http://example.com/
                 *
                 * So any other public methods not prefixed with an underscore will
                 * map to /index.php/welcome/<method_name>
                 * @see http://codeigniter.com/user_guide/general/urls.html
                 */
                public function index()
                {
                    //$this->load->view('welcome_message');
                    $this->load->view('index');
                    $this->load->model('model_add_user');
                    $this->model_add_user->add_user();

                }


            }


            ?>

3 个答案:

答案 0 :(得分:1)

在您的控制器中进行以下更改:

public function index() {
    if ($this->input->post()) {
        $this->load->model('model_add_user');
        $this->model_add_user->add_user();
    }
    $this->load->view('index');
}

答案 1 :(得分:0)

控制器名称的路径直接命中控制器的索引功能。在您的代码中,您加载视图并调用fieldlen=5; function.So每次刷新模型调用页面并将null插入数据库

您需要为插入数据创建单独的函数

add_user

将表单操作更改为

 public function index()
{
//$this->load->view('welcome_message');
$this->load->view('index');
}
function add_user()
{
$this->load->model('model_add_user');
$this->model_add_user->add_user();
}

答案 2 :(得分:0)

成功插入后..只需将其重定向到...功能/控制器,它将刷新页面并阻止表单再次提交。

 public function index() {
    //$this->load->view('welcome_message');
    $this->form_validation->set_rules('field', 'Field', 'trim|required');
    if ($this->form_validation->run()) {
        $this->load->model('model_add_user');
        $this->model_add_user->add_user();
        redirect('welcome')
    } else {
        $this->load->view('index');
    }
}
相关问题