timmah1 Posted September 4, 2008 Share Posted September 4, 2008 I have form with numerous checkboxes labled as so <input type="checkbox" name="medication[]" value="128"> <input type="checkbox" name="medication[]" value="12"> When I post the form, how do I get everyone of the boxes checked into the database? I'm lost. Thanks in advance! Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted September 4, 2008 Share Posted September 4, 2008 how do I get everyone of the boxes checked into the database? Can you explain exactly what you want to do with the values and the database? Your just going to use a foreach loop. <?php foreach ($_POST['medication'] as $med){ echo $med .'<br>'; } ?> Quote Link to comment Share on other sites More sharing options...
timmah1 Posted September 4, 2008 Author Share Posted September 4, 2008 Each medication is associated with a certain value. I need the values to go into the database as 128,12,etc A foreach loop adds every med in a different row, it all needs to be in the same field Quote Link to comment Share on other sites More sharing options...
Psycho Posted September 4, 2008 Share Posted September 4, 2008 Each medication is associated with a certain value. I need the values to go into the database as 128,12,etc A foreach loop adds every med in a different row, it all needs to be in the same field Doing that is bad practice. I fyou have a many to one relationship you should have a child table for those records. But, I will answer your question nonetheless. The great thing about checkboxes is that if they are not checked their value is not posted. So, concatenating the values with commas is very simple: $meds = implode(',', $_POST['medication']); Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted September 4, 2008 Share Posted September 4, 2008 Try this <?php $meds = implode(", ", $_POST['medication']); $query = "INSERT INTO table (field_name) VALUES ('$meds')"; $result = mysql_query($query)or die(mysql_error()); ?> Quote Link to comment Share on other sites More sharing options...
timmah1 Posted September 4, 2008 Author Share Posted September 4, 2008 Thank you mjdamato Does exactly what is needed. This is for charting. When someone enters their medications on the form, it puts them into the database field "medications", along with their username and a timestamp. I'm not sure why it's 'bad practice', it works for what I need it to. Are there great drawbacks for doing this? Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted September 4, 2008 Share Posted September 4, 2008 The "correct" way to do it would be to insert a new row for every different medication they selected. So you would make a new table called something like "user_medications", and it would look something like userID | medicineID ------------------- 1 128 1 12 4 68 That way it just makes it easier to select the information you need. The way your doing it your going to have to select the filed, split up all the medicine ID's, then do a million queries to get specific information on each medication (which I assume is in another table). If you do it the way I described, you can get all the information you need with one query using a join. Quote Link to comment Share on other sites More sharing options...
Psycho Posted September 4, 2008 Share Posted September 4, 2008 Are there great drawbacks for doing this? Depends on your definition of "great drawbacks" and your future needs. Having the data "normalized" such as pocobueno1388 explained you will have much greater flexibility. Without knowing your current or future needs I can't say how much of a benefit, if any, it would be. But, let's say you needed a report of how many users are taking each medication. The only way to do it with the current structure would be to query the entire database and then create a post-processing script in PHP to analyze the data. Which would create a lot of unnecessary overhead. With a child table like pocobueno1388 showed you could get the numebr of users taking each medication with the following query: SELECT count(*) FROM medications JOIN users on medications.user_id = users.id GROUP BY medications Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.