Phear46 Posted May 7, 2013 Share Posted May 7, 2013 OK, I wrote one function to write my form to a DB, then i created another page, with a different form and wrote another function to write it to a DB. Being lazy and wanting to streamline the process for further 'form writing' I tried to be crafty and hack togther a function that would write the correct form, to the correct database no matter what. So i could come up with any number of forms with different sizes etc different field names and it would just automagically work. I thought.... Hey, if i just make sure the 'name' of the input on the $_POST is the same string as the field name in the db, then it would work no matter what. Until i thought about when my form contained 'arrays' such as a check box array. Now im stuck.... The idea was to assign the post array to a new array so i could mess with it and not screw with the original POST array. Easy - //Form results into array $form_array = array(); foreach ($_POST as $k => $v) { $form_array[$k] = $v; } } print_r($form_array); Now what if $k is an array? I figure define another array using the string of $k something like: //Form results into array $form_array = array(); foreach ($_POST as $k => $v) { if (is_array($k)) { //Create new array with name $k $k = array(); $k[k] = $v; } else { $form_array[$k] = $v; } } print_r($form_array); But this just calls the array '$k' instead of '$string from $k'. Then i got to thinking.... even if i know how to use the string to create the variable name, how do i call it from the rest of the script? I cant use $string because writing it now i don't know what $string will be! Now I'm confused and don't know if this is even possible. I imagine it is but may be somewhat beyond my abilities at this point! I apologize if this isn't making any sense. If you do understand and could possibly point me in the direction of some useful functions within PHP already that would be awesome! i cant find anything yet but ill keep looking! Quote Link to comment Share on other sites More sharing options...
php_nub_qq Posted May 7, 2013 Share Posted May 7, 2013 you can just $form_array = $_POST Quote Link to comment Share on other sites More sharing options...
Jessica Posted May 7, 2013 Share Posted May 7, 2013 While you can just assign the entire $_POST array into another variable, why would you have to? If all your code is already in a function, just send the $_POST array to the function. As long as you're not passing by reference, your original array will not be touched. The actual problem you're having isn't very clear however. Quote Link to comment Share on other sites More sharing options...
Phear46 Posted May 7, 2013 Author Share Posted May 7, 2013 Ive got a solution, gimme a sec and ill post what ive done... almost there! Quote Link to comment Share on other sites More sharing options...
Phear46 Posted May 7, 2013 Author Share Posted May 7, 2013 function my_Form_To_DB_Test($host,$user,$pass,$db,$table) { //Form results into array $form_array = array(); foreach ($_POST as $k => $v) { $form_array[$k] = $v; } array_pop($form_array); //Remove last entry from $_POST array (should be the button name/value!) print_r($form_array); echo "<br />"; /***** CONSTRUCT QUERY ****/ //Get field names, format into string ready for mySQL Query eg $mySQL_field_names = "(field1, field2, field3)" $mySQL_field_names = ""; $tmpStr = ""; foreach($form_array as $k => $v) { if ($tmpStr == ""){ mysql_escape_string($k); $tmpStr = $k; } else { mysql_escape_string($k); $tmpStr = "$tmpStr,$k"; } } $mySQL_field_names = "(" . $tmpStr . ")"; //Get values, format into string ready for mySQL Query eg $mySQL_values = "('value1', 'value2', '3,3a,3b')" $mySQL_values = ""; $tmpStr = ""; foreach($form_array as $k => $v) { if (is_array($v)) { foreach($v as $val) { if ($tmpArrStr == ""){ mysql_escape_string($val); $tmpArrStr = $val; } else { mysql_escape_string($val); $tmpArrStr = "$tmpArrStr,$val"; } } if ($tmpStr == ""){ mysql_escape_string($v); $tmpStr = "'$tmpArrStr'"; } else { $tmpStr = "$tmpStr, '$tmpArrStr'"; } } else { if ($tmpStr == ""){ mysql_escape_string($v); $tmpStr = "'$v'"; } else { mysql_escape_string($v); $tmpStr = "$tmpStr, '$v'"; } } } $mySQL_values = "(" . $tmpStr . ")"; //Construct final query $query = "INSERT INTO $table $mySQL_field_names VALUES $mySQL_values"; echo $query; This works as intended now. I was over complicating it before, the trick I find it to be MORE lazy.... the function will write a form input to the database aslong as the input name == db field name E.G: //This POST array -> Array ( [firstname] => John [surname] => Doe [dob] => 1980-01-01 [class] => Array ( [0] => english [1] => maths [2] => science ) ) //Creates this mySQL query -> INSERT INTO students (firstname,surname,dob,class) VALUES ('John', 'Doe', '1980-01-01', 'english,maths,science') So this should work whatever form I pass to the function i think If anyone has a suggestion to make it better im all ears! i know the foreach at the very top isnt needed, ill get rid of it. Quote Link to comment Share on other sites More sharing options...
Barand Posted May 7, 2013 Share Posted May 7, 2013 That would be fine if didn't rely on bad, un-normalized table design EG 'english,maths,science' Those should be in a separate table, one value per row and should use subject_id values and not names Quote Link to comment Share on other sites More sharing options...
Phear46 Posted May 7, 2013 Author Share Posted May 7, 2013 Thanks for the input Barand, could you explain a little more? the only reason I'm storing the values like that is because the 'class' column is of the SET data type. I appreciate that its probably not the best database design around but I've not been at this long, this is the first 'project' I've started since following a beginner tutorial. If you have any good reference websites or links to learning materials for database design that would be great. Im not sure what you mean by un-normalized design & subject_id values not names? I was thinking of setting up the table to have one column (field?) per 'subject/class' whatever and then setting true/false. Would this be better? If so why? I dont see a difference as long as i can 'select * from table where class = science' for instance. Admittedly i haven't tried this yet so can guarantee it will work >.< Thanks for reading the original post anyway, i think i got a bit of word vomit in the OP and couldn't for the life of me explain my self properly! Sorry! Quote Link to comment Share on other sites More sharing options...
Barand Posted May 7, 2013 Share Posted May 7, 2013 this should help http://forums.phpfreaks.com/topic/273634-best-way-to-set-up-tables-when-multiple-values/?do=findComment&comment=1408360 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.