Hi, i'm tearing my hair out at this, i've got a submission form which is created from a mysql database. This works fine, it retrieves the data and creates fields for user input.
i've got a table called 'games' where the game is either open or closed (open = yes/no). The form picks up which games are still open and creates rows on the page. The user can then enter their predicted scores for each game. So far so good.
Except when they click submit, it only inserts the last game into the 'predictions' table. Firstly it's not counting the number of fields in the post submission, but even when I force it (size_array = 4) it creates 4 records but only the first one has information in it from the last game. Hope i'm making sense!
All the rest of the code is fine, the problem code is case 2: below.
<?
session_start();
require_once($_SERVER['DOCUMENT_ROOT'].'/db_connect.php');
?>
<form action="<?= $_SERVER['PHP_SELF'] .'?step='. ($step+1); ?>" method="post">
<?
$step = isset($_GET['step']) ? $_GET['step'] : 1;
switch ($step) {
case 1:
?>
<?
$res=mysql_query("SELECT * FROM games WHERE open = 'yes'");
if(mysql_num_rows($res)==0) echo "There is no data in the table";
else
for($i=0;$i<mysql_num_rows($res);$i++) {
$row=mysql_fetch_assoc($res);
echo "Home : $row[hteam] ";
?>
<tr>
<td>
<input type="" id="homescore" name="homescore" maxlength="2">
</td>
</tr>
<?
echo "Away: $row[ateam] " ;
?>
<tr>
<td>
<input type="" id="awayscore" name="awayscore" maxlength="2">
</td>
</tr>
<br>
<?
}
?>
<tr>
<td><input type="submit" id="submit" name="submit" value="Submit Your Predictions!"></td>
</tr>
<?
break;
case 2:
//problem code below
$size_array = count($_POST['homescore']);
for ($w=0; $w<$size_array; $w++){
$query = 'INSERT INTO predictions (pred_home, pred_away)'.
" VALUES ('".mysql_real_escape_string($_POST['homescore'][$w])."', '".mysql_real_escape_string($_POST['awayscore'][$w])."')";
$result = mysql_query($query) or die ("Error in query: $query");
}
} ?>
</form>
Any help would be appreciated.
i've got a table called 'games' where the game is either open or closed (open = yes/no). The form picks up which games are still open and creates rows on the page. The user can then enter their predicted scores for each game. So far so good.
Except when they click submit, it only inserts the last game into the 'predictions' table. Firstly it's not counting the number of fields in the post submission, but even when I force it (size_array = 4) it creates 4 records but only the first one has information in it from the last game. Hope i'm making sense!
All the rest of the code is fine, the problem code is case 2: below.
<?
session_start();
require_once($_SERVER['DOCUMENT_ROOT'].'/db_connect.php');
?>
<form action="<?= $_SERVER['PHP_SELF'] .'?step='. ($step+1); ?>" method="post">
<?
$step = isset($_GET['step']) ? $_GET['step'] : 1;
switch ($step) {
case 1:
?>
<?
$res=mysql_query("SELECT * FROM games WHERE open = 'yes'");
if(mysql_num_rows($res)==0) echo "There is no data in the table";
else
for($i=0;$i<mysql_num_rows($res);$i++) {
$row=mysql_fetch_assoc($res);
echo "Home : $row[hteam] ";
?>
<tr>
<td>
<input type="" id="homescore" name="homescore" maxlength="2">
</td>
</tr>
<?
echo "Away: $row[ateam] " ;
?>
<tr>
<td>
<input type="" id="awayscore" name="awayscore" maxlength="2">
</td>
</tr>
<br>
<?
}
?>
<tr>
<td><input type="submit" id="submit" name="submit" value="Submit Your Predictions!"></td>
</tr>
<?
break;
case 2:
//problem code below
$size_array = count($_POST['homescore']);
for ($w=0; $w<$size_array; $w++){
$query = 'INSERT INTO predictions (pred_home, pred_away)'.
" VALUES ('".mysql_real_escape_string($_POST['homescore'][$w])."', '".mysql_real_escape_string($_POST['awayscore'][$w])."')";
$result = mysql_query($query) or die ("Error in query: $query");
}
} ?>
</form>
Any help would be appreciated.