2014-03-31 37 views
0

我得到的錯誤是:我嘗試在SQL語句中使用的變量,我已經試過mysql_real_escape_string但仍然無法

致命錯誤:未捕獲的異常「PDOException」有消息「SQLSTATE [42S22]:列未找到:在 'where子句'」

1054未知列 'gz_01' 這是我原來的代碼:

// $universityID would be column name like gz_01, gz_02, wh_01, wh_02...etc... 
    // value in gz_01 column is int type 
    $universityID = "gz_01"; 

    addSubscription($universityID); 

    function addSubscription($university){ 

    Here I skip the codes of connection to SQL 

    // count the subscription 
    $sql = "UPDATE qr_code_count 
      SET $university = $university + 1 
      ";      

    $pdo->exec($sql); 
    } 

但gz_01值不爲每個addSubscription()調用加1。

當我用$ SQL = 「UPDATE qr_code_count SET gz_01 = gz_01 + 1」我的代碼和預期一樣,在gz_01值增加爲每個addSubscription()調用。

我在stackoverflow上搜索了答案,發現使用mysql_real_escape_string()的可能解決方案,但是我的代碼仍然失敗。

任何人都可以給我一個關於我的代碼有什麼問題的指針嗎?謝謝。

+0

1)什麼$大學的精確值是您使用? (只有gz_01?) 2)你有什麼錯誤嗎? – Uriziel

+0

你有沒有嘗試用字符包裝字段名?它可以通過MYSQL引擎以某種尷尬的方式解析'_'字符。 – Uriziel

+0

這可能不在上下文中,但是可以嘗試關閉同一行中的SQL嗎? –

回答

1

所以,你必須看起來數據庫表像這樣用一排?

| z_01 | gz_02 | wh_01 | wh_02 | 
| 0 | 1 | 3 | 4 | 

爲什麼不讓表格變得有意義?

| university_id | qr_code_count | 
|  z_01  |  0   | 
|  gz_02  |  1   | 
|  wh_01  |  3   | 
|  wh_02  |  4   | 

然後你用錯誤的更少變化非常簡單地更新:

function addSubscription($uniId){ 

    $stmt = $dbh->prepare("UPDATE qr_code_count 
          SET qr_code_count = qr_code_count + 1 
          WHERE university_id = :university_id"); 

    $stmt->bindParam(':university_id', $uniId, PDO::PARAM_INT); 
    $pdo->exec($sql); 
} 

$universityID = "gz_01"; 

addSubscription($universityID); 
0

如果在數據庫表中設置爲auto_increment,則不需要將id加1,但是必須刪除ID中的「gz_」部分。

+0

1)每個表只能有1個auto_increment 2)有時你總是不想增加價值,所以它不是我認爲的問題的正確答案。 – Uriziel

+0

你可以編寫一個函數將id分成字符串和數字。然後通過最後一次連接字符串和遞增值並將該值插入表中來增加數字。 – Vagabond

+0

事實上,我(和發佈作者)可以做到這一點。但是,他目前的嘗試如何更容易或更好? – Uriziel

3

試試這個

$sql = 'UPDATE qr_code_count SET `'.$university.'` = `'.$university.'` + 1'; 
+0

有什麼不寫的原因是:'$ sql ='UPDATE qr_code_count SET \'$ university \'= \'$ university \'+ 1';'? –

+0

使用簡單引號將不會讀取'$ university'作爲變量,而是作爲一個字符串,因此它不起作用。 –

-1
mysql_connect('localhost','root','root'); 
mysql_select_db('prenium'); 
$university="property_id"; 
$sql = 'UPDATE property_images 
     SET '.$university.' = '.$university.'+1'; 

if(mysql_query($sql)) echo "DONE !"; 
else die(mysql_error()); 

我測試了這一點,它的工作與我

EDITED

+0

不,這會產生MySQL語法錯誤。 –

+0

我已經測試過它和它一起工作 –

+0

嗨Jeffery,我試過了你的方法,但仍然得到了致命錯誤的錯誤:帶有消息'SQLSTATE [42S22]的未捕獲異常'PDOException':找不到列:1054未知列'gz_01 'in'where clause'' – Dennisboys

0

試試這個

$sql = "UPDATE qr_code_count 
     SET :columnName1 = :columnName2 + 1 
     ";      
$stmt = $pdo->prepare($sql); 
$stmt->bindParam(':columnName1', $universityID, PDO::PARAM_STR); 
$stmt->bindParam(':columnName2', $universityID, PDO::PARAM_STR); 
$stmt->execute(); 
0

假設你的大學總是在格式gz_01, gz_02, wh_01, wh_02...etc... 字母不重複和數字。下面的代碼將做到這一點。

function addSubscription($university){ 
    $universitySub = $university[0].$university[1].$university[2]; 
    $universityNumberInc = ($university[3].$university[4]+1); 
    if(strlen($universityNumberInc) < 2) { $universityNumberInc = "0".$universityNumberInc;} 
    $universityinc = $universitySub.$universityNumberInc; 

$sql = "UPDATE qr_code_count 
      SET $university = $universityinc; 
      ";      

    $pdo->exec($sql); 
    } 

圖片前後+1到$university enter image description here

0
if $university = 'gh_01' 
get only number from above string using below code 
$num = intval(preg_replace("/[^0-9]/", "", $university)); 
$num=$num+1; 
get only letters 
$letter=preg_replace('/[0-9]/','',$university); 
$unversity=$letter.'_'.$num; // combine both letter and number 
$sql = "UPDATE qr_code_count SET '".$university."' = "'.$university."'"; 
相關問題