2012-03-10 74 views
1

我正在抓我的頭使這行代碼工作,但沒有成功......如果有人能指出什麼是最佳做法寫這樣的查詢?不能讓mysql查詢使用雙或單qoute工作

if(isset($_POST) && $_POST["id"] > 0) 
{ 
    include('../../config.php');    

    $id = $_POST["id"]; 
    $title = mysql_real_escape_string($_POST["title"]); 
    $desc = mysql_real_escape_string($_POST["desc"]);   

    $cat = $_POST["catid"]; 

    $_DB->Execute("UPDATE gallery_imgs SET title = `$title`, description = `$desc` WHERE id = $id"); 

    header("location: admin.php?mode=images&id=$cat"); 
} 
else 
{ 
    //Other stuff! 
} 

這是我的錯誤:

Error number: 1054 
Error  : Unknown column 'The SIEK!' in 'field list' 

回答

1

首先,你必須叫mysql_real_escape_string()的字符串值,但你也必須驗證的$_POST['id']內容。

// invalid string values will convert to integer 0 
// If that is not allowable, you should not proceed with the query. 
$id = intval($_POST['id']); 

輸入字符串必須在單引號括,未反引號(它被用於列和表標識符);

$_DB->Execute("UPDATE gallery_imgs SET title = '$title', description = '$desc' WHERE id = $id"); 

順便說一句,你應該在重定向頭使用前驗證的$_POST['catid']內容。例如,如果它應該是數字:

// At least cast it to an int if it is an invalid string.... 
$catid = intval($_POST['catid']); 
+0

謝謝!也爲旁註! – 2012-03-10 17:43:00

+0

如果$ title是「'現在不要看!」會發生什麼?哎呀。當然你至少應該提到'mysql_escape_string()'! – TerryE 2012-03-10 19:08:56

+0

@TerryE OP已經在標題和描述中調用了'mysql_real_escape_string()'。看到上面的原始帖子... – 2012-03-10 19:12:16

3

那些不是您在此處使用的單引號。刻度標記`是用於列名的。

2

MySQL使用單引號環繞字符串。喜歡這個: '。

$_DB->Execute("UPDATE gallery_imgs SET title = '$title', description = '$desc' WHERE id = $id"); 

你還應該看看驗證$ id。 (使用is_numeric()或轉換爲int)。

提示:要獲得更健壯,更安全的MySQL解決方案,您應該查看PHP PDO。