bravo14 Posted May 3, 2014 Share Posted May 3, 2014 Hi Using the array below I am populating a select field $scoring=array( '3' => 'First', '2' => 'Second', '1' => 'Third', '0' => 'Fourth', 'R' => 'Retired or mechanical failure', 'F' => 'Fell', 'FN'=> 'Fell and non-starter in re-run of race', 'N2'=> 'Exclusion for exceeding two minute time allowance', 'E' => 'Exclusion for starting infringement', 'FX'=> 'Fell and excluded from re-run of race', 'X' => 'Other exclusion', 'N' => 'Replaced by a reserve, or non-starter', '-'=> 'No ride'); <select name="points[]" class="txtfield"> <option value="">-----------------</option> <?php foreach($scoring as $key=> $value): echo '<option value="'.$key.'">'.$value.'</option>'; //close your tags!! endforeach; ?> </select> on submit I then use the following code, checking if the value of `points` is numeric, and if not set pts to be 0 and add the posted value to a different field. <?php if(isset($_POST['submit-scores'])){ $heat=$_POST['heat']; $next_heat=$_POST['nextheat']; $card=$_POST['card']; for($i=0;$i<4;$i++){ //set rider variables $id=$_POST['id']; $points=$_POST['points']; if(is_numeric($points)){ $DNF = NULL; $pts=$points;} else{ $pts=0; $DNF = $points; } $rider=$_POST['rider']; if(isset($_POST['substitute'])){ $sub=$_POST['sub']; $subpts=$_POST['subpts']; } else{ $sub=NULL; $subpts=NULL; } //update database $sql1="UPDATE tbl_heat SET rider_name='$rider[$i]', points='$pts[$i]', DNF= '$DNF[$i] ', substitute='$sub[$i]', sub_points='$subpts[$i]' WHERE id='$id[$i]'"; echo $sql1.'<br/>'; //$result1=mysql_query($sql1)or die(mysql_error()); } //update card info $sql="Update tbl_card SET next_heat = $next_heat where `card_id` = $card"; //$result=mysql_query($sql)or die(mysql_error()); //header ('Location: score-card.php?card='.$card.'#'.$nextheat); } ?> when I echo the query I get the following result UPDATE tbl_heat SET rider_name='Chris Harris', points='', DNF= '3 ', substitute='', sub_points='' WHERE id='853'UPDATE tbl_heat SET rider_name='Kenneth Bjerre', points='', DNF= '2 ', substitute='', sub_points='' WHERE id='854'UPDATE tbl_heat SET rider_name='Martin Smolinski', points='', DNF= '1 ', substitute='', sub_points='' WHERE id='855'UPDATE tbl_heat SET rider_name='Chris Holder', points='', DNF= 'F ', substitute='', sub_points='' WHERE id='856' it appears to be adding a space to the end of each of the array keys, therefore saying that 3 is not numeric etc, so a cople of things, why is the space being added, secondly is this the best way to achieve the result? Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted May 3, 2014 Share Posted May 3, 2014 did you look at your query statement? you have typed a space in it after the $DNF[$i] variable. Quote Link to comment Share on other sites More sharing options...
bravo14 Posted May 3, 2014 Author Share Posted May 3, 2014 I have corrected that, however the problem is occurring before the query line, because if the value is numeric then the posted value should be the value of the $pts variable and not the $DNF variable Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted May 3, 2014 Share Posted May 3, 2014 in your form field, name="points[]" is an array. it won't be numeric. Quote Link to comment Share on other sites More sharing options...
bravo14 Posted May 3, 2014 Author Share Posted May 3, 2014 I have changed the submit code to <?php if(isset($_POST['submit-scores'])){ $heat=$_POST['heat']; $next_heat=$_POST['nextheat']; $card=$_POST['card']; for($i=0;$i<4;$i++){ //set rider variables $id=$_POST['id']; $points=$_POST['points']; echo $points; if($points=='3'){ $pts = 3; $DNF = NULL; } elseif($points=='2'){ $pts = 2; $DNF = NULL; } elseif($points == '1'){ $pts = 1; $DNF = NULL; } elseif($points == '0'){ $pts = 0; $DNF = NULL; } else{ $pts = 0; $DNF = $points; } $rider=$_POST['rider']; if(isset($_POST['substitute'])){ $sub=$_POST['sub']; $subpts=$_POST['subpts']; } else{ $sub=NULL; $subpts=NULL; } //update database $sql1="UPDATE tbl_heat SET rider_name='$rider[$i]', points='$pts[$i]', DNF= '$DNF[$i]', substitute='$sub[$i]', sub_points='$subpts[$i]' WHERE id='$id[$i]'"; echo $sql1.'<br/>'; //$result1=mysql_query($sql1)or die(mysql_error()); } //update card info $sql="Update tbl_card SET next_heat = $next_heat where `card_id` = $card"; //$result=mysql_query($sql)or die(mysql_error()); //header ('Location: score-card.php?card='.$card.'#'.$nextheat); } ?> but I am now getting ArrayUPDATE tbl_heat SET rider_name='Chris Harris', points='', DNF= '3', substitute='', sub_points='' WHERE id='853'ArrayUPDATE tbl_heat SET rider_name='Kenneth Bjerre', points='', DNF= '2', substitute='', sub_points='' WHERE id='854'ArrayUPDATE tbl_heat SET rider_name='Martin Smolinski', points='', DNF= '1', substitute='', sub_points='' WHERE id='855'ArrayUPDATE tbl_heat SET rider_name='Chris Holder', points='', DNF= 'F', substitute='', sub_points='' WHERE id='856' I still cant get it to update the points with a value Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted May 3, 2014 Share Posted May 3, 2014 since your form field is an array, and you apparently have 4 similar select menus, you must use array notation/functions to access the elements of the array. you can either use your for($i=0;$i<4;$i++){ loop index $i variable - $points=$_POST['points'][$i]; or just use a foreach loop foreach($_POST['points'] as $points){ ... } 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.