Welcome Guest, Not a member yet? Register   Sign In
insert into database
#1

[eluser]praveenarya[/eluser]
hi all,
i am new to CI,i worked over year on php4 and now i wanted to develop a shopping cart using framework and really felt good and liked this CI,not doing anything complicated but learning basics
my problem is i have a form i have validated it and now i want to insert those vaues into database but i am getting error
Code:
<?php

class Form extends Controller {
    
    function index()
    {
        $this->load->helper(array('form', 'url'));
        
        $this->load->library('form_validation');
            
        $this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[5]|max_length[12]|xss_clean');
        $this->form_validation->set_rules('password', 'Password', 'trim|required|matches[passconf]|md5');
        $this->form_validation->set_rules('passconf', 'Password Confirmation', 'trim|required');
        $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');
        $this->form_validation->set_error_delimiters('<div class="error">', '</div>');
            
        if ($this->form_validation->run() == FALSE)
        {
            $this->load->view('myform');
        }
        else
        {
            $this->load->view('formsuccess');
        }
        $this->load->database();
        $sql = "INSERT INTO exp_pre_email_addresses (name,email,password) VALUES('".$this->db->escape_str($_POST['username'])."','".$this->db->escape_str($_POST['email'])."','".$this->db->escape_str($_POST['password'])."')";
    }
}
?&gt;
i am getting errors can anyone help me
thanks a lot
#2

[eluser]Fatih[/eluser]
Hello praveenarya, welcome to CodeIgniter Forum pages.

Your code is not convenient for MVC idea. You should separate your database works to Model file. Please read these guide pages carefully.
#3

[eluser]praveenarya[/eluser]
thank you very much for your reply,
i have read this userguide in few hours i think i need to practice those points more 8-/
thank you for a fast reply
#4

[eluser]ggoforth[/eluser]
[quote author="praveenarya" date="1248171025"]hi all,
i am new to CI,i worked over year on php4 and now i wanted to develop a shopping cart using framework and really felt good and liked this CI,not doing anything complicated but learning basics
my problem is i have a form i have validated it and now i want to insert those vaues into database but i am getting error
Code:
&lt;?php

class Form extends Controller {
    
    function index()
    {
        $this->load->helper(array('form', 'url'));
        
        $this->load->library('form_validation');
            
        $this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[5]|max_length[12]|xss_clean');
        $this->form_validation->set_rules('password', 'Password', 'trim|required|matches[passconf]|md5');
        $this->form_validation->set_rules('passconf', 'Password Confirmation', 'trim|required');
        $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');
        $this->form_validation->set_error_delimiters('<div class="error">', '</div>');
            
        if ($this->form_validation->run() == FALSE)
        {
            $this->load->view('myform');
        }
        else
        {
            $this->load->view('formsuccess');
        }
        $this->load->database();
        $sql = "INSERT INTO exp_pre_email_addresses (name,email,password) VALUES('".$this->db->escape_str($_POST['username'])."','".$this->db->escape_str($_POST['email'])."','".$this->db->escape_str($_POST['password'])."')";
    }
}
?&gt;
i am getting errors can anyone help me
thanks a lot[/quote]

Ok, where to start. First, the way you have your database insert code , it will happen regardless of if the validation fails. Move your insert code into the else of your if statement. Also, make use of the set_value() instead of using the $_POST array. In your form validations your using functions like trim() that will trim the input and return it for use. You can take advantage of these by using set_value('username') instead of $_POST['username']. Also, I can't see in your code where you ever actually run the insert to the database. Maybe you left that out? Lastly, if you make use of the Active Record (see user guide) to build your queries, it will take care of escaping and other things that make your queries safer.

Greg
#5

[eluser]praveenarya[/eluser]
hi i have done as there in the userguide i created a Blog_insert file in models page
Code:
&lt;?php class Blog_insert extends Model {

    var $title   = '';
    var $content = '';
    var $date    = '';

    function Blogmodel()
    {
        // Call the Model constructor
        parent::Model();
    }
    
    function get_last_ten_entries()
    {
        $query = $this->db->get('entries', 10);
        return $query->result();
    }

    function insert_entry()
    {
        $this->name->post('username'); // please read the below note
        $this->email->post'email'];
        $this->password->$_POST['password'];
        

        $this->db->insert('exp_pre_email_addresses', $this);
    }

    function update_entry()
    {
        $this->name->post('username'); // please read the below note
        $this->email->post'email'];
        $this->password->$_POST['password'];

        $this->db->update('entries', $this, array('id' => $_POST['id']));
    }

}
?&gt;
and in controllers folder my form code is like this
Code:
&lt;?php

class Form extends Controller {
    
    function index()
    {
        $this->load->helper(array('form', 'url'));
        
        $this->load->library('form_validation');
            
        $this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[5]|max_length[12]|xss_clean');
        $this->form_validation->set_rules('password', 'Password', 'trim|required|matches[passconf]|md5');
        $this->form_validation->set_rules('passconf', 'Password Confirmation', 'trim|required');
        $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');
        $this->form_validation->set_error_delimiters('<div class="error">', '</div>');
            
        if ($this->form_validation->run() == FALSE)
        {
            $this->load->view('myform');
        }
        else
        {
            $this->load->view('formsuccess');
        }
    }
    
}



class Blog_controller extends Controller {

    function blog()
    {
        $this->load->model('Blog_insert');

        $data['query'] = $this->Blog->insert_entry();

        
    }
}
?&gt;

Am i correct please help me
thank you
#6

[eluser]praveenarya[/eluser]
please help me with the code iam straching my brain for 1 hour where i went wrong
Code:
&lt;?php

class Form extends Controller {
    
    function index()
    {
        $this->load->helper(array('form', 'url'));
        
        $this->load->library('form_validation');
            
        $this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[5]|max_length[12]|xss_clean');
        $this->form_validation->set_rules('password', 'Password', 'trim|required|matches[passconf]|md5');
        $this->form_validation->set_rules('passconf', 'Password Confirmation', 'trim|required');
        $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');
        $this->form_validation->set_error_delimiters('<div class="error">', '</div>');
            
        if ($this->form_validation->run() == FALSE)
        {
            $this->load->view('myform');
        }
        else
        {
        
        $this->load->model('Blog_insert');
        $data['query'] = $this->Blog->insert_entry();
            $this->load->view('formsuccess');
            
            
        
            
        }
    }
    
}

?&gt;
i am getting error

can any one help me out here
#7

[eluser]ggoforth[/eluser]
[quote author="praveenarya" date="1248181415"]please help me with the code iam straching my brain for 1 hour where i went wrong
Code:
&lt;?php

class Form extends Controller {
    
    function index()
    {
        $this->load->helper(array('form', 'url'));
        
        $this->load->library('form_validation');
            
        $this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[5]|max_length[12]|xss_clean');
        $this->form_validation->set_rules('password', 'Password', 'trim|required|matches[passconf]|md5');
        $this->form_validation->set_rules('passconf', 'Password Confirmation', 'trim|required');
        $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');
        $this->form_validation->set_error_delimiters('<div class="error">', '</div>');
            
        if ($this->form_validation->run() == FALSE)
        {
            $this->load->view('myform');
        }
        else
        {
        
        $this->load->model('Blog_insert');
        $data['query'] = $this->Blog->insert_entry();
            $this->load->view('formsuccess');
            
            
        
            
        }
    }
    
}

?&gt;
i am getting error

can any one help me out here[/quote]

You've still got a few problems here. First off, your not dealing with to much code here, so take things line by line and your problems will start to show themselvs. In your blog model insert code you are referencing the $_POST array like this:

Code:
$this->name->post('username'); // <- this is wrong

If you want to correctly call the field values you would use:

Code:
$this->input->post('field_name');
or
Code:
set_value('field_name'); // <- since your using the validation class

Also, I would personally pass the variables into the model like this:

Code:
$this->Blog->insert_entry(set_value('username'),set_value('email'));

Then in your blog model, rewrite your insert function like this:

Code:
function insert_entry($username, $email)
{
    $arr = array('username'=>$username,'email'=>$email);
    $this->db->insert('exp_pre_email_addresses',$arr);
}

Doing it this way is more flexible as you will be able to use this model again if you need to insert information in another part of the site. give this a shot, take things one line at a time, compare what you have written to the userguide, and I think you'll have an easy time figuring out where your problems are.

Greg
#8

[eluser]praveenarya[/eluser]
thank you very much for your precious advice my problem is solved




Theme © iAndrew 2016 - Forum software by © MyBB