954,561 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Insert Selected data In MySQL

Hi,
I have a long list of options( 60 option) where user can selecte none or all of them.
the code is like that

<selecte name="list">
<option value="1">option</option>
...
</selecte>

and I have to display it again as a list ( ), my question is what the best format to store the selected options in DATABASE?

m.cliter
Newbie Poster
19 posts since Feb 2008
Reputation Points: 10
Solved Threads: 0
 

What do you mean by best format to store the selected option ? If the value is an integer, have an integer field. If its an alphanumeric, have a varchar field. Is this what you are talking about ?

nav33n
Purple hazed!
Moderator
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
 

thank you for your reply.
the otpions are written in english alphabet. for exemple, if the user selecte 20 option how can I insert all this options in database. do I need one column or do I need 60 columns?!!
my question is about database design, not about columns type.

m.cliter
Newbie Poster
19 posts since Feb 2008
Reputation Points: 10
Solved Threads: 0
 

since most people will probably never pick more than a few, it would save space to only have one column.

try this:

<?php
//multi-list boxes post an array of results
//get this array into a single comma-seperated-value string
foreach($_POST['select'] as $key=>$val)
{
	//add item as comma-seperated-value
	$csv .= $val .',';
}

//trim trailing comma
$csv = rtrim($csv,',');

//INSERT $csv INTO A SINGLE FIELD IN YOUR DATABASE ROW HERE!!!
//you can do the sql HERE...

//end part one!

//later you can sort your comma-seperated-value that you retrieve from the database back into individual elements like so...

//you can do the sql HERE...

//store all individual items in an array
$arrayOfItems = explode(',',$csv);

//build each of your items in a list
foreach($arrayOfItems as $key=>$val)
{
	$output .= '<li>' . $val . '</li>';	
}

//display output
echo $output;


?>
johnsquibb
Junior Poster in Training
84 posts since Nov 2007
Reputation Points: 14
Solved Threads: 14
 

Thanks for you help, I really appreciated

m.cliter
Newbie Poster
19 posts since Feb 2008
Reputation Points: 10
Solved Threads: 0
 

hi
What if I changed the select tag by input tag with checkbox as a type?
I know I can get each one using $_POST[] and put the comma ',' between sentances. But the problem is when the user want to edit his choices, I have to mark his previous choices.

<input type="checkbox" name="name1" /> sentence 1
<input type="checkbox" name="name2" /> sentence 2
<input type="checkbox" name="name3" checked="<strong>checked</strong>"/> sentence 3
<input type="checkbox" name="name4" /> sentence 4

any hint?
PS: I have too many options.

m.cliter
Newbie Poster
19 posts since Feb 2008
Reputation Points: 10
Solved Threads: 0
 

Working off the code sample in my earlier post...
Here would be one way to ensure the checkboxes were checked if they had chosen them previously...

//array to hold checkbox options
$checkBoxOptions = array('sentence 1' , 'sentence 2' , 'sentence 3' , 'sentence 4');
 
//create checkbox output
$cbOut = '';

foreach($checkBoxOptions as $val)
{
	//test to see if stored choices match a checkbox value.
	if(in_array($val , $arrayOfItems))
	{
		//stored choice was found, mark this box
		$cbOut .= '<input type="checkbox" name="'.$val.'" checked="checked" value="'.$val.'"/><label>' . $val . '</label><br/>';		
	}
	else
	{
		//choice has not been stored, leave box unchecked
		$cbOut .= '<input type="checkbox" name="'.$val.'" value="'.$val.'"/><label>' . $val . '</label><br/>';
	}
}

//output checkboxes
echo $cbOut;
johnsquibb
Junior Poster in Training
84 posts since Nov 2007
Reputation Points: 14
Solved Threads: 14
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You