$member_id = //users member id in database//
$track = //the track results being updated//
$engine = //the engine id from the members table in database//
$engine_points_system = array();
$engine_points_system["qualpos1"] = 30;
$engine_points_system["qualpos2"] = 20;
$engine_points_system["qualpos3"] = 18;
$engine_points_system["qualpos4"] = 17;
$engine_points_system["qualpos5"] = 16;
$enginepoints = 0;
$qualifyingpoints = 0;
$results_query = mysql_query("SELECT pos_1, pos_2, pos_3, pos_4, pos_5
from engine_qualifying_results WHERE track_id = '$track'")
or die ("Failed to update" . mysql_error());
$row = mysql_fetch_array($results_query);
$enginequalifying = array();
for ($i = 1; $i <= 5; $i++) {
$enginequalifying["pos$i"] = $row['pos_$i'];
}
for($i = 1; $i <=5; $i++) {
if($engine == $enginequalifying["pos$i"]){
$enginepoints += $engine_points_system["qualpos$i"];
$qualifyingpoints += $engine_points_system["qualpos$i"];
}
}
$results_query = mysql_query("INSERT INTO member_results (member_id, engine_points)
VALUES ('$member_id', $enginepoints')")
or die ("Failed to update" . mysql_error());
答案 0 :(得分:2)
$enginequalifying["pos$i"] = $row['pos_$i'];
在这一行中你有'pos_$i'
。这是文字字符串'pos_$i'
。您应该使用"pos_$i"
代替。
$enginequalifying["pos$i"] = $row["pos_$i"];
<强>更新强>:
在您的代码中$enginequalifying
是多余的,不需要。您可以在其位置使用$row
。
for($i = 1; $i <=5; $i++){
if($engine == $row["pos_$i"]){
$enginepoints += $engine_points_system["qualpos$i"];
$qualifyingpoints += $engine_points_system["qualpos$i"];
}
}
另外,作为@ax。指出,您的INSERT中还有额外的'
(或缺少'
)。
$results_query = mysql_query("INSERT INTO member_results (member_id, engine_points)
VALUES ('$member_id', '$enginepoints')")
or die ("Failed to update" . mysql_error());
答案 1 :(得分:2)
看看这段代码:
<?php
$i = 5;
print "i is $i";
print "\n";
print 'i is $i';
?>
你希望它打印出来:
i is 5
i is 5
但相反,它会打印出来:
i is 5
i is $i
这是因为当字符串用单引号括起来时,不会评估$i
。它只是字符串$i
。
要修复代码,请尝试替换此行:
$enginequalifying["pos$i"] = $row['pos_$i'];
这一行:
$enginequalifying["pos$i"] = $row["pos_$i"];
行情有所作为。
顺便说一句, ESCAPE你的SQL !!! 。请?
答案 2 :(得分:2)
不是答案,但是太难以发表评论:您可以绕过整个循环来构建引擎限定数组,只需执行以下操作:
SELECT pos_1 AS pos1, pos_2 AS pos2, etc...
查询,然后只需:
$enginequalifying = mysql_fetch_assoc($result);
当原始查询字符串中的简单as
别名可以完成相同的操作时,为您提供PHP获取/重命名数据库字段会浪费CPU周期。
顺便说一句,这也将删除Rocket在答案中指出的字符串引用错误。
答案 3 :(得分:0)
我不认为可以在不知道数据库中有什么的情况下说出来。
但我可以告诉你,在最后一个SQL查询中有一个语法错误($ enginepoints以引号结尾)。