douqiaoru2583 2016-01-27 12:37
浏览 19
已采纳

双倍时间数据被插入mysql中

every time i try to insert data my get inserted twice or thrice in the database, the second time instead of data, value 0 is getting inserted. i too need to insert the data using submit button.but it's not working.

hello_con.php

<?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_model.php

<?php
class Heloo_model extends CI_Model {


    public function insert_user($user)
    {
        $this->load->database();
        $this->db->insert("userdetail",$user);

    }

}
?>

heloo_view.php

<html>
<head>
<title>heloo world</title>
</head>
<body>
<p>welcome to heloo world</p>

<form method="post">
Enter your name:
<input type="text" name="user" placeholder="enter your name">
<input type="submit" name="submit" value="sub">
</form>
</body>
</html>
  • 写回答

2条回答 默认 最新

  • donglang7236 2016-01-27 12:51
    关注

    You can use it as:

    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');
    }
    

    Issues in your code:

    1. You are getting empty rows in database because you are not validate the data, you need to validate the either getting from post or not.
    2. You are echoing "hi, welcome" after using return, it cant be return user name because of return.
    3. Always load your view file at the end of function not on top.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题