Welcome Guest, Not a member yet? Register   Sign In
the best practice for inserting data from form to mysql
#1

[eluser]Flemming[/eluser]
Hi gang,

I'm working on my first project with CI (and any framework for that matter) and loving it so far! I'm just following examples and tutorials at the moment and wondering about the best practice for inserting form data to mysql.

I have the following code: (see 'WHAT-GOES-HERE' for where I'm stuck!)

Code:
$fields['username'] = 'Username';
$fields['password'] = 'Password';
$fields['passconf'] = 'Password Confirmation';
$fields['email'] = 'Email Address';
        
$this->validation->set_fields($fields);
        
$this->validation->set_error_delimiters('<li>', '</li>');
                
if ($this->validation->run() == FALSE)
{
   $this->load->view('myform');
}
else
{
// insert into db!
   $data = array('username' => WHAT-GOES-HERE?????, 'password' => WHAT-GOES-HERE?????,'email' => WHAT-GOES-HERE?????, 'url' => WHAT-GOES-HERE?????);

$str = $this->db->insert_string('users', $data);

$this->load->view('formsuccess');
}

as you can see - I'm not sure how to get the validated values into my insert query. Any advice much appreciated!

thanks,

Flemming
#2

[eluser]frenzal[/eluser]
I'm assuming you don't want the actual query string, but you want it executed so use $this->db->insert();

as for your data I allways prepare my array with $this->input->post("field") combined with active record I'm quite sure that whatever gets inserted is pretty clean.

Code:
$data = array('username' => $this->input->post('username'), 'password' => $this->input->post('password'),'email' => $this->input->post('email'), 'url' =>  $this->input->post('url'));

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

[eluser]xwero[/eluser]
The validation library alters the data according to the rules in the $_POST global so you can use $_POST['fieldname'], input->post('fieldname') or validation->fieldname. All have the same value.
#4

[eluser]Flemming[/eluser]
excellent - many thanks for the fast and really helpful replies!
#5

[eluser]wiredesignz[/eluser]
Validation really needs a method to return the fields for these situations
Code:
$this->validation->get_fields();
#6

[eluser]xwero[/eluser]
I think validation should leave the fields to the input library. The validation->fieldname is confusing. The same with the set_fields method. You think it can set the default value of the inputs but it replaces the input name with a friendly name for the error messages and when a key exists in the POST global then it sets the POST value otherwise the value is set to an empty string.
#7

[eluser]Rick Jolly[/eluser]
[quote author="wiredesignz" date="1207664668"]Validation really needs a method to return the fields for these situations
Code:
$this->validation->get_fields();
[/quote]

Not best practice, but until that method is implemented you can access the private variable that stores the fields:
Code:
$this->validation->_fields;
#8

[eluser]Flemming[/eluser]
so this:

Quote:$data = array('username' => $this->validation->username, 'password' => $this->validation->password

would be the wrong way?




Theme © iAndrew 2016 - Forum software by © MyBB