CodeIgniter Forums
Problems passing data in registration form - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Problems passing data in registration form (/showthread.php?tid=53279)



Problems passing data in registration form - El Forum - 07-17-2012

[eluser]Stromgren[/eluser]
Hey CI users Smile

Im currently working on a form which causes me some problems..
Problably me having some troubling figuring out whats arrays and objects in this..

Anyways.. First of all i have a table filled with interests that people can check off in the form.
The table has two columns, ID and NAME.

So i fetch my interests and in my view i make my checkboxes like this

Code:
<?php foreach($interests->result() as $interest): ?>
     <?php echo form_checkbox('interest[]', $interest->id, FALSE); echo $interest->name; ?>
<?php endforeach; ?>

In the controller i recieve the data like this:

Code:
$interests = $this->input->post('interest');

And then i send it, along with some other data, to my model:

Code:
$this->Registration_model->register_user($username, $email, $password, $country, $city, $interests, $setup, $job, $bio, $activation_code);

Now in the model i recieve a user_id from the query which writes all the data except the interests. Now i want to write to another table, users_interests, a row for each interest checked. This row should contain a user_id and a interest_id.

Code:
...
$user_id = mysql_insert_id();
  
foreach($interests as $interest)
{
$int_id = $interest->id;
$query = "INSERT INTO users_interests (user_id, int_id) VALUES (?, ?)";
$this->db->query($query, array($user_id, $int_id));
}

When i run this i get this error:

Quote:Error Number: 1048

Column 'int_id' cannot be null

INSERT INTO users_interests (user_id, int_id) VALUES (29, NULL)

So the int_id is empty. As i said, im problably mixing something up when using arrays and objects, but i cant seem to fix this..

Any help is apprecieted

Thanks in advance Smile


Problems passing data in registration form - El Forum - 07-17-2012

[eluser]Oscar Dias[/eluser]
I think when you do the foreach inside the model the $interest variable is the ID. So in your model it would be like:
Code:
...
$user_id = mysql_insert_id();
  
foreach($interests as $interest)
{
$query = "INSERT INTO users_interests (user_id, int_id) VALUES (?, ?)";
$this->db->query($query, array($user_id, $interest));
}

This should do the trick ;-)


Problems passing data in registration form - El Forum - 07-17-2012

[eluser]Stromgren[/eluser]
[quote author="Oscar Dias" date="1342557335"]I think when you do the foreach inside the model the $interest variable is the ID. So in your model it would be like:
Code:
...
$user_id = mysql_insert_id();
  
foreach($interests as $interest)
{
$query = "INSERT INTO users_interests (user_id, int_id) VALUES (?, ?)";
$this->db->query($query, array($user_id, $interest));
}

This should do the trick ;-)[/quote]

Yeeeaah !!! You're like a genius Big Grin Thank you very much.. I even understood why Wink