2012-03-14 46 views
0

我想更新從數組中獲取的表中的值。我沒有得到任何錯誤,但它並沒有更新我的表ex_test_setting。我究竟做錯了什麼?我跟着其他帖子和谷歌搜索,但我不能得到一個正確的答案。PHP更新數組中的值

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'"); 
} 
+0

那是什麼'<>'? – jmishra 2012-03-14 10:04:05

+0

當你運行'echo $ newpoint'這行時,你有沒有得到任何值?還有什麼'ex_test_setting'表的結構? ..最後,最好寫'SELECT SUM(points_available)AS sum1',這樣你就可以得到'$ totalPoints ['sum1']' – Ashraf 2012-03-14 10:06:12

+1

@ ladiesMan217的值,它通常在PHP中使用,就像'!=' – Ashraf 2012-03-14 10:07:22

回答

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'"); 
} 
+1

你應該解釋你改變了什麼,爲什麼,它也關於它的啓用^^ – MakuraYami 2012-03-14 10:15:03

+0

@ sam_13嘗試了它,並再次遍歷所有的代碼,但它沒有更新,還檢查了ex_test_setting字段是int。 – Wilest 2012-03-14 10:37:47

+0

我認爲我的更新查詢出了問題,因爲我再次刪除並鍵入了它,現在它正在工作 – Wilest 2012-03-14 10:44:47