AdRock Posted March 22, 2010 Share Posted March 22, 2010 I have a form with a lot of fields but they are in groups with checkboxes and text fields. The question is what is the best way to store this data in the database? Would i store the whole $_POST array as a single field and explode when i want to pull from the database or is it best to have a separate field for each value (that would make the table huge with possibly a lot of empty fields)? The one that bothers me the most of the text fields beucase there is 4 text boxes for one row on my form and I have about 10 rows. The form is like an application form where you put in your work experience Here is part of my example form checkboxes <div class="left"> <p class="label_checkbox_pair"> <input type="checkbox" name="quals[]" value="carpenter" /><label for="carpenter">Carpenter:</label> </p> <p class="label_checkbox_pair"> <input type="checkbox" name="quals[]" value="mechanic" /><label for="mechanic">Mechanic</label> </p> <p class="label_checkbox_pair"> <input type="checkbox" name="quals[]" value="electrician" /><label for="electrician">Electrician</label> </p> <p class="label_checkbox_pair"> <input type="checkbox" name="quals[]" value="plumber" /><label for="plumber">Plumber</label> </p> <p class="label_checkbox_pair"> <input type="checkbox" name="quals[]" value="fire warden" /><label for="fire_warden">Fire Warden</label> </p> </div> and text fields <li> <input name="Experience0 From" type="text" size="3" maxlength="4" /> - <input name="Experience0 To" type="text" size="3" maxlength="4" /> <input class="exprol" name="Experience0 Role" type="text" size="18" /> <input class="expfest" name="Experience0 Festival" type="text" size="30" /> </li> <li> <input name="Experience1 From" type="text" size="3" maxlength="4" /> - <input name="Experience1 To" type="text" size="3" maxlength="4" /> <input class="exprol" name="Experience1 Role" type="text" size="18" /> <input class="expfest" name="Experience1 Festival" type="text" size="30" /> </li> <li> <input name="Experience2 From" type="text" size="3" maxlength="4" /> - <input name="Experience2 To" type="text" size="3" maxlength="4" /> <input class="exprol" name="Experience2 Role" type="text" size="18" /> <input class="expfest" name="Experience2 Festival" type="text" size="30" /> </li> Link to comment https://forums.phpfreaks.com/topic/196106-advice-on-how-to-save-this-data-from-form/ Share on other sites More sharing options...
scvinodkumar Posted March 22, 2010 Share Posted March 22, 2010 i think the best way is "storing the whole $_POST array as a single field and explode them while displaying." Link to comment https://forums.phpfreaks.com/topic/196106-advice-on-how-to-save-this-data-from-form/#findComment-1029918 Share on other sites More sharing options...
AdRock Posted March 22, 2010 Author Share Posted March 22, 2010 That's what I thought I've done that with the checkboxes and I created 10 fields for the last part (10 rows on the form) and i using the implode/explode to get each row in it's own field. Just another quick question, the first 2 columns are dates and the other 2 are text. Can you do form validation suing something like: <li> <input name="Experience4[] From" type="text" size="3" maxlength="4" /> - <input name="Experience4[] To" type="text" size="3" maxlength="4" /> <input class="exprol" name="Experience4[] Role" type="text" size="18" /> <input class="expfest" name="Experience4[] Festival" type="text" size="30" /> </li> check_input($_POST['experience4 From']) Link to comment https://forums.phpfreaks.com/topic/196106-advice-on-how-to-save-this-data-from-form/#findComment-1029920 Share on other sites More sharing options...
scvinodkumar Posted March 22, 2010 Share Posted March 22, 2010 yeah you can validate first two values, like, if($_POST['experience4 From'][0]!='') { //do some validation } if($_POST['experience4 From'][1]!='') { //do some validation } Link to comment https://forums.phpfreaks.com/topic/196106-advice-on-how-to-save-this-data-from-form/#findComment-1029922 Share on other sites More sharing options...
AdRock Posted March 22, 2010 Author Share Posted March 22, 2010 I've been trying different things and i got the checkboxes to implode with a comma but i'm having trouble with every text field. It still tries to implode the empty fields and i'm getting an error. Warning: implode() [function.implode]: Invalid arguments passed in for each row of text fields //variables for checking the user's name $userid = $_SESSION['userid']; $quals = check_input(implode(",",$_POST['quals'])); $roles = check_input(implode(",",$_POST['roles'])); $region = check_input(implode(",",$_POST['region'])); $experience0 = check_input(implode(",",$_POST['experience0'])); $experience1 = check_input(implode(",",$_POST['experience1'])); $experience2 = check_input(implode(",",$_POST['experience2'])); $experience3 = check_input(implode(",",$_POST['experience3'])); $experience4 = check_input(implode(",",$_POST['experience4'])); $experience5 = check_input(implode(",",$_POST['experience5'])); $experience6 = check_input(implode(",",$_POST['experience6'])); $experience7 = check_input(implode(",",$_POST['experience7'])); $experience8 = check_input(implode(",",$_POST['experience8'])); $experience9 = check_input(implode(",",$_POST['experience9'])); I was thinking about checking if they are empty before doing the implode but is there an effieicent way of doing it like if a for loop? Link to comment https://forums.phpfreaks.com/topic/196106-advice-on-how-to-save-this-data-from-form/#findComment-1029923 Share on other sites More sharing options...
scvinodkumar Posted March 22, 2010 Share Posted March 22, 2010 You should use implode only after checking the input, like $quals_value = ''; $quals = $_POST['quals']; if(check_input($quals)) $quals_value = $implode(",",$_POST['quals']); else //error message Link to comment https://forums.phpfreaks.com/topic/196106-advice-on-how-to-save-this-data-from-form/#findComment-1029925 Share on other sites More sharing options...
AdRock Posted March 22, 2010 Author Share Posted March 22, 2010 This should be the last question. Is there a way i can loop through each of these without having to do loads of if statements? if(!empty($_POST['experience0'])) $experience0 = check_input(implode(",",$_POST['experience0'])); if(!empty($_POST['experience1'])) $experience1 = check_input(implode(",",$_POST['experience1'])); if(!empty($_POST['experience2'])) $experience2 = check_input(implode(",",$_POST['experience2'])); if(!empty($_POST['experience3'])) $experience3 = check_input(implode(",",$_POST['experience3'])); if(!empty($_POST['experience4'])) $experience4 = check_input(implode(",",$_POST['experience4'])); if(!empty($_POST['experience5'])) $experience5 = check_input(implode(",",$_POST['experience5'])); if(!empty($_POST['experience6'])) $experience6 = check_input(implode(",",$_POST['experience6'])); if(!empty($_POST['experience7'])) $experience7 = check_input(implode(",",$_POST['experience7'])); if(!empty($_POST['experience8'])) $experience8 = check_input(implode(",",$_POST['experience8'])); if(!empty($_POST['experience9'])) $experience9 = check_input(implode(",",$_POST['experience9'])); I though about using variable variables but i can't get it to work in a for loop Link to comment https://forums.phpfreaks.com/topic/196106-advice-on-how-to-save-this-data-from-form/#findComment-1029938 Share on other sites More sharing options...
scvinodkumar Posted March 22, 2010 Share Posted March 22, 2010 No problem, use this loop [code=php:0] for($i=0;$i<10;$i++) { if(!empty($_POST['experience'.$i])) $experience.$i = check_input(implode(",",$_POST['experience'.$i])); } [/code] Link to comment https://forums.phpfreaks.com/topic/196106-advice-on-how-to-save-this-data-from-form/#findComment-1029939 Share on other sites More sharing options...
AdRock Posted March 22, 2010 Author Share Posted March 22, 2010 That did the trick....cheers mate Link to comment https://forums.phpfreaks.com/topic/196106-advice-on-how-to-save-this-data-from-form/#findComment-1029953 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.