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条)

报告相同问题?

悬赏问题

  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗