For Each Problem
Hi Guys,
Finally getting my head around this. I’m trying to create a script where I’m using a foreach statement to post various data in the database. And using the script below this works:
PHP Code:<?php
if(isset($_REQUEST['Submit']))
{
// Get global values to post into database
$team_id = $_GET['team_id'];
$lastinsert = '1';
// Values to be inserted into reports table
$player_id = $_POST['player_id'];
// IF i'm not getting data then insert into various tables
if(!isset($_GET['report_id']))
{
// Values to be inserted into goals table
foreach($_REQUEST['r'] as $row)
{
mysql_query("INSERT INTO `stats` ( `player_id`,`team_id`, `report_id` ) VALUES ('".$row['player_id']."', '".(int)$_GET['team_id']."', '".$lastinsert."')") OR DIE(mysql_error());
}
}
else
{
$query_one = mysql_query("Update `stats` set player_id='$player_id', team_id='$team_id', report_id='$report_id' where report_id=".$_GET['report_id']);
$msg = "Updated";
}
} //End of POST submit
if(isset($_GET['report_id']))
{
//GET data from Reports Table
$query_one = mysql_query("Select * From goals where report_id=".$_GET['report_id']);
$report_id = $row['report_id'];
$player_id = $row['player_id'];
$team_id = $row['team_id'];
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<form name="reports" method="post" action="">
<?php echo $query_one; ?>
<table width="100%" border="0" cellspacing="1" cellpadding="1">
<tr>
<td width="14%"><font size="1" face="Verdana, Arial, Helvetica, sans-serif"><strong>Goalkeeper//:</strong></font></td>
<td width="86%"> <span class="next">
<?php $p_id = $player_id ? $player_id : ''; ?>
<select class="input" name="r[1]player_id " size="1" style="width: 145" tabindex="1">
<option value="" <?php echo ($p_id == '' ? 'selected="selected"' : ''); ?>>Goalscorer</option>
<?php
$type_array=get_player_names();
foreach ($type_array as $players)
{
print("<option value=\"".$players['player_id']."\""
. ($p_id == $players['player_id'] ? 'selected="selected"' : '')
. ">".$players['player_name']."</option>\n");
}
?>
</select>
</span> </td>
</tr>
<tr>
<td> </td>
<td><span class="next">
<?php $p_id = $player_id ? $player_id : ''; ?>
<select class="input" name="r[2]player_id" size="1" style="width: 145" tabindex="1">
<option value="" <?php echo ($p_id == '' ? 'selected="selected"' : ''); ?>>Goalscorer</option>
<?php
$type_array=get_player_names();
foreach ($type_array as $players)
{
print("<option value=\"".$players['player_id']."\""
. ($p_id == $players['player_id'] ? 'selected="selected"' : '')
. ">".$players['player_name']."</option>\n");
}
?>
</select>
</span></td>
</tr>
</table>
<input type="submit" name="Submit" value="Submit" class="button">
<input type="reset" name="Reset" value="Reset" class="button">
</form>
</body>
</html>
However I have two questions
1)How do I use foreach to get the row for each of the dropdowns where report_id=1 from the database (2 records) and place the first player_id value in the database in the first dropdown, and the second player_id value in the second dropdown?
2) Once they are then placed in each dropdown how can I then UPDATE this data for each row in the database accordingly?
Thanks
Chris






Bookmarks