2013-03-25 27 views
1

我運行下面的查詢的價值,通過PHP的mysqli $con->query($sql);UPDATE DB SET ...填充一個具體的VARCHAR 0或1,無論我設置

PHP代碼:

$sql = 'UPDATE registrations SET '.$update_columns_list.' WHERE registrationkey = "'.$registration['RegistrationKey'].'"'; 
echo "RESULT: ".$result = $con->query($sql); 

價值在上面的例子中$sql

UPDATE registrations SET referenceid = "4469731" and action = "newregistration" and status = "newregistration" and registrationdatetimegmt = "1363371386" and billingmonth = "2013-03" and accessexpirationdatetimegmt = "1373957999" and returnurl = "https://api.mywebsite.com/login.aspx?siteid=209" and lastmodifieddatetimegmt = "1363371398" and studentkey = "12345-67890-12345-67890" and firstname = "amanda" and lastname = "hemingway" and email = "[email protected]" and phonenumber = "111-111-1111" and city = "city" and state = "ca" and postalcode = "90210" and country = "us" and coursecode = "ABC-406" and coursetitle = "example course title" and courseproductcode = "t3310" WHERE registrationkey = "12345-67890-12345-67890" 

一切似乎都很好,當我運行此查詢(我得到一個「成功」的消息和受影響的行數與預期)。但是,當我通過phpMyAdmin查看數據庫中受影響的行時,我在「referenceid」列中不斷找到1或0值。沒有其他腳本影響此表或數據庫。我一直無法確定何時放置0而不是1的任何模式。

referenceidkeyid這兩個字段都是VARCHAR(100)'s。

+0

是完整的更新語句正在執行? – 2013-03-25 14:54:51

+1

查詢中的php代碼是什麼? – Waygood 2013-03-25 14:56:38

+0

用逗號代替'和' – Alex 2013-03-25 14:59:08

回答

3

您正在執行的更新語句執行布爾操作。這是我不喜歡的mysql中的行爲。

當前的更新語句如下所示:

UPDATE tableName SET col1 = 2 AND col2 = 3 

MySQL服務器,因爲語法允許不拋出任何異常,並把它讀成:

UPDATE tableName SET col1 = (2 AND (col2 = 3)) 

如果你想更新多個列,請使用,分隔不是AND的列。

UPDATE registrations 
SET referenceid = "4469731", 
     action = "newregistration", ..... 
+0

謝謝,J W,您的解決方案工作。 – bwright 2013-03-25 15:02:03

+0

我更新了答案。 ':)' – 2013-03-25 15:02:54

+0

謝謝!導致我原來的問題的邏輯缺陷現在更加清晰。 – bwright 2013-03-25 16:50:37

1

當你更新你不加列,而不是逗號,所以你的更新應該是這樣的:

UPDATE registrations 
SET referenceid = "4469731", 
    action = "newregistration", 
    status = "newregistration", 
    registrationdatetimegmt = "1363371386", 
    billingmonth = "2013-03", 
    accessexpirationdatetimegmt = "1373957999", 
    returnurl = "https://api.mywebsite.com/login.aspx?siteid=209", 
    lastmodifieddatetimegmt = "1363371398", 
    studentkey = "12345-67890-12345-67890", 
    firstname = "amanda", 
    lastname = "hemingway", 
    email = "[email protected]", 
    phonenumber = "111-111-1111", 
    city = "city", 
    state = "ca", 
    postalcode = "90210", 
    country = "us", 
    coursecode = "ABC-406", 
    coursetitle = "example course title", 
    courseproductcode = "t3310" 
WHERE registrationkey = "12345-67890-12345-67890" 
相關問題