Jump to content

Coding help with unlimited number of input fields on a form


guestabc1

Recommended Posts

Hi I am trying to create a page where football results can be input within one form for all the matches for that weekend. I need a little help with processing this form. Here's what I have so far:

 

This is displaying the form showing the the home team, away team, date of the match and input fields for the home and away scores.

if( $total_rows > 0 )
{
	for($rows = 0; $rows < $total_rows; $rows++)
	{
		$row_array = mysql_fetch_array($query_result);
		$date = date("Y-m-d");		
		print('<tr>');
		print('<td class="TableBody">'.$row_array[$FIXTURE_RESULTS__HOME_TEAM_NAME].'</td>');

		print('<td class="TableBody" align="center"><input class="FormField" type="text" name="home_score_'.$row_array[$FIXTURE_RESULTS__FIXTURE_ID].'" value="'.$_SESSION['f_team_name'].'" size="1" maxlength="10" /></td>');
		print('<td class="TableBody" align="center">vs</td>');
		print('<td class="TableBody" align="center"><input class="FormField" type="text" name="away_score_'.$row_array[$FIXTURE_RESULTS__FIXTURE_ID].'" value="'.$_SESSION['f_team_name'].'" size="1" maxlength="10" /></td>');
		print('<td class="TableBody">'.$row_array[$FIXTURE_RESULTS__AWAY_TEAM_NAME].'</td>');
		print('<td class="TableBody">'.format_date_ddmmyyyy($row_array[$FIXTURE_RESULTS__MATCH_DATE]).'</td>');
		print('</tr>');
	}
}

 

The following code is processing the form to check that all scores have been entered when save is pressed:

if(isset($_POST['SaveButton']) && $_POST['SaveButton'] == 'Save')
{
	// Build SQL string
	$sql  = 'SELECT fixture_result_id, home_team, home_score, away_team, away_score, date ';
	$sql .= 'FROM fixture_results ';
	$sql .= 'WHERE league_id = "'.$league_id.'" AND (date < NOW() AND home_score IS NULL AND away_score IS NULL) ORDER BY date ASC ';

	// Execute SQL statement
	$query_result = mysql_query($sql,$db_connection);

	if(!$query_result)
	{
		mysql_close($db_connection);
		print('<p>Sorry, there was a database error.</p>');
		include('../../scripts/admin/sc_admin_finish_page.php');
		exit();
	}

	$total_rows = mysql_num_rows($query_result);

	// Save was pressed
	for($rows = 0; $rows < $total_rows; $rows++)
	{
		$row_array = mysql_fetch_array($query_result);

		// Validate input
		$_SESSION['f_error_msg'] = '';

		// Check Score
		if($_SESSION['f_error_msg'] == '')
		{
			if($_POST['home_score_'.$row_array[$FIXTURE_RESULTS__FIXTURE_ID]] == '')
				$_SESSION['f_error_msg'] = 'Error - Please enter all home scores';
			else if ($_POST['away_score_'.$row_array[$FIXTURE_RESULTS__FIXTURE_ID]] == '')
				$_SESSION['f_error_msg'] = 'Error - Please enter all away scores';

		}
	}

 

Then if the session variable is = '' basically there's not errors so the following script is executed where the scores are meant to get updated in the database.

else
	{
		// No error - update the database
				// Build SQL string
		$sql  = 'SELECT fixture_result_id, home_team, home_score, away_team, away_score, date ';
		$sql .= 'FROM fixture_results ';
		$sql .= 'WHERE league_id = "'.$league_id.'" AND (date < NOW() AND home_score IS NULL AND away_score IS NULL) ORDER BY date ASC ';

		// Execute SQL statement
		$query_result = mysql_query($sql,$db_connection);

		if(!$query_result)
		{
			mysql_close($db_connection);
			print('<p>Sorry, there was a database error.</p>');
			include('../../scripts/admin/sc_admin_finish_page.php');
			exit();
		}

		$total_rows = mysql_num_rows($query_result);

		// Save was pressed
		for($rows = 0; $rows < $total_rows; $rows++)
		{


			// Start database transaction
			$transaction_error = false;
			$sql = 'START TRANSACTION';
			$db_query_result = mysql_query($sql,$db_connection);
			if(!$query_result)
				$transaction_error = true;

			// Update query on table fixture_results
			$sql  = 'UPDATE fixture_results ';
			$sql .= 'SET ';
			$sql .= 'home_score = "'.$_POST['home_score_'.$row_array[$FIXTURE_RESULTS__FIXTURE_ID]].'", ';
			$sql .= 'away_score = "'.$_POST['away_score_'.$row_array[$FIXTURE_RESULTS__FIXTURE_ID]].'" ';
			$sql .= 'WHERE fixture_result_id = "'.$row_array[$FIXTURE_RESULTS__FIXTURE_ID].'"';

			$query_result = mysql_query($sql, $db_connection);

			if(!$query_result)
				$transaction_error = true;



			if(!$transaction_error)
				$sql = 'COMMIT';
			else
				$sql = 'ROLLBACK';

			$query_result = mysql_query($sql, $db_connection);

			if(!$query_result)
				$transaction_error = true;

		if($transaction_error == true)
		{
			mysql_close($db_connection);
			print('<p>Sorry, there was a database error.<br />Please try again later.</p>');
			include('../../scripts/admin/sc_finish_admin_page.php');
			exit();
		}

		// Go back to league page
		print('<script type="text/javascript">
					location.href="admin_league.php?ob='.$order_by.'&so='.$sort_order.'&r='.$record.'"
					</script>');
	}
}

 

This is not working do I need to store the posts into some kind of array? I'm lost with this and any help would be appreciated! thanks in advance!

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.