Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Not counting or inserting all fields ???

Status
Not open for further replies.

gibblin

Technical User
Oct 9, 2003
27
GB
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.
 
That's because your markup has "homescore" as a single field repeated multiple times. You need to add brackets to the end of the name attribute, like this:
Code:
<input type="" name="homescore[COLOR=red][][/color]"  maxlength="2">
That will cause PHP to populate $_POST['homescore'] with an array of those values, as your code expects. Without the brackets, PHP will treat it as just a single value.
 
thanks for that, but it doesn't work. still have the same problem!
 
do you think you should set the $step variables BEFORE you output the <form> tag that uses the step variable?

without doing so your switch code will always execute only case 1.
 
taking a liberty in a coffee break, i've adapted your code a bit.

Code:
<?
session_start();
require_once($_SERVER['DOCUMENT_ROOT'].'/db_connect.php');
$step = isset($_GET['step']) ? $_GET['step'] : 1;
$nextstep = $step++;
$output = <<<HTML
<form action="{$_SERVER['PHP_SELF']}?step=$nextstep" method="post">
HTML;
switch ($step) {
	case 1:
		$output = '';
		$res=mysql_query("SELECT * FROM games WHERE open = 'yes'") or die (mysql_error());
		if(mysql_num_rows($res)==0){
			$output = "There is no data in the table";
		}else{
			$output .= "<table>\r\n";
			while($row = mysql_fetch_assoc($res)){
				$output .= <<<HTML
	<!---NEXT RECORD--->
	<tr>
		<td>
			Home : {$row['hteam']}
		</td>
	<tr>
		<td>
			<input type="text" id="homescore[]" name="homescore[]"  maxlength="2">
		</td>
	</tr>
	<tr>
		<td>
			Away: {$row['ateam']}
		</td>
	</tr>
	<tr>
		<td>
			<input type="text" id="awayscore[]" name="awayscore[]"  maxlength="2">
		</td>
	</tr>

HTML;
			} //end of while loop
			$output .= <<<HTML
	<!---close the table --->	
	<tr>
		<td>
			<input type="submit" id="submit" name="submit" value="Submit Your Predictions!">
		</td>
	</tr>
</table>
</form>
HTML;
		} //end the if/
		echo $output;
	break; //end the first case
	
	case 2:
		$size_array = count($_POST['homescore']);
		
		//cleanse the data
		array_walk($_POST['homescore'], '_cleanse');
		array_walk($_POST['awayscore'], '_cleanse');
		
		//assemble the query for multiple use
		$query = 'INSERT INTO predictions (pred_home, pred_away) values (%u, %u)';
		for ($w=0; $w<$size_array; $w++){
			$_query = sprintf($query, $_POST['homescore'][$w], $_POST['awayscore'][$w]);
			mysql_query($_query)
				or die ("Error in query: $_query <br/>Error was: ". mysql_error());
		} //end the for loop
		
	break; //end the second class
}

function _cleanse(&$val){
	if (function_exists('mysql_real_escape_string')){
		return mysql_real_escape_string(intval(trim($val)));
	} else {
		return mysql_escape_string(intval(trim($val)));
	}
}
?>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top