把信息不起作用

时间:2014-03-25 17:30:06

标签: php html mysql sql

我有一个"赛车"我正在记录赛车手成绩的网站,但我试图一次性输入所有结果。如下所示是代码

1 个答案:

答案 0 :(得分:1)

你的问题是你相信当你有多个<input name="" />你认为创建一个数组$_POST[]时,这不是真的...

while ($row = mysql_fetch_assoc($result))
{

    $reID = $row['reID'];
    $racerID = $row['racerID'];

    echo "<tr>";
    echo "<td>$reID<input type='hidden' name='reID' value='$reID'>"; 
    echo "<td>$racerID<input type='hidden' name='racerID' value='$racerID'>"; 

    echo"<td><input type='text' name='rank'>";
    echo"<td><input type='text' name='timetaken'>";   

}

你每次打印时都会写出reID,racerID,等级和时间......默认情况下它会占用最后一个。

你可以做的是解决这个问题:

$count = 0;
while ($row = mysql_fetch_assoc($result))
{
    $count++;

    $reID = $row['reID'];
    $racerID = $row['racerID'];

    echo "<tr>";
    echo "<td>$reID<input type='hidden' name='reID$count' value='$reID'>"; 
    echo "<td>$racerID<input type='hidden' name='racerID$count' value='$racerID'>"; 

    echo"<td><input type='text' name='rank$count'>";
    echo"<td><input type='text' name='timetaken$count'>";   

}
echo "<input type='hidden' name='count' value='$count' />";

这样每个值都是唯一的......

然后代替:

$rank=$_POST['rank'];
$timetaken=$_POST['timetaken'];
$reID=$_POST['reID'];
$racerID=$_POST['racerID'];
$count_racerID= count($_POST['racerID']);

这样做

$count = $_POST['count'];
for($i=1; $i<=$count; $i++){
    $rank[] = $_POST['rank'.$i];
    $timetaken[] = $_POST['timetaken'.$i];
    $reID[] = $_POST['reID'.$i];
    $racerID[] = $_POST['racerID'.$i];
}
$count_racerID = $count;

现在你有阵列了!

对于您的SQL语句......

$sql = mysql_query("INSERT INTO RaceResults (rank, timetaken, reID, racerID) VALUES ('$rank', '$timetaken', '$reID', '$racerID')");

应该是

$sql = mysql_query("INSERT INTO RaceResults (rank, timetaken, reID, racerID) VALUES ('$_rank', '$_timetaken', '$_reID', '$_racerID')");

最终产品应该是这样的:

输入表格:

$reID = $_GET['reID'];
$result = mysql_query("SELECT * FROM RaceEventRacer WHERE reID = $reID");

$count = 0;
while ($row = mysql_fetch_assoc($result))
{
    $count++;

    $reID = $row['reID'];
    $racerID = $row['racerID'];

    echo "<tr>";
    echo "<td>$reID<input type='hidden' name='reID$count' value='$reID'>"; 
    echo "<td>$racerID<input type='hidden' name='racerID$count' value='$racerID'>"; 

    echo"<td><input type='text' name='rank$count'>";
    echo"<td><input type='text' name='timetaken$count'>";   

}
echo "<input type='hidden' name='count' value='$count' />";

并且SQL应该是:

$count = $_POST['count'];
for($i=1; $i<=$count; $i++){
    $rank[] = $_POST['rank'.$i];
    $timetaken[] = $_POST['timetaken'.$i];
    $reID[] = $_POST['reID'.$i];
    $racerID[] = $_POST['racerID'.$i];
}
$count_racerID = $count;

for($i=0;$i<$count_racerID;$i++){
    $_rank= mysql_escape_string($rank[$i]);
    $_timetaken= mysql_escape_string($timetaken[$i]);
    $_reID= mysql_escape_string($reID[$i]);
    $_racerID= mysql_escape_string($racerID[$i]);

    $sql = mysql_query("INSERT INTO RaceResults (rank, timetaken, reID, racerID) VALUES ('$_rank', '$_timetaken', '$_reID', '$_racerID')");
    $result = mysql_query($sql);
}