I entered as scores -1 to 1, 0 to 0, and a to b for AwayScore and HomeScore, respectively. Here's the output.
string(2) "-1"
string(1) "1"
Game #8 has been updated.
string(1) "0"
string(1) "0"
Please ensure scores are whole numbers only.
string(1) "a"
string(1) "b"
Please ensure scores are whole numbers only.
So -1 to 1 was the only one that worked. Here's my full code for that section. This is in a secure area so I'm less concerned about user input, but I'll probably escape them when they go live anyway.
First, (#B) the code checks to make sure something was input for both scores, so if they are left blank, we don't do anything. Scores will be entered by the VPs and the scores aren't always reported in a timely manner.
Then, (#C) we are supposed to check to see if they are positive integer, so we can't have scores of -27 to 3, or 3.2 to 2.7, or pi to i.
In #D, we update the records in the MySQL table. If it fails (#E), it says to contact me. If it works (#F), it says "Game #NNN updated."
The bottom ELSE (#G) is the ELSE if #C fails.
Code: Select all
foreach($_POST as $array)
{#A
foreach($array as $gamescore)
{#B
#Check to see that something was input
IF($gamescore['AwayScore']!='' && $gamescore['HomeScore']!='')
{#C
#Check to see that scores were positive integers
IF((filter_var($gamescore['AwayScore'], FILTER_VALIDATE_INT, array('Options'=>array('min_range'=>0)))) AND
(filter_var($gamescore['HomeScore'], FILTER_VALIDATE_INT, array('Options'=>array('min_range'=>0)))))
{#D
$scoreupdate = "UPDATE Schedule SET AwayScore = '$gamescore[AwayScore]',
HomeScore = '$gamescore[HomeScore]' WHERE GameID = '$gamescore[GameID]'";
#Update the records
IF(!$update = mysqli_query($cxn, $scoreupdate))
{#E
echo "Couldn't update record.<br />"
."Please contact the WebMaster.";
}#E
#Echo update success
ELSE
{#F
echo "Game #".$gamescore[GameID]. " has been updated.<br />";
}#F
}#D
ELSE
{#G
echo "Please ensure scores are whole numbers only.<br />";
}#G
}#C
}#B
}#A
As far as your "strict comparison" comment, if I'm reading you right, I have three IF statements, and the first one [IF($gamescore['AwayScore']!='' && $gamescore['HomeScore']!='')] is OK but the second one [IF(!$update = mysqli_query($cxn, $scoreupdate))] could be improved, but since it has the = in it, I'm not sure how to re-write it......