2013-10-14 34 views
0

我試過這個查詢,同時使用逗號和「AND」語句,如下圖所示。我收到語法錯誤PHP MySQL更新設置多個列的查詢

出錯了。您的SQL語法有錯誤;檢查對應於你的MySQL服務器版本的手冊正確的語法在1號線

每次

附近'are available 24/7 by phone and email to answer any questions and to assist you '使用我嘗試此查詢:

$sql = mysql_query("UPDATE general 
    SET bookabandheading = $_POST[bookabandheading 
    AND bookaband = $_POST[bookaband] 
    AND contactus = $_POST[contactus] 
    AND aboutuslisten = $_POST[aboutuslisten] 
    AND contactusheading = $_POST[contactusheading] 
    AND nightclubsheading = $_POST[nightclubsheading] 
    AND acousticheading = $_POST[acousticheading] 
    AND schoolsheading = $_POST[schoolsheading] 
    AND privateheading = $_POST[privateheading] 
    AND concertsheading = $_POST[concertsheading] 
    AND festivalsheading = $_POST[festivalsheading] 
    AND submissions = $_POST[submissions] 
    AND interns = $_POST[interns] 
    AND managementbio = $_POST[managementbio] 
    AND latestnews = $_POST[latestnews] 
    AND artistofthemonth = $_POST[artistofthemonth] 
    AND artistofthemonthphoto = $_POST[artistofthemonthphoto] 
    AND artistofthemonthid = $_POST[artistofthemonthid] 
    AND listentoourartists = $_POST[listentoourartists] 
    AND musicianswanted = $_POST[musicianswanted] 
    AND aboutus = $_POST[aboutus] 
    AND bshowcases = $_POST[bshowcases] 
    AND bandavails = $_POST[bandavails]"); 

查詢在不同的數據庫工作的另一VPS,但我只是遷移服務器,它不再有效。任何幫助都很有幫助!

+0

如果你能在這裏粘貼確切的錯誤信息,這將有助於解決這個問題很多。 – aorcsik

+0

出錯了。你的SQL語法有錯誤;請查看與您的MySQL服務器版本相對應的手冊,以找到正確的語法,以便在第1行「通過電話和電子郵件提供24/7全天候可用以回答任何問題並提供幫助」 – user2880653

+0

此錯誤意味着, $ _POST包含字符串'...可用24/7 ...'等等,並且不允許在sql查詢中使用原始的,未轉義的,未加引號的字符串。 – aorcsik

回答

5

雖然主要的問題是,你錯過bookamandheading後右括號,還是我奉勸你重構比如這個請求是這樣的:

$keys = array("bookabandheading", "bookaband", "contactus", "aboutuslisten", 
       "contactusheading", "nightclubsheading", "acousticheading", 
       "schoolsheading", "privateheading", "concertsheading", 
       "festivalsheading", "submissions", "interns", "managementbio", 
       "latestnews", "artistofthemonth", "artistofthemonthphoto", 
       "artistofthemonthid", "listentoourartists", "musicianswanted", 
       "aboutus", "bshowcases", "bandavails"); 
$set = array(); 
foreach ($keys as $key) { 
    $set[] = sprintf(" %s = '%s' ", $key, mysql_escape_string($_POST[$key])); 
} 
$sql = mysql_query("UPDATE general SET " . implode(", ", $set)); 

這是很容易維護,也更安全一點通過轉義輸入。

更新:添加where語句例如

$where = array(); 
$where[] = sprintf(" some_string = '%s' ", mysql_escape_string($some_string)); 
$where[] = sprintf(" some_integer = %d ", $some_integer); 
$where = " WHERE " . implode(" AND ", $where); 
$sql = mysql_query("UPDATE general SET " . implode(", ", $set) . " " . $where); 
+0

您先生,是對的。更容易維護 – user2880653

+0

是否可以在這種語句中添加where子句? – user2880653

+0

當然,給答案增加了一些例子 – aorcsik

0
$_POST[bookabandheading 

變化

$_POST[bookabandheading] 
+0

即使在更改後仍然存在相同的語法錯誤。 – user2880653

1

我看到三件事情不對的:

    在查詢
  • 原始POST數據 - 至少是用戶mysql_real_escape_string
  • 的參數看起來像字符串應該在他們周圍引號
  • 沒有WHERE選項,所以你會更新的每一行該表
+0

在字符串周圍嘗試引號後更新2個或更多字段時,仍然無法使其工作。 – user2880653

+0

我建議您使用TEXT類型而不是VARCHAR類型來存儲MySQL數據庫,並將其與textarea作爲表單而不是文本框,這甚至可以保留數據中的空格。 – KarlosFontana

+0

不知何故關鍵字AND不適用於更新語句,而是使用komma代替:UPDATE TableName SET column1 ='value'AND column2 ='value2'WHERE someColumn ='some_value';不起作用。 寧可使用:UPDATE tableName SET column1 ='value',column2 ='value2'WHERE someColumn ='some_value'; – KarlosFontana

1

你有幾個誤區:

  • 語法錯誤。更改

    $ _ POST [bookabandheading$ _ POST [bookabandheading]

  • 這也是令人難以置信的容易出現SQL注入。你應該使用mysqli,但是如果你在mysql上設置(從5.5.0開始不推薦使用),你應該使用mysql_real_escape_string()來轉義每個$ _POST變量。

  • 每個$ _POST變量都需要使用引號進行參數化。因此,一個例子:

    $ _ POST [「bookabandheading」](這樣做的所有$ _ POST變量)

+0

我將連接字符串切換到mysqli,它現在可以工作... – user2880653

+0

我想知道你是如何從這個答案中得到這個想法的。 – aorcsik