check box value not saved properly in database
I write this from in Codeigniter as a form_view.php
.
<?php echo form_open('form'); ?>
<h5>Man</h5>
<input type="checkbox" name="options[]" value="m"/>
<h5>Lady</h5>
<input type="checkbox" name="options[]" value="f" />
<h5>Username</h5>
<input type="text" name="username" value="" size="50" />
<div><input type="submit" value="Submit" /></div>
</form>
And then I add this data to the database like this:
$data=array(
开发者_开发百科'type'=> $_POST['options[]'],
'name'=> $_POST['name']
);
$this->db->insert('user_data',$data);
The name field is ok. But the type field has nothing. I can not find the error. Please help me.
Assuming that you want multiple values to be stored with comma (as separator) between them
$data=array(
'type'=> implode(',', $_POST['options']),
'name'=> $_POST['name']
);
Remove []
from options.
$_POST['options']
will be an array. You may have to join the values to a string.
A few suggestions:
- If neither one is checked the value will not be submitted to the database, so you need a default.
- Consider using a radio type instead of checkbox. Except for some rare folks, I don't think too many people are going to be both man and lady.
- Change $_POST['options[]'] to $_POST['options']
Make
'type'=> $_POST['options[]']
as
'type'=> $_POST['options']
精彩评论