Php / Mysql - 从表单转移的错误变量

时间:2012-02-14 22:21:16

标签: php mysql

我在数据库中有一个非常大的记录,其中包含最多48个用户的记录。由于每个用户都重复了字段(唯一的区别是不同的基数),我创建了一个查询来识别用户1到48的字段。

这非常有效,结果是一个表格,它确实显示了我在db中的内容。但是,如果需要,我希望能够更改每个字段中包含的值,因此我将表创建为输入字段。在每行的末尾有一个OK按钮,如果按下该按钮,则用于更新相关字段。

但是,出于某种原因我无法解释按钮总是引用记录1或48而不是其行所指示的那个(即使同一个$ slot变量在行的其他地方使用并按计划工作)。

页面加载了唯一ID($ target),用于标识结果的确定记录。

我首先定义循环需要工作的次数。

$d=48;
$n=1;

我从DCO_results表中选择了正确的会话:

$dataevent=mysql_query("SELECT year, event, competition, id, session FROM DCO_results WHERE id='$target'", $CONNECTW); 
while ($row=mysql_fetch_row($dataevent)){
$year = "$row[0]";
$event = "$row[1]";
$comp = "$row[2]";
$event_id = "$row[3]";
$session = "$row[4]";

if ($session == "FP") { $session="- Practice"; }
if ($session == "Q") { $session="- Qualifying"; }
if ($session == "Q1") { $session="- Qualifying 1"; }
if ($session == "Q2") { $session="- Qualifying 2"; }
if ($session == "QA") { $session="- Qualifying Aggregate"; }                
if ($session == "R") { $session="- Race"; }
if ($session == "R1") { $session="- Race 1"; }
if ($session == "R2") { $session="- Race 2"; }
if ($session == "T") { $session=""; }


$datacomp=mysql_query("SELECT competition FROM DCO_competitions WHERE id='$comp'", $CONNECTW); 
while ($row=mysql_fetch_row($datacomp))
{
    $competition = "$row[0]";
}

$title = "$year"."&nbsp"."$competition"."&nbsp"."$event"."&nbsp"."$session";
}

我创建了表格的标题:

echo "<p>&nbsp;</p>";
echo "<strong>$title</strong>";
echo "<p>&nbsp;</p>";
echo "<table>";
echo "<tr><th>Pos</th><th width='100'>Driver</th><th>Nat</th><th width='80'>Team</th><th>Laps</th><th width='60'>Engine</th><th>Time</th><th>Tyres</th><th width='60'>Gap</th><th width='40'>Sp. 1</th><th width='40'>Sp. 2</th><th width='40'>Sp. 3</th><th width='40'>Fastest</th><th width='30'>P/S</th><th></th></tr>";

现在用数据填充表的循环。这部分效果很好 - 即我得到的正确数据出现在它意图的位置。 注意:在每行中,每个用户有15个字段,乘以48个用户。这些字段称为“driver1”,“driver1_nationality”,“driver1_team”等,直到“driver48”,“driver48_nationality”为止。它们遵循相同的模式,它们是一个记录中的所有字段。 $ fieldXX变量是通过将前缀“driver”与增量$ slot编号和所需的后缀相结合而创建的,如下所示:

while ($n<=$d) {

$slot = $n;         


$field1="driver"."$slot";
$field2="driver"."$slot"."_nationality";
$field3="driver"."$slot"."_team"; 
$field4="driver"."$slot"."_laps"; 
$field5="driver"."$slot"."_engine";
$field6="driver"."$slot"."_time";
$field7="driver"."$slot"."_tyres";
$field8="driver"."$slot"."_gap"; 
$field9="driver"."$slot"."_split1"; 
$field10="driver"."$slot"."_split2"; 
$field11="driver"."$slot"."_split3"; 
$field12="driver"."$slot"."_fastlap"; 
$field13="driver"."$slot"."_pitstops"; 
$field14="driver"."$slot"."_maxspeed"; 
$field15="driver"."$slot"."_comments"; 

现在为每个驱动程序定义了从1到48的字段,我加载了每个驱动程序的数据。

$datares=mysql_query("SELECT $field1, $field2, $field3, $field4, $field5, $field6, $field7, $field8, $field9, $field10, $field11, $field12, $field13, $field14, $field15 FROM DCO_results WHERE id='$target'", $CONNECTW);
while ($row=mysql_fetch_row($datares))

{

    $data1="$row[0]";
    $data2="$row[1]";
    $data3="$row[2]";
    $data4="$row[3]";
    $data5="$row[4]";
    $data6="$row[5]";
    $data7="$row[6]";
    $data8="$row[7]";
    $data9="$row[8]";
    $data10="$row[9]";
    $data11="$row[10]";
    $data12="$row[11]";
    $data13="$row[12]";
    $data14="$row[13]";
    $data15="$row[14]";
}

我终于把它放在桌子上了。输入字段允许我根据需要编辑从数据库中获取的数据。包含$ slot的第一个单元格是位置。使用提交按钮的最后一个链接到表单操作。

echo "<form action='$RKP/kernel/lib/php_lib/action/AC_Results.php?op=edit&event=$target&slot=$slot' method='post'>";
echo "<tr><td>$slot</td><td><input name='field1' class='input' value='$data1' style='width:110px'></input></td><td><input name='field2' class='input' value='$data2'  style='width:30px'></input></td><td><input name='field3' class='input' value='$data3' style='width:70px'></td><td><input name='field4' class='input' value='$data4' style='width:30px'></td><td><input name='field5' class='input' value='$data5' style='width:60px'></td><td><input name='field6' class='input' value='$data6' style='width:70px'></td><td><input name='field7' class='input' value='$data7' style='width:50px'></td><td><input name='field8' class='input' value='$data8' style='width:50px'></td><td><input name='field9' class='input' value='$data9' style='width:40px'></td><td><input name='field10' class='input' value='$data10' style='width:40px'></td><td><input name='field11' class='input' value='$data11' style='width:40px'></td><td><input name='field12' class='input' value='$data12' style='width:30px'></td><td><input name='field13' class='input' value='$data13' style='width:30px'></td><td><input type='submit' value='OK'></input></td></tr>";

$n++;
}   

echo "</table>";

}

这是页面的结尾,据我所知,它应该有效。数据显示正常,但是当我在做出任何更改后单击“确定”时,操作会出错。即,目标$ RKP / kernel / lib / php_lib / action / AC_Results.php?op = edit&amp; event = $ target&amp; slot = $ slot,它应该替换$ slot,无论我点击的行是什么值,总是有插槽= 1 - 这意味着它是更改的driver1的记录。

以下是操作文件AC_Results.php的代码

$op=$HTTP_GET_VARS['op'];
$event_id=$HTTP_GET_VARS['event'];

$slot=$_GET['slot'];
$data1=$HTTP_POST_VARS['field1'];
$data2=$HTTP_POST_VARS['field2'];
$data3=$HTTP_POST_VARS['field3']; 
$data4=$HTTP_POST_VARS['field4'];
$data5=$HTTP_POST_VARS['field5'];
$data6=$HTTP_POST_VARS['field6'];
$data7=$HTTP_POST_VARS['field7'];
$data8=$HTTP_POST_VARS['field8'];
$data9=$HTTP_POST_VARS['field9'];
$data10=$HTTP_POST_VARS['field10'];
$data11=$HTTP_POST_VARS['field11'];
$data12=$HTTP_POST_VARS['field12'];
$data13=$HTTP_POST_VARS['field13'];
$data14=$HTTP_POST_VARS['field14'];
$data15=$HTTP_POST_VARS['field15'];

$day = gmdate("d");
$month = gmdate("m");
$year = gmdate("Y");


if ($op == "edit")
{

$field1="driver"."$slot";
$field2="driver"."$slot"."_nationality";
$field3="driver"."$slot"."_team"; 
$field4="driver"."$slot"."_laps"; 
$field5="driver"."$slot"."_engine";
$field6="driver"."$slot"."_time";
$field7="driver"."$slot"."_tyres";
$field8="driver"."$slot"."_gap"; 
$field9="driver"."$slot"."_split1"; 
$field10="driver"."$slot"."_split2"; 
$field11="driver"."$slot"."_split3"; 
$field12="driver"."$slot"."_fastlap"; 
$field13="driver"."$slot"."_pitstops"; 
$field14="driver"."$slot"."_maxspeed"; 
$field15="driver"."$slot"."_comments"; 


mysql_query("UPDATE DCO_results SET $field1='$data1', $field2='$data2', $field3='$data3', $field4='$data4', $field5='$data5', $field6='$data6', $field7='$data7', $field8='$data8', $field9='$data9', $field10='$data10', $field11='$data11', $field12='$data12', $field13='$data13', $field14='$data14', $field15='$data15' WHERE id='$event_id'", $CONNECTW);

//header("Location: ../../../../login/admin/results/");

}

整个问题都倾向于$ slot值不正确 - 我相信如果是这样的话,其余的动作代码会正常工作,因为它看起来是一致的。

我无法解释它是如何工作的,所以任何指针都受到赞赏!

0 个答案:

没有答案