2013-08-29 56 views
0

我正在嘗試使用WordPress登錄的用戶標識,該標識應在表wp_m_subscription_transaction中搜索transaction_user_ID從wordpress數據庫表中獲取特定數據

該表格有一個名爲transaction_user_ID的列和另一個名爲transaction_paypal_ID的列。

邏輯:如果用戶ID ==交易的用戶ID,然後獲得事務貝寶ID

應傳遞給URL隨着用戶ID電子郵件地址 和執行的URL,以獲得一個代碼 - 這是我的最終產出

這是如何實現的?

我正在開發的代碼是這樣的,這顯然不完全&錯誤的獲取和使用數據庫查詢

<?php $user_id = get_current_user_id(); 
     $query = "SELECT * FROM $wpdb->;wp_m_subscription_transaction 

     if ($user_id ==$query) { 
     <a href="http://primary.copyminder.com/cm_prodkey.php?DID=********&Password=*****&ProdCode=*******&NumRequired=1&TransNum=(GET THIS FROM DATABASE) &CustEmail=". $current_user->user_email .">Get ur Code</a> 
    } 
else { 
echo 'You are not logged in as user ';}?> 
+0

這段代碼甚至不會運行。你有至少可以運行的代碼嗎?這是一個相當大的混亂。 –

+0

沒有我沒有它,我仍然試圖讓代碼正確的代碼至少運行,但我仍然從教程中學習 –

回答

2

首先,因爲你需要的電子郵件地址,以及,讓所有的當前用戶的詳細信息,而不僅僅是ID,使用wp_get_current_user()。其次,你的查詢是錯誤的;你還沒有關閉引號,並有一個流浪分號。如果我沒有看錯,你需要的東西,如:

select transaction_paypal_ID 
    from wp_m_subscription_transaction 
where transaction_user_ID = [the user's ID] 

如果你只在查詢得到一個值,你可以用$wpdb->get_var()

檢索如果查詢不返回一行,並不一定表示用戶未登錄。您可以使用is_user_logged_in()來檢查用戶是否已登錄。

這樣的事情應該工作。我沒有測試過,你必須自己建立網址。

<?php 
if (is_user_logged_in()) { 
    $user = wp_get_current_user(); 
    $paypal_id = $wpdb->get_var("select transaction_paypal_ID from wp_m_subscription_transaction where transaction_user_ID = " . (int) $user->ID); 
    if ($paypal_id) { 
     // Build up your URL here, with $paypal_id and $user->user_email 
    } 
    else { 
     echo 'No transaction found for the current user'; 
    } 
} 
else { 
    echo 'You are not logged in'; 
} 
?> 
+0

感謝這工作正如我所願,是他們的一種方式,我可以掩蓋我建立的網址因爲它會緩解url中的一些密碼 –

2

嘗試此查詢。

$query = "SELECT transaction_paypal_ID FROM wp_m_subscription_transaction where transaction_user_ID = ".$user_id; 

$result = $wpdb->get_row($wpdb->prepare($query), ARRAY_A); 
$transaction_paypal_ID = $result['transaction_paypal_ID']; 
2

由於您沒有提供數據庫結構,字段名稱或許多其他因素,因此此代碼不附帶任何許可證。

但是,被視爲僞代碼,這會讓你朝着正確的方向前進。

$user_id = get_current_user_id(); 
// Global/bring in $wpdb, and the $table_prefix variables 
global $wpdb, $table_prefix; 
// Use $wpdb prepare to be sure the query is properly escaped 
$query = $wpdb->prepare("SELECT * FROM " . $table_prefix . "m_subscription_transaction WHERE user_id = %d", $user_id); 
// Turn on error reporting, so you can see if there's an issue with the query 
$wpdb->show_errors(); 
// Execute the query 
$results = $wpdb->get_results($query); 
// Are there any records? If so, that means the user ID matched 
if ($results) { 
    $row = $results[0]; 
    // Get the data from the row 
    echo '<a href="http://primary.copyminder.com/cm_prodkey.php?DID=********&Password=*****&ProdCode=*******&NumRequired=1&TransNum=' . $row->TransNum . ' &CustEmail=". $current_user->user_email .">Get ur Code</a>'; 
} else { 
    echo 'No records for this user.'; 
}