Kamalkrishna Sahu
Kamalkrishna Sahu

Reputation: 67

double time data is getting inserted in 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>

Upvotes: 1

Views: 196

Answers (2)

devpro
devpro

Reputation: 16117

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.

Upvotes: 2

Jim Wright
Jim Wright

Reputation: 6058

You need to validate the user as it will be inserting a row, even when you haven't submitted the form.

Replace your heloo() with this:

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

This code will show the form initially, until the user submits the form and creates the row.

Upvotes: 2

Related Questions