2012-09-16 82 views
0

我在嘗試更新mysql服務器中的表時遇到了一個奇怪的問題。PHP SQL更新錯誤

代碼:

$name = trim(FilterText($_POST['name'])); 
$description = trim(FilterText($_POST['description'])); 
$type = $_POST['type']; 

if($groupdata['type'] == "3" && $_POST['type'] != "3"){ echo "You may not change the group type if it is set to 3."; exit; } // you can't change the group type once you set it to 4, fool 
if($type < 0 || $type > 3){ echo "Invalid group type."; exit; } // this naughty user doesn't even deserve an settings update 

if(strlen(HoloText($name)) > 25){ 
    echo "Name too long\n\n<p>\n<a href=\"".WWW."/groups/".$groupid."/id\" class=\"new-button\"><b>Done</b><i></i></a>\n</p>\n\n<div class=\"clear\"></div>"; 
} elseif(strlen(HoloText($description)) > 200){ 
    echo "Description too long\n\n<p>\n<a href=\"".WWW."/groups/".$groupid."/id\" class=\"new-button\"><b>Done</b><i></i></a>\n</p>\n\n<div class=\"clear\"></div>"; 
} elseif(strlen(HoloText($name)) < 1){ 
    echo "Please give a name\n\n<p>\n<a href=\"".WWW."/groups/".$groupid."/id\" class=\"new-button\"><b>Done</b><i></i></a>\n</p>\n\n<div class=\"clear\"></div>"; 
} else { 
    mysql_query("UPDATE groups SET name = '".$name."', type = $type, desc='".$description."' WHERE id = $groupid AND ownerid = '".USER_ID."' LIMIT 1") or die(mysql_error()); 
    echo "Editing group settings successful\n\n<p>\n<a href=\"".WWW."/groups/".$groupid."/id\" class=\"new-button\"><b>Done</b><i></i></a>\n</p>\n\n<div class=\"clear\"></div>"; 
} 

在的mysql_query更新組,我不斷收到一個錯誤說,我將DESC = blahblahbla時的一部分,在我的SQL語法錯誤。

當我從查詢中取出「desc」部分,並且只插入名稱和類型時,查詢完美地工作,但是當我將desc添加回查詢時,它會再次拋出錯誤。在desc部分中沒有「 - 」可以填充它,即使有,我也會在代碼開始時對它們進行過濾。

任何幫助將不勝感激。

我在使用CMS的情況下,你想知道

在此先感謝! :)

回答

2

desc是MySQL中的保留字,用於order by子句。你必須用反引號進行轉義:

UPDATE .... `desc`=etc... 
      ^-- ^-- 

和當然,你也得到由幾十好事者跳上誰就會聲稱,使用mysql _ *()函數將導致宇宙爆在5 ...... 4 ...... 3 ......

+0

感謝您的!我沒有點擊「按'desc'」排序。太感謝了! – zuc0001

+0

那些忙碌的人正在進行公共服務。 – pilcrow

0

試試這個

mysql_query("UPDATE `groups` SET `name` = '".$name."', `type` = $type, `desc` = '".$description."' WHERE `id` = '$groupid' AND `ownerid` = '".USER_ID."' LIMIT 1") or die(mysql_error()); 
+0

如果你打算只改變一個長的字符串只有2個字符,你通常應該指出哪裏的變化。我們是人類,我們不會輕易做'差異'。 –