我有一個名爲projects的SQL數據庫,其中包含字段post_id,feature_image,order和landscape。風景是所有密集目的的一個類別。PHP在While while循環中更新SQL命令
作爲我試圖達到的功能的廣泛概述;
頁面加載 1個記錄集將所有分類的post_id#(1,2,4,5,8,12 ...等)和重新編號具體爲1,2,3,4,5 ...等 爲每個post_id#重新編號然後將更新發送到一個字段中,該字段被稱爲order,它將採用這些值。 然後2個記錄集(一個奇數,一個偶數)加載的不是post_id,而是訂單#和所有相關信息(在本例中是精選圖像),並根據任何類別在屏幕上輸出兩列圖像,被加載。
我明白我可以通過post_id訂購,即不是的問題。我特別希望能夠在頁面加載時將所有數字重新排列爲1,2,3,4,5,6等數字。
我只發佈if語句的第一部分,因爲我知道它不是那個混亂的部分。一切工作與變量等,唯一的問題是我不能得到darn變量發送到字段順序下的數據庫。我在這方面工作了太久,不知道我現在做錯了什麼。沒有錯誤,只是訂單字段在數據庫中保持爲NULL。
我會爲任何可以修復此代碼的人拋出一個tickertape遊行,或者告訴我爲什麼我正在做所有的低音贊助,並且有更好的方法來做到這一點。
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
if (PHP_VERSION < 6) {
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
}
$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}
mysql_select_db($database_connTest, $connTest) or die("cannot select database");
$query_RS_Odd = "SELECT projects.post_id, projects.feature_image, projects.`order` FROM projects WHERE projects.landscape = 'landscape'";
$RS_Odd = mysql_query($query_RS_Odd, $connTest) or die(mysql_error());
$row_RS_Odd = mysql_fetch_assoc($RS_Odd);
$totalRows_RS_Odd = mysql_num_rows($RS_Odd);
?>
<?php do {
if ($row_RS_Odd['post_id'] == 1){
$resort_next = 1;
mysql_query("UPDATE projects SET order=".$resort_next." WHERE post_id=".$row_RS_Odd['post_id']."");
echo $row_RS_Odd['post_id'];
echo $resort_next;
}
else {
echo 'No records found';
}
} while ($row_RS_Odd = mysql_fetch_assoc($RS_Odd)); ?>
它不斷更新'post_id' 1,這是預期的嗎? –
是的,在這個簡單的情況下,它應該像其他人一樣,如果陳述(不包括在此)我有它繼續其他的。我保持簡單只是爲了查看它是否會更新SQL表。不幸的是,post_id#1的字段順序仍然爲NULL或0 –
另外,作爲以下協助,我發現SQL查詢確實有錯誤「您的SQL語法中有錯誤;請檢查與您的對應的手冊MySQL服務器版本的正確語法使用附近''修改'= 1 WHERE post_id = 1'在第1行「但我不知道爲什麼 –