Okay i realise this is going to be really poor coding, but i'm having problems with it an wondering if anyone can help, what happens is i put in names into the text boxes and i want it to add to the User and Results table, but at the moment i either get about the for loop error sayin its invalid or i only get it adding the first username or the last username it's never all of them.
<?php
if ($_SERVER['REQUEST_METHOD'] == "POST")
{
$newresult = mysql_result(mysql_query("SELECT max(result_id) AS newr FROM Results"), 0, "newr") + 1;
$now = time();
foreach($rank as $pos=>$nick)
{
$nick = ($nick == "" ? $rank2[$pos] : $nick);
if (empty($nick))
$missing++;
if ($missing)
{
echo "Missing usernames for some positions";
die();
}
}
foreach($rank as $pos=>$nick)
{
$nick = ($nick == "" ? $rank2[$pos] : $nick);
$nick = trim($nick);
if ($nick == "")
{
echo("Missing username for position $pos.");
die();
}
}
{
$nick = mysql_escape_string($nick);
$user_query = @mysql_query("SELECT * FROM User WHERE name like '$nick'") or die("Error executing mysql query: " . mysql_error());
if(@mysql_num_rows($user_query) == 1)
{
$row = @mysql_fetch_assoc($user_query);
$user_id = $row['user_id'];
}
else
{
$new_user_query = @mysql_query("INSERT INTO User (user_id, name) VALUES ('', '$nick')") or die("Error executing mysql query: " . mysql_error());
$user_id = @mysql_insert_id();
}
$add_result_query = @mysql_query("INSERT INTO Results (result_id, user_id, rank, time) VALUES ('$newresult', '$user_id', '$pos', '$now')") or die("Error executing query: " . mysql_error());
}
print "Results added successfully.<br />";
exit;
}
$useroptions = "<option value=\"\">--</option>";
$username_query = @mysql_query("SELECT * FROM User ORDER BY name ASC") or die ("Error executing query: " . mysql_error());
while($row == @mysql_fetch_assoc($username_query))
$useroptions .= "\n<option value=\"$row[name]\">$row[name]</option>";
?>
<form action="<?=$_SERVER['PHP_SELF']?>" method="POST">
<table border="0" cellpadding="5" cellspacing="0" width="95%">
<?php
for ($i = 1; $i <= 5; $i++)
{
?>
<tr>
<td>
Position <?=$i?>:
</td>
<td>
<input type="text" name="rank[<?=$i?>]" size="20" />
</td>
<td>
<i>or</i>
</td>
<td>
<select name="rank2[<?=$i?>]">
<?=$useroptions?>
</select>
</td>
</tr>
<?php
}
?>
<tr>
<td colspan="2" align="center">
<input type="hidden" name="process" value="yes">
<input type="submit" value="Add race" />
</td>
</tr>
</table>
</form>