2012-01-08 28 views
0

MySQL查詢錯誤我有這樣的代碼:在WordPress

add_action('delete_post', 'my_delete_function'); 
function my_delete_function($post_id) { 
    global $wpdb; 
    $achievement = get_the_category($post_id); 
    $h = $achievement[0]->cat_ID; 
    $s = ''.str_replace('"', '', $h); 
    $p = var_dump(htmlentities($s)); 
    $wpdb->query("INSERT INTO ".$wpdb->prefix."votes (post, votes, guests, usersinks, guestsinks) VALUES('', ".$p.", '', '', '') ") or die(mysql_error()); 
} 

MySQL總是拋出此錯誤

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' '', '', '')' at line 1 

伊夫跑在phpMyAdmin相同的查詢,取代了PHP和值VAR和它工作正常

另外,通過在空白頁上使用echo函數,我確信$ s的值只是一個數字。

任何幫助表示讚賞

+0

在最後一行之前添加一個'var_dump($ s);'來檢查你輸出的內容。如果它包含HTML,請使用'var_dump(htmlentities($ s));' – 2012-01-08 11:39:36

+0

您能顯示完整的最終查詢嗎? – 2012-01-08 11:41:32

+0

檢查您的單引號實際上是單引號而不是反引號。還要替換$ s。與。(int)$ s。只想確認一下。 – 2012-01-08 11:42:15

回答

3

也許 $wpdb->query("INSERT INTO ".$wpdb->prefix."votes (post, votes, guests, usersinks, guestsinks) VALUES('', '".$s."', '', '', '') ") or die(mysql_error());

+0

這樣做伎倆謝謝。 – 2012-01-08 11:49:35

0

試試這個:

global $wpdb; 
$achievement = get_the_category($post_id); 
$h = $achievement[0]->cat_ID; 
$s = str_replace('"', '', $h); 
$wpdb->query(" 
    INSERT INTO ".$wpdb->prefix."votes (votes) 
    VALUES ('".$s."');") or die(mysql_error()); 

如果不工作,那麼你很可能留下需要的值空白字段。

1

我的猜測是,$ s是空的,SQL查詢最終看起來像這樣:

VALUES('', , '', '', '') 

你爲什麼要這麼做

$s = str_replace('"', '', $h); 

它實際上是可能的INT包含「?這是不好的,但我最終會這樣做

$s = (int)str_replace('"', '', $h); 

確保該變量是一個int - no 有什麼關係。

3

如果你想獲得最後的錯誤和最後一個查詢可以使用WPDB $對象這個屬性:

$wpdb->last_error 

會告訴你過去的錯誤,如果你有一個。

$wpdb->last_query 

將爲您提供展示我希望這將幫助你的最後一個查詢(發生錯誤)