homepage Welcome to WebmasterWorld Guest from 54.145.183.169
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
update form array not working
update to two tables
catalepticstate



 
Msg#: 4199464 posted 3:45 pm on Sep 9, 2010 (gmt 0)

Hi,

My update form array does not work.



I am trying to upload to two tables: matches and matchscores



I need to upload to matchsores twice as it must hold information for two teams per match_id



This is my HTML
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td><table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>

<td height="25" bgcolor="#000000" class="tableTextHeading"><div align="center">Date</div></td>
<td height="25" bgcolor="#000000" class="tableTextHeading"><div align="center">Home</div></td>
<td height="25" bgcolor="#000000" class="tableTextHeading"><div align="center">Score</div></td>
<td height="25" bgcolor="#000000" class="tableTextHeading"><div align="center">Away</div></td>
<td height="25" bgcolor="#000000" class="tableTextHeading"><div align="center">Kick-Off</div></td>
<td height="25" bgcolor="#000000" class="tableTextHeading"><div align="center">Venue</div></td>
<td height="25" bgcolor="#000000" class="tableTextHeading"><div align="center">Referee</div></td>
<td height="25" bgcolor="#000000" class="tableTextHeading"><div align="center">Update</div></td>
</tr>

<?php do { ?>
<form action="<?php echo $editFormAction; ?>" method="POST" name="fixtures">
<tr>

<td><div align="center"><span id="sprytextfield1">
<label>
<input name="date[]" type="text" class="date" size="8" style="text-align:center" value="<?php echo $row_match_fixtures['shortDate']; ?>" />
<span class="textfieldRequiredMsg">A value is required.</span>
</label>
</span></div></td>
<td><div align="center"><a href="match-player.php?id=<?php echo $row_match_fixtures['match_id']; ?>&team=<?php echo $row_match_fixtures['team1_id']; ?>"><?php echo $row_match_fixtures['team1_name']; ?></a></div></td>
<td><div align="center"><input name="s1[]" type="text" value="<?php echo $row_match_fixtures['score1']; ?>" size="1" style="text-align:center" />
v <input name="s2[]" type="text" value="<?php echo $row_match_fixtures['score2']; ?>" size="1" style="text-align:center" />
</div></td>
<td><div align="center"><a href="match-player.php?id=<?php echo $row_match_fixtures['match_id']; ?>&team=<?php echo $row_match_fixtures['team2_id']; ?>"><?php echo $row_match_fixtures['team2_name']; ?></a></div></td>
<td><div align="center"><span id="sprytextfield2">
<label>
<input name="time[]" type="text" size="3" value="<?php echo $row_match_fixtures['time']; ?>" style="text-align:center" />
</label>
<span class="textfieldInvalidFormatMsg">Invalid format.</span><span class="textfieldRequiredMsg">A value is required.</span></span></div></td>
<td><div align="center"><span id="spryselect1">
<label>
<select name="venue[]">
<?php
do {
?>
<option value="<?php echo $row_venue['venue_id']?>"<?php if (!(strcmp($row_venue['venue_id'], $row_venue['venue_name']))) {echo "selected=\"selected\"";} ?>><?php echo $row_venue['venue_name']?></option>
<?php
} while ($row_venue = mysql_fetch_assoc($venue));
$rows = mysql_num_rows($venue);
if($rows > 0) {
mysql_data_seek($venue, 0);
$row_venue = mysql_fetch_assoc($venue);
}
?>
</select>
</label>
<span class="selectRequiredMsg">Please select an item.</span></span></div></td>
<td><div align="center"><span id="spryselect2">
<label>
<select name="referee[]">
<?php
do {
?>
<option value="<?php echo $row_referee['ref_id']?>"<?php if (!(strcmp($row_referee['ref_id'], $row_referee['sname']))) {echo "selected=\"selected\"";} ?>><?php echo $row_referee['fname']?> <?php echo $row_referee['sname']?></option>
<?php
} while ($row_referee = mysql_fetch_assoc($referee));
$rows = mysql_num_rows($referee);
if($rows > 0) {
mysql_data_seek($referee, 0);
$row_referee = mysql_fetch_assoc($referee);
}
?>
</select>
</label>
<span class="selectRequiredMsg">Please select an item.</span></span></div></td>

<td><div align="center"><input name="Submit[]" type="submit" id="Submit" value="Save" /></div></td>


</tr>
<input name="match_id[]" type="hidden" value="<?php echo $row_match_fixtures['match_id']; ?>" />
<input name="mscore1[]" type="hidden" value="<?php echo $row_match_fixtures['mscore1']; ?>" />
<input name="mscore2[]" type="hidden" value="<?php echo $row_match_fixtures['mscore2']; ?>" />
<input name="team1_id[]" type="hidden" value="<?php echo $row_match_fixtures['team1_id']; ?>" />
<input name="score1[]" type="hidden" value="<?php echo $row_match_fixtures['score1']; ?>" />
<input name="team2_id[]" type="hidden" value="<?php echo $row_match_fixtures['team2_id']; ?>" />
<input name="score2[]" type="hidden" value="<?php echo $row_match_fixtures['score2']; ?>" />

<input type="hidden" name="MM_update" value="fixtures" />
</form>
<?php } while ($row_match_fixtures = mysql_fetch_assoc($match_fixtures)); ?>

</table></td>
</tr>
</table>


This is the SQL:
$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "fixtures")) {
$updateSQL = sprintf("UPDATE matches SET `date`=%s, `time`=%s, referee_id=%s, venue_id=%s WHERE match_id=%s",
GetSQLValueString($_POST['date'], "date"),
GetSQLValueString($_POST['time'], "text"),
GetSQLValueString($_POST['referee'], "int"),
GetSQLValueString($_POST['venue'], "int"),
GetSQLValueString($_POST['match_id'], "int"));

$updateSQL = sprintf("UPDATE matchscores SET match_id=%s, team=%s, score=%s WHERE matchscores_id=%s",
GetSQLValueString($_POST['match_id'], "int"),
GetSQLValueString($_POST['team1_id'], "int"),
GetSQLValueString($_POST['score1'], "int"),
GetSQLValueString($_POST['mscore1'], "int"));

$updateSQL = sprintf("UPDATE matchscores SET match_id=%s, team=%s, score=%s WHERE matchscores_id=%s",
GetSQLValueString($_POST['match_id'], "int"),
GetSQLValueString($_POST['team2_id'], "int"),
GetSQLValueString($_POST['score2'], "int"),
GetSQLValueString($_POST['mscore2'], "int"));

mysql_select_db($database_db, $db);
$Result1 = mysql_query($updateSQL, $db) or die(mysql_error());

$updateGoTo = "fixtures-edit.php";
if (isset($_SERVER['QUERY_STRING'])) {
$updateGoTo .= (strpos($updateGoTo, '?')) ? "&" : "?";
$updateGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $updateGoTo));
}


The query to display the information is taken from this query:
mysql_select_db($database_db, $db);
$query_match_fixtures = "select m.match_id, date_format(m.date, '%W %D %M %Y') as mDate, date_format(m.date, '%d/%m/%Y') as shortDate, m.time, t1.team_id as team1_id, t1.division, m.report, t1.team_name as team1_name, s1.score as score1, t2.team_id as team2_id, t2.team_name as team2_name, s2.score as score2, v.venue_name, r.fname, r.sname, s1.matchscores_id as mscore1, s2.matchscores_id as mscore2
from matches m left join (matchscores s1 left join team t1 on t1.team_id = s1.team) on (s1.match_id = m.match_id) left join (matchscores s2 left join team t2 on t2.team_id = s2.team) on (s2.match_id = m.match_id)
LEFT JOIN referee r ON r.ref_id = m.referee_id LEFT JOIN venue v ON v.venue_id = m.venue_id
where s1.team <> s2.team
group by match_id
order by m.match_id";
$match_fixtures = mysql_query($query_match_fixtures, $db) or die(mysql_error());
$row_match_fixtures = mysql_fetch_assoc($match_fixtures);
$totalRows_match_fixtures = mysql_num_rows($match_fixtures);


Where am I going wrong?

 

catalepticstate



 
Msg#: 4199464 posted 1:47 pm on Sep 13, 2010 (gmt 0)

anyone?

nickelman



 
Msg#: 4199464 posted 11:03 pm on Sep 19, 2010 (gmt 0)

You don't have your query set up as an array and you need a counter.

In your query:
$row_match_fixtures[] = mysql_fetch_assoc($match_fixtures);

add a counter outside the loop. An array has to start with a zero for the first loop or it won't work correctly:
$count = 0;

then inside the loop
<input name="match_id[]" type="hidden" value="<?php echo $row_match_fixtures[$count]['match_id']; ?>" />

at the end of every loop increment the counter:
$count = $count +1;

I stopped using that canned Dreamweaver code eons ago, so I'm not sure how to edit it. I know that when updating the db without it the sql would look something like this (from one of my pages, not yours):

mysql_select_db($database_reports, $reports);
$game_id[] = ($_POST['game_id']);
$game_count[] = ($_POST['game_count']);
$detail_count = count($_POST['game_id']);
$pull_date = ($_POST['pull_date']);

echo "Please Wait While Records Are Being Updated<br>";

if ( is_array($_POST['game_count'] ) ) {
for ($i = 0; $i < $detail_count; $i++) {
if (isset($game_count[$i]) && $game_count[$i] <> "") {

$updateSQL = mysql($database_games, "UPDATE game_earnings SET earnings = " . $game_count[$i] . " WHERE date = '$pull_date' AND game_id = " . $game_id[$i]) ;
}
}
}
echo "<br>";
echo "Update complete.";

Hope this helps

catalepticstate



 
Msg#: 4199464 posted 4:32 pm on Sep 21, 2010 (gmt 0)

Can I take a look at one of your pages that works like mine?

I am trying to modify my code but I dont know if I am doing it right.

catalepticstate



 
Msg#: 4199464 posted 11:51 am on Sep 23, 2010 (gmt 0)

This does not work either :'(

$limit = count($match_id);

for($i=0;$i<$limit;$i++) {
if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "fixtures")) {
$updateSQL = sprintf("UPDATE matches SET `date`=%s, `time`=%s, referee_id=%s, venue_id=%s WHERE match_id=%s",
GetSQLValueString($_POST['date[]'], "date"),
GetSQLValueString($_POST['time[]'], "text"),
GetSQLValueString($_POST['referee[]'], "int"),
GetSQLValueString($_POST['venue[]'], "int"),
GetSQLValueString($_POST['match_id[]'], "int"));

mysql_select_db($database_db, $db);
$Result1 = mysql_query($updateSQL, $db) or die(mysql_error());

$updateGoTo = "fixtures-edit.php";
if (isset($_SERVER['QUERY_STRING'])) {
$updateGoTo .= (strpos($updateGoTo, '?')) ? "&" : "?";
$updateGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $updateGoTo));
}
}

nickelman



 
Msg#: 4199464 posted 1:39 pm on Sep 25, 2010 (gmt 0)

This is an overly simplified version:



<?php
mysql_select_db($database_reports, $reports);
$query_game = "SELECT * FROM game_list WHERE type_reports > -1 ORDER BY name_reports";
$game_sel = mysql_query($query_game, $reports) or die(mysql_error());
$game[] = mysql_fetch_assoc($game_sel);
$count = 0; ?>

<form action="earnings_update.php" method="post" name="get_game" id="get_game" >
<table border="1">
<tr>
<td colspan=3>Game Earnings Update<input name="pull_date" type="hidden" value="<?php echo date("Y-m-d"); ?>" id="pull_date"></td>
</tr>
<tr>
<td>Count</td>
<td>Game Name</td>
<td>Enter Value</td>
</tr>
<?php do { ?>
<tr>
<td><?php echo $count + 1; ?></td>
<td><?php echo $game[$count]['name_reports']; ?></td>
<td>
<input name="game_count[]" type="text" size="10" maxlength="10" >
<input name="game_id[]" type="hidden" id="game_id" value="<?php echo $game[$count]['ids'] ?>" ></td>
</tr>

<?php $count = $count + 1; ?>
<?php } while ($game[] = mysql_fetch_assoc($game_sel)); ?>
</table>
<input name="Submit" type="submit" id="Submit" value="Submit" >
</form>


And the insertion code:


<?php
mysql_select_db($database_reports, $reports);
$game_id[] = ($_POST['game_id']);
$game_count[] = ($_POST['game_count']);
$detail_count = count($_POST['game_id']);
$pull_date = ($_POST['pull_date']);

echo "Please Wait While Records Are Being Updated<br>";

if ( is_array($_POST['game_count'] ) ) {
for ($i = 0; $i < $detail_count; $i++) {
if (isset($game_count[$i]) && $game_count[$i] <> "") {

$result = mysql_query ("INSERT INTO game_earnings VALUES (
'{0}',
'{$game_id[$i]}',
'{$game_count[$i]}',
'{$pull_date}')");
}
}
}
echo "<br>";
echo "Update complete."; ?>



Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved