Welcome Guest, Not a member yet? Register   Sign In
Simple Inserting Form Data into DB
#1

[eluser]alvaroeesti[/eluser]
UPDATE: problem solved completely thanks to 'InsiteFX'. Thank you
============

Hello

I am trying to do a very simple thing. One page with a Form to enter username and pwd. It should be inserted in the DB.

Here is the CONTROLLER


Code:
<?php

class Inserting_controller extends CI_Controller {

  public function __construct()
       {
            parent::__construct();
            $this->load->model('inserting_model');
            
       }


  public function index ()
  {
    
  $this->load->view('inserting_view');
  }

  public function insert()
  {
    
   extract($_POST); # this hopefully should get the posted data on the form ..
          $this->Inserting_model->insertdata($username, $password);#this should forward them to the Model
    
    
  }


}
?>

Here is the MODEL

Code:
<?php


class Inserting_model extends CI_Model{


    function __construct()
    {
        // Call the Model constructor
        parent::__construct();
        $this->load->database();
    }
        
    
    public function insertdata($username, $password)
    {
        
        $hashed_pwd = sha1($password);

        $data = array(
        'username' => 'Username',
        'password' => '$hashed_pwd'
        );

         $this->db->insert('users', $data);

      
        
    }



}

?>
And here is the View. But nothing gets inserted. The field for the Pwd has been set to Binary 20 (UNHEX function)


Code:
<?php echo form_open(base_url() . 'index.php/inserting_controller/inserting_view'); ?>
      
      <ul>
        
            <li>


            
            <label>Username</label>      
                
            <div>&lt;?php echo form_input(array('id' => 'username', 'name' => 'username')); ?&gt;</div>
          
            </li>
        
        
        
            <li>
            
            <label>Password</label>      
                
            <div>&lt;?php echo form_password(array('id' => 'password', 'name' => 'password')); ?&gt;</div>  
              
            </li>
            <li>&lt;?php echo validation_errors();?&gt;</li>
            
            <li>&lt;?php echo form_submit(array('name' =>'submit'),'Insert');?&gt; </li>
        
      </ul>
      
      &lt;?php echo form_close(); ?&gt;


#2

[eluser]InsiteFX[/eluser]
Code:
// Controller
public function insert()
{
    $data = array(
        'username' => $this->input->post('username', TRUE),
        'password' => sha1($this->input->post('password', TRUE)
    );

    $this->Inserting_model->insertdata($data); // this should forward them to the Model
}

// MODEL
public function insertdata($data)
{
    $this->db->insert('users', $data);
}

// form_open
&lt;?php echo form_open('inserting_controller/insert'); ?&gt;
#3

[eluser]alvaroeesti[/eluser]
Thank you very much. I am currently implementing the changes and reflecting upon them. When I finish, I ll let you know but definitively already it is wonderful help because I can see the contrasts to which I had been trying. Very good the insight of correcting also the "action" of the Form, it was not going anywhere before.
#4

[eluser]InsiteFX[/eluser]
You can use these for debuging your code.
Code:
var_dump($array);

print_r($array);

Also read up on the CodeIgniter Profiler, it will give you all kinds of information.
#5

[eluser]alvaroeesti[/eluser]

Yes, I am devouring information on CI. Today about 12 hours so far. I am still scratching my head a bit as I haven't succeeded yet in inserting the data. Upon clicking submit, well it takes me to a "Object not found!
The requested URL was not found on this server" I am not worried about that, because that is independent from inserting the data. but I ask myself, where would the insert form take me to? it looks to a page called 'insert' ? even though 'insert' is actually the second function of the controller. Yes, error messages are essential: where do I place those debugging aids you included?
#6

[eluser]InsiteFX[/eluser]
Code:
// Controller
public function insert()
{
    $data = array(
        'username' => $this->input->post('username', TRUE),
        'password' => sha1($this->input->post('password', TRUE)
    );

    // See if we are getting input post data.
    // If this is empty then your form_open url is wrong.
    var_dump($data);
    exit;

    $this->Inserting_model->insertdata($data); // this should forward them to the Model
}

// MODEL
public function insertdata($data)
{
    $this->db->insert('users', $data);
}

Also you should be using the form_validation library
#7

[eluser]alvaroeesti[/eluser]


Yes, dont worry about the validation because I do have that for the real login for people. This page that you are seeing is my "backoffice". There will not be automatic registration, thus, I will internally create the username and the pwd with this page I am trying now. Then, for their own login, I do have all the sanitization routines and functions, xss clean, prepared statements, etc.
#8

[eluser]alvaroeesti[/eluser]

SOLVED:

blushing...I did not realize that there was an 'exit' command there, thus, the program was going to exit right after that. Deleted the 'exit' and it worked straight away...how embarrassing...




Theme © iAndrew 2016 - Forum software by © MyBB