947740 Posted May 7, 2008 Share Posted May 7, 2008 I have these form fields: I have to add all of these to a database with a query similar to this: "INSERT INTO hitting (player,team,gameid,ab,r,h,2b,3b,hr,rbi,position,so,bb) values (\"$_POST['C']\",\"$_POST['gameteam']\",\"$_POST['gamedate']\",\"$_POST['ab_catcher']\",\"$_POST['r_catcher']\",\"$_POST['h_catcher']\",\"$_POST['2b_catcher']\",\"$_POST['3b_catcher']\",\"$_POST['hr_catcher']\",\"$_POST['rbi_catcher']\",\"C\",\"$_POST['k_catcher']\",\"$_POST['bb_catcher']\")" I have tried to think of an efficient way to do this, but have drawn a blank. I have to enter these values for _catcher through ex_bat1, through _6. Does anyone have any ideas? gamedate = 6 gameteam = V ab_catcher = r_catcher = h_catcher = 2b_catcher = 3b_catcher = hr_catcher = rbi_catcher = k_catcher = bb_catcher = ab_1B = r_1B = h_1B = 2b_1B = 3b_1B = hr_1B = rbi_1B = k_1B = bb_1B = ab_2B = r_2B = h_2B = 2b_2B = 3b_2B = hr_2B = rbi_2B = k_2B = bb_2B = SS = Brandon Herrick ab_SS = r_SS = h_SS = 2b_SS = 3b_SS = hr_SS = rbi_SS = k_SS = bb_SS = ab_3B = r_3B = h_3B = 2b_3B = 3b_3B = hr_3B = rbi_3B = k_3B = bb_3B = LF = Curtis Morrical ab_LF = r_LF = h_LF = 2b_LF = 3b_LF = hr_LF = rbi_LF = k_LF = bb_LF = CF = Curtis Morrical ab_CF = r_CF = h_CF = 2b_CF = 3b_CF = hr_CF = rbi_CF = k_CF = bb_CF = RF = Curtis Morrical ab_RF = r_RF = h_RF = 2b_RF = 3b_RF = hr_RF = rbi_RF = k_RF = bb_RF = ex_bat1 = Sean Carruthers ab_spare1 = r_spare1 = h_spare1 = 2b_spare1 = 3b_spare1 = hr_spare1 = rbi_spare1 = k_spare1 = bb_spare1 = ex_bat2 = Sean Carruthers ab_spare2 = r_spare2 = h_spare2 = 2b_spare2 = 3b_spare2 = hr_spare2 = rbi_spare2 = k_spare2 = bb_spare2 = ex_bat3 = Sean Carruthers ab_spare3 = r_spare3 = h_spare3 = 2b_spare3 = 3b_spare3 = hr_spare3 = rbi_spare3 = k_spare3 = bb_spare3 = ex_pit1 = Sean Carruthers ab_SP = r_SP = h_SP = 2b_SP = 3b_SP = hr_SP = rbi_SP = k_SP = bb_SP = w_1 = l_1 = sv_1 = bsv_1 = sho_1 = cg_1 = so_1 = bb_1 = er_1 = p_h_1 = s_1 = b_1 = ex_pit2 = Sean Carruthers ab_RP1 = r_RP1 = h_RP1 = 2b_RP1 = 3b_RP1 = hr_RP1 = rbi_RP1 = k_RP1 = bb_RP1 = w_2 = l_2 = sv_2 = bsv_2 = sho_2 = cg_2 = so_2 = bb_2 = er_2 = p_h_2 = s_2 = b_2 = ex_pit3 = Sean Carruthers ab_RP2 = r_RP2 = h_RP2 = 2b_RP2 = 3b_RP2 = hr_RP2 = rbi_RP2 = k_RP2 = bb_RP2 = w_3 = l_3 = sv_3 = bsv_3 = sho_3 = cg_3 = so_3 = bb_3 = er_3 = p_h_3 = s_3 = b_3 = ex_pit4 = Sean Carruthers ab_RP3 = r_RP3 = h_RP3 = 2b_RP3 = 3b_RP3 = hr_RP3 = rbi_RP3 = k_RP3 = bb_RP3 = w_4 = l_4 = sv_4 = bsv_4 = sho_4 = cg_4 = so_4 = bb_4 = er_4 = p_h_4 = s_4 = b_4 = ex_pit5 = Sean Carruthers ab_RP4 = r_RP4 = h_RP4 = 2b_RP4 = 3b_RP4 = hr_RP4 = rbi_RP4 = k_RP4 = bb_RP4 = w_5 = l_5 = sv_5 = bsv_5 = sho_5 = cg_5 = so_5 = bb_5 = er_5 = p_h_5 = s_5 = b_5 = ex_pit6 = Sean Carruthers ab_CP = r_CP = h_CP = 2b_CP = 3b_CP = hr_CP = rbi_CP = k_CP = bb_CP = w_6 = l_6 = sv_6 = bsv_6 = sho_6 = cg_6 = so_6 = bb_6 = er_6 = p_h_6 = s_6 = b_6 = addstats = Add the Statistics Link to comment https://forums.phpfreaks.com/topic/104554-adding-form-fields-to-database/ Share on other sites More sharing options...
DyslexicDog Posted May 7, 2008 Share Posted May 7, 2008 First of all using un-escaped variables isn't a good idea. Could you explain a little better on what the over all plan is for the application, there might be a better design idea available. Link to comment https://forums.phpfreaks.com/topic/104554-adding-form-fields-to-database/#findComment-535193 Share on other sites More sharing options...
rhodesa Posted May 7, 2008 Share Posted May 7, 2008 if you put the list of keys into an array, you can do some fancy footwork.... <?php //List of valid keys $keys = array('gamedate','gameteam','ab_catcher'); //etc //Build dataset $data = array(); foreach($keys as $key) $data[$key] = mysql_real_escape_string($_POST[$key]); //Build SQL $sql = "INSERT INTO tablename (`".implode("`,`",array_keys($data))."`) VALUES ('".implode("','",$data)."')"; //Execute SQL mysql_query($sql) or die(mysql_error()); ?> Link to comment https://forums.phpfreaks.com/topic/104554-adding-form-fields-to-database/#findComment-535194 Share on other sites More sharing options...
947740 Posted May 7, 2008 Author Share Posted May 7, 2008 It is a baseball statistics website. The point is to add the stats. to the database, for use later. All of these stats are for one game; only administrators can access this page. rhodesa-I would have to make about 17 arrays, would I not? Seeing as I have 17 different players? Link to comment https://forums.phpfreaks.com/topic/104554-adding-form-fields-to-database/#findComment-535196 Share on other sites More sharing options...
rhodesa Posted May 7, 2008 Share Posted May 7, 2008 It is a baseball statistics website. The point is to add the stats. to the database, for use later. All of these stats are for one game; only administrators can access this page. rhodesa-I would have to make about 17 arrays, would I not? Seeing as I have 17 different players? I just re-read your post and I realize now what you are trying to do. The code I posted before isn't what you are looking for. More code coming your way in a minute... Link to comment https://forums.phpfreaks.com/topic/104554-adding-form-fields-to-database/#findComment-535224 Share on other sites More sharing options...
947740 Posted May 7, 2008 Author Share Posted May 7, 2008 Thanks a lot! Link to comment https://forums.phpfreaks.com/topic/104554-adding-form-fields-to-database/#findComment-535225 Share on other sites More sharing options...
rhodesa Posted May 7, 2008 Share Posted May 7, 2008 Ok...so here is some code that will help point you in the right direction hopefully. But, I notice there are a lot of inconsistencies in the names of your form items. Like catcher when it should be C, or k_1B when it should be so_1B. So, this probably won't work right away, and you'll need to play with the form a little: <?php $positions = array('C','1B','2B','SS','3B','LF','CF','RF'); $stats = array('ab','r','h','2b','3b','hr','rbi','k','bb'); foreach($positions as $position){ $data = array( 'player' => mysql_real_escape_string($_POST[$position]), 'team' => mysql_real_escape_string($_POST['gameteam']), 'gameid' => mysql_real_escape_string($_POST['gamedate']), 'position' => mysql_real_escape_string($position), ); foreach($stats as $stat) $data[$stat] = mysql_real_escape_string($stat.'_'.$_POST[$position]); $sql = "INSERT INTO hitting (`".implode("`,`",array_keys($data))."`) VALUES ('".implode("','",$data)."')"; } ?> Also, this would be A LOT better if you used arrays in your form elements. Here is an example: <form> <h1>First Baseman</h1> At bats: <input type="text" name="ab['1B']" /><br> Runs: <input type="text" name="r['1B']" /><br> Hits: <input type="text" name="h['1B']" /><br> <h1>Second Baseman</h1> At bats: <input type="text" name="ab['2B']" /><br> Runs: <input type="text" name="r['2B']" /><br> Hits: <input type="text" name="h['2B']" /><br> </form> Can you alter the form to follow this schema? If so, this will become much easier... Link to comment https://forums.phpfreaks.com/topic/104554-adding-form-fields-to-database/#findComment-535232 Share on other sites More sharing options...
947740 Posted May 7, 2008 Author Share Posted May 7, 2008 Thanks a lot. I noticed some of the errors in the formnames and I fixed some of those. Yeah, I can alter the form, but it might take me a while since there are so many fields. By the way, I do not need to escape the strings, because administrators are only going to be entering numbers. Some of the lacks in consistency lay in the fact that the first part is hitting, and the second is hitting and pitching. How would I go about processing the code if I used the second way you suggested? Link to comment https://forums.phpfreaks.com/topic/104554-adding-form-fields-to-database/#findComment-535276 Share on other sites More sharing options...
947740 Posted May 7, 2008 Author Share Posted May 7, 2008 I changed all of the field names now. If you want to see the names go http://www.iowatelecom.net/~scarruthers/fields.txt Link to comment https://forums.phpfreaks.com/topic/104554-adding-form-fields-to-database/#findComment-535303 Share on other sites More sharing options...
rhodesa Posted May 7, 2008 Share Posted May 7, 2008 To be honest, the code is almost the same, but it's just a cleaner way of doing it...do a print_r($_POST); and you'll see how nice and organized everything is. <?php $positions = array('C','1B','2B','SS','3B','LF','CF','RF'); $keys = array('player','ab','r','h','2b','3b','hr','rbi','k','bb'); foreach($positions as $position){ $data = array( 'team' => $_POST['gameteam'], 'gameid' => $_POST['gamedate'], 'position' => $position, ); foreach($keys as $key) $data[$key] = $_POST[$key][$position]; $sql = "INSERT INTO hitting (`".implode("`,`",array_keys($data))."`) VALUES ('".implode("','",$data)."')"; } ?> and then the form: <form> <h1>First Baseman</h1> Name: <input type="text" name="player['1B']" /><br> At bats: <input type="text" name="ab['1B']" /><br> Runs: <input type="text" name="r['1B']" /><br> Hits: <input type="text" name="h['1B']" /><br> Doubles: <input type="text" name="2b['1B']" /><br> Triples: <input type="text" name="3b['1B']" /><br> Home runs: <input type="text" name="hr['1B']" /><br> RBIs: <input type="text" name="rbi['1B']" /><br> Strikeouts: <input type="text" name="k['1B']" /><br> Walks: <input type="text" name="bb['1B']" /><br> </form> Link to comment https://forums.phpfreaks.com/topic/104554-adding-form-fields-to-database/#findComment-535308 Share on other sites More sharing options...
rhodesa Posted May 7, 2008 Share Posted May 7, 2008 I get a 404 loading that URL Link to comment https://forums.phpfreaks.com/topic/104554-adding-form-fields-to-database/#findComment-535309 Share on other sites More sharing options...
947740 Posted May 7, 2008 Author Share Posted May 7, 2008 Trying to fix that... Seeing as you wrote it out, just ignore the link. I actually did the form fields backwards. E.G. 1B['ab']; Do I have to do it backwards then? Link to comment https://forums.phpfreaks.com/topic/104554-adding-form-fields-to-database/#findComment-535310 Share on other sites More sharing options...
rhodesa Posted May 7, 2008 Share Posted May 7, 2008 if it's that way, just update this line: $data[$key] = $_POST[$position][$key]; Link to comment https://forums.phpfreaks.com/topic/104554-adding-form-fields-to-database/#findComment-535348 Share on other sites More sharing options...
947740 Posted May 7, 2008 Author Share Posted May 7, 2008 Thanks a lot rhodesa. I will give that a try this evening. I do not know what I would have done without your help, other than doing it the long, manual way. Link to comment https://forums.phpfreaks.com/topic/104554-adding-form-fields-to-database/#findComment-535392 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.