spitfire1945 Posted December 31, 2008 Share Posted December 31, 2008 Fear not, the problem is not as dramatic as the title suggests I have a few input textfields in a form. I am just having a bit of a coder block as to how to get these variables into the database. Ill start with the variables: $_POST['a_sf'] $_POST['a_be'] $_POST['a_bb'] $_POST['b_sf'] $_POST['b_be'] $_POST['b_bb'] $_POST['c_sf'] $_POST['c_be'] $_POST['c_bb'] and I have a db table called table with columns id, sf, be, and bb I want the table to store data like this +---+------+-----+-----+----+ | id | type | sf | be | bb | +---+------+-----+-----+----+ | 1 | a | 12 | 78 | 54 | +---+------+-----+-----+----+ | 2 | b | 98 | 45 | 37 | +---+------+-----+-----+----+ so on and so forth.. any coding ideas as to how I can achieve this? I have tried several for loops and foreach loops et al but since the post variables are alphabets and not numerical its a little bit of a pain in the bum. or maybe a better way to setup the variable names for the text fields perhaps? Link to comment https://forums.phpfreaks.com/topic/138976-solved-php-multi-dimension-disaster-topped-with-mysql-dilemmas/ Share on other sites More sharing options...
xtopolis Posted December 31, 2008 Share Posted December 31, 2008 I'm not sure what you're asking? Since you only have 9 elements, why not just insert them using mysql? <?php $a_sf = intval($_POST['a_sf']); $a_be = intval($_POST['a_be'); //etc.. $b_sf = intval($_POST['b_sf']); //etc.. $sql = "INSERT INTO yourTable (id,type,sf,be,bb) VALUES ('','a','$a_sf','$a_be','$a_bb'), ('','b','$b_sf','$b_be','$b_bb'), ('','c','$c_sf','$c_be','$c_bb')"; ?> However, if you're saying you have more than this.. Perhaps a foreach($_POST as $key => $val) ..splitting the key name on "_" and maybe putting them into an array based on $splitresult[0]... depends.. Link to comment https://forums.phpfreaks.com/topic/138976-solved-php-multi-dimension-disaster-topped-with-mysql-dilemmas/#findComment-726838 Share on other sites More sharing options...
GingerRobot Posted December 31, 2008 Share Posted December 31, 2008 or maybe a better way to setup the variable names for the text fields perhaps? Yup. Use arrays for the names of the text fields. I'd go with two dimensions like this: <input type="text" name="data[a][sf]" /><br /> <input type="text" name="data[a][be]" /><br /> <input type="text" name="data[a][bb]" /><br /> <input type="text" name="data[b][sf]" /><br /> <input type="text" name="data[b][be]" /><br /> <input type="text" name="data[b][bb]" /><br /> //etc Then use PHP like this: $temp = array(); $temp2 = array(); foreach($_POST['data'] as $type => $stats){ $temp2[] = "('".mysql_real_escape_string($type)."'"; foreach($stats as $stat){ $temp2[] = "'".mysql_real_escape_string($stat)."'"; } $temp[] = join(',',$temp2).')'; $temp2 = array(); } $sql = join(',',$temp); $sql = "INSERT INTO yourtable SET (type,sf,be,bb) VALUES $sql"; mysql_query($sql) or trigger_error(mysql_error(),E_USER_ERROR); Link to comment https://forums.phpfreaks.com/topic/138976-solved-php-multi-dimension-disaster-topped-with-mysql-dilemmas/#findComment-726842 Share on other sites More sharing options...
spitfire1945 Posted December 31, 2008 Author Share Posted December 31, 2008 haHA! Geniuses Thanks guys Ginger's ideas seems a bit more feasible since its scalable. I just couldn't get my head around multi-dimensional arrays. I will try the code and get back to you guys. In the meantime, thanks. Link to comment https://forums.phpfreaks.com/topic/138976-solved-php-multi-dimension-disaster-topped-with-mysql-dilemmas/#findComment-726845 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.