I’m having some trouble with a form designed to insert fixtures into a database for a sports league.
The previous page contains a form which defines through _POST “area”/”division”/”season” and contains an iterated (to ten) loop of a group of fixture fields, which are defined as having arrayed names like “hometeam[$i]”, “awayteam[$i]” and so forth.
On the page which processes the input of the fixtures, I need to pull each individual iteration of the form – i.e. “hometeam[1]” “awayteam[1]” and so on – from within the $_POST array and insert that into the MYSQL database before moving onto the next one. I’m totally in over my head with foreach and if clauses so I thought it best to ask here.
<?php
$a=1;
while($a<11){
foreach($_POST as $key){
if(is_array($key))
{foreach($key as $value1=>$value2){
if($value1=$a){
if($_POST='hometeam')$home_id=$value2;
else if($_POST='awayteam')$away_id=$value2;
else if($_POST='day')$day=$value2;
else if($_POST='month')$month=$value2;
else if($_POST='year')$year=$value2;
else if($_POST='hour')$hour=$value2;
else if($_POST='mins')$mins=$value2;}
$date = ($year . "-" . $month . "-" . $day);
$time = (($hour) . ":" . ($mins) . ":00");
$enter_query = "INSERT INTO matches (home_id, away_id, date, time, league_id) VALUES ('$home_id', '$away_id', '$date', '$time', '$league_id'";
if($hour != "00"){
$enter_result = mysql_query($enter_query);
}}}
}
$a++;
}
?>
If anyone can point at where I’m blatantly going wrong here, I’d be immensely grateful. I’m sure it’s not meant to be this hard but I’m flustered and can’t see past what I’ve typed, and I’m not sure my knowledge is accurate as-is.
Running a print_r on the array gives out the following structure:
Array ( [area] => 1 [season] => 2 [division] => 1 [hometeam]
=> Array ( [1] => 17 [2] => 2 [3] => 12 [4] => 17 [5] => 17
[6] => 17 [7] => 17 [8] => 17 [9] => 17 [10] => 17 ) [awayteam]
=> Array ( [1] => 6 [2] => 4 [3] => 10 [4] => 17 [5]
where the same 10fold structure continues with arrays indexed as hometeam, awayteam, day, month, year, hour, mins.
$_POSTis an associative array so its values are accessed through key names, for example$_POST['hometeam']. This would contain an array of each of thehometeam[]values.The second thing I see is that you need to remember that the = operator is for assigning a value, and == is for comparison. So if ($value = $a) would assign $a to $value and return the result of that, not a logical test.
You should considered replacing the big chain of if/else with a select block. But that’s optional and I’ve left that as is from your example.
My final suggestion is that whist you don’t have to have whitespace, it does make it a lot easier to review code when it’s not working properly.
I haven’t tested this code but this is how I would rework what you have.
I hope this gets you on the right track at least.
EDIT: I’ve refactored this down a bit. It doesn’t do any checking of what is returned on the form (which is a security issue) but that’s outside the scope of this question..
I can’t see where league_id comes from. If it is a part of $_POST you can reference it in the same way (if it is not an array then don’t use the [$a]).