php - double time data is getting inserted in mysql -


every time try insert data inserted twice or thrice in database, second time instead of data, value 0 getting inserted. need insert 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 controller.      *      * maps following url      *      http://example.com/index.php/welcome      *  - or -        *      http://example.com/index.php/welcome/index      *  - or -      * since controller set default controller in       * config/routes.php, it's displayed @ http://example.com/      *      * other public methods not prefixed underscore      * map /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 heloo world</p>  <form method="post"> enter name: <input type="text" name="user" placeholder="enter name"> <input type="submit" name="submit" value="sub"> </form> </body> </html> 

you can use 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 code:

  1. you getting empty rows in database because not validate data, need validate either getting post or not.
  2. you echoing "hi, welcome" after using return, cant return user name because of return.
  3. always load view file @ end of function not on top.

Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -