PHP从数组中更新表中的值

时间:2012-03-14 10:00:48

标签: php

我正在尝试更新从数组中获取的表中的值。我没有收到任何错误,但它没有更新我的表ex_test_setting。我究竟做错了什么?我跟着其他帖子并用谷歌搜索了但我无法得到正确答案。

echo "<strong>Checking if the test points changed</strong>";
echo "</BR>";
$getTotalPoints = mysql_query("SELECT SUM(points_available) FROM ex_question WHERE test_name = '$tid_mod1'") or die(mysql_error()); 
$totalPoints = mysql_fetch_array($getTotalPoints);
echo "New points for test: " . $totalPoints['SUM(points_available)'];
echo "</BR>";

$sql2="SELECT DISTINCT point FROM ex_test_setting WHERE testid = '$tid_mod1'";
$res2=mysql_query($sql2);
while($row2 = mysql_fetch_array($res2)) {
$point1=$row2['point'];
echo "Old points for test: " . $point1;
echo "</BR>";
}


if ($totalPoints['SUM(points_available)'] <> $point1) {
echo "Updating...";
$newpoint = $totalPoints['SUM(points_available)'];
echo $newpoint;
mysql_query("UPDATE ex_test_setting SET point = '$newpoint' WHERE test_name = '$tid_mod1'");
}

1 个答案:

答案 0 :(得分:0)

试试这段代码: -

echo "<strong>Checking if the test points changed</strong>";
echo "</BR>";
$getTotalPoints = mysql_query("SELECT SUM(points_available) as point_av FROM ex_question WHERE test_name = '$tid_mod1'") or die(mysql_error()); 
$totalPoints = mysql_fetch_array($getTotalPoints);
echo "New points for test: " . $totalPoints['point_av'];
echo "</BR>";

$sql2="SELECT DISTINCT point FROM ex_test_setting WHERE testid = '$tid_mod1'";
$res2=mysql_query($sql2);
while($row2 = mysql_fetch_array($res2)) {
$point1=$row2['point'];
echo "Old points for test: " . $point1;
echo "</BR>";
}


if ($totalPoints['point_av'] != $point1) {
echo "Updating...";
$newpoint = $totalPoints['point_av'];
echo $newpoint;
mysql_query("UPDATE ex_test_setting SET point = '$newpoint' WHERE test_name = '$tid_mod1'");
}