Php 在mysql中插入双时间数据

Php 在mysql中插入双时间数据,php,mysql,codeigniter,Php,Mysql,Codeigniter,每次我尝试在数据库中插入数据时,都会插入两到三次,第二次不是数据,而是插入值0。 我也需要使用submit按钮插入数据。但它不起作用 你好 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); class Hello_con extends CI_Controller { /** * Index Page for this controller. * *

每次我尝试在数据库中插入数据时,都会插入两到三次,第二次不是数据,而是插入值0。 我也需要使用submit按钮插入数据。但它不起作用

你好

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Hello_con 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('heloo_view');
    }

    public function heloo()
    {

        $this->load->view('heloo_view');
        $user=$this->input->post('user');

        $user = array(
                'user_name'=>$user
                );

         $this->load->model("heloo_model");
         $this->heloo_model->insert_user($user);
         return $user;
         echo "hi, welcome:"."$user";
    }
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */

您需要验证用户,因为它将插入一行,即使您尚未提交表单

将heloo()替换为以下内容:

public function heloo()
{
    // User will be false if there wasn't any post data
    $user=$this->input->post('user');

    if($user !== false) {
        // The form was submitted
        $user = array(
            'user_name'=>$user
        );

        $this->load->model("heloo_model");
        $this->heloo_model->insert_user($user);

        echo "hi, welcome:"."$user";
    } else {
        // The form needs to be shown here
        $this->load->view('heloo_view');
    }
}

此代码将最初显示表单,直到用户提交表单并创建行。

您需要验证用户,因为它将插入行,即使您尚未提交表单

将heloo()替换为以下内容:

public function heloo()
{
    // User will be false if there wasn't any post data
    $user=$this->input->post('user');

    if($user !== false) {
        // The form was submitted
        $user = array(
            'user_name'=>$user
        );

        $this->load->model("heloo_model");
        $this->heloo_model->insert_user($user);

        echo "hi, welcome:"."$user";
    } else {
        // The form needs to be shown here
        $this->load->view('heloo_view');
    }
}
此代码将最初显示表单,直到用户提交表单并创建行。

您可以将其用作:

public function heloo()
{
    if(isset($this->input->post('user'))){
        $user = $this->input->post('user');

        $insert = array(
            'user_name'=>$user
        );

        $this->load->model("heloo_model");
        $this->heloo_model->insert_user($insert);
        echo "hi, welcome:".$user; 
    }

    $this->load->view('heloo_view');
}
代码中的问题:

  • 您在数据库中得到的是空行,因为您不需要验证数据,您需要验证从post获得的数据或不从post获得的数据
  • 您在使用
    return
    后回显“您好,欢迎”,因为
    return
    无法返回用户名
  • 始终在函数末尾而不是顶部加载视图文件
  • 您可以将其用作:

    public function heloo()
    {
        if(isset($this->input->post('user'))){
            $user = $this->input->post('user');
    
            $insert = array(
                'user_name'=>$user
            );
    
            $this->load->model("heloo_model");
            $this->heloo_model->insert_user($insert);
            echo "hi, welcome:".$user; 
        }
    
        $this->load->view('heloo_view');
    }
    
    代码中的问题:

  • 您在数据库中得到的是空行,因为您不需要验证数据,您需要验证从post获得的数据或不从post获得的数据
  • 您在使用
    return
    后回显“您好,欢迎”,因为
    return
    无法返回用户名
  • 始终在函数末尾而不是顶部加载视图文件

  • 您需要验证用户,因为它将插入行,即使您尚未提交表单。从“提交”按钮删除名称属性。您需要验证用户,因为它将插入行,即使您尚未提交表单。从“提交”按钮删除名称属性。