• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problems passing data in registration form

#1
[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

#2
[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 ;-)

#3
[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


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.