2013-10-22 74 views
-1

你好,我正在研究一個模板,我必須獲取一些值並將它們插入到我自己創建的wordpress數據庫表中。 現在,因爲我不知道如何在wordpress數據庫中插入記錄,所以我嘗試在我的本地服務器上進行練習。 我temlate文件是:在wordpress數據庫中插入兩行

<?php 
/** 
    Template Name: Injection. 
*/ 
get_header(); 
?> 
<form method="post" action=""> 
Album: <input type="text" name="album" /> 
Artist: <input type="text" name="artist" /> 
<input type="submit" name="submit"/> 
</form> 
<?php 
if(isset($_POST['submit'])) 
{ 
require_once('../../../wp-load.php'); 
global $wpdb 

$album=$_POST['album']; 
$artist=$_POST['artist']; 

$wpdb->insert('music', array('album' => $album, 'artist' => $artist), array('%s', '%s')) 
} 
?> 

,在這裏我有一個奇怪的問題,如果我不使用這裏$ wbdb類,那麼我temlate文件數據顯示在前端,但是當我用$ WPDB插入,然後它顯示什麼前端。

那麼你有什麼建議我該怎麼做在MySQL中插入數據。

感謝

回答

1

您沒有添加;登錄你的模板,嘗試像下面的代碼。

<?php 
/** 
    Template Name: Injection. 
*/ 
get_header(); 
?> 
<form method="post" action=""> 
Album: <input type="text" name="album" /> 
Artist: <input type="text" name="artist" /> 
<input type="submit" name="submit"/> 
</form> 
<?php 
if(isset($_POST['submit'])) 
{ 
require_once('../../../wp-load.php'); 
global $wpdb; 

$album=$_POST['album']; 
$artist=$_POST['artist']; 

$wpdb->insert('music', array('album' => $album, 'artist' => $artist), array('%s', '%s')); 
} 
?> 
+0

其實我檢查了很多次,本身沒有給予任何解析erroe頁,但要知道它的工作,我在表成功保存數據....謝謝 – Dinesh

+0

歡迎你親愛的:) –

1

「在前端顯示什麼。」我認爲有什麼錯誤的PHP,找出錯誤是什麼,試試這個:

編輯wp-config.php,使WP_DEBUG爲真。

define('WP_DEBUG', true); 

現在應該看到頭版上的一些錯誤消息。

+0

感謝您的時間.... – Dinesh

0

首先打開theme/function.php文件,並將此代碼添加到function.php文件中。 首先添加hook_action()。

add_action('wpcf7_before_send_mail','save_to_db'); 

現在創建存儲數據到數據庫的函數。

function save_to_db($cf7) { 

     global $wpdb; 

     $name = $_POST["your-name"]; 
     $mail = $_POST["your-email"]; 
     $subject = $_POST["your-subject"]; 
     $phoneno = $_POST["your-phoneno"]; 
     $address = $_POST["your-address"]; 
     $message = $_POST["your-message"]; 

     $test = $wpdb->insert($wpdb->prefix.'contactus', 
     array(
      'name'   => 'test', 
      'email'  => '[email protected]', 
      'subject'  => 'fsaf', 
      'phone'  => '1234566230', 
      'address'  => 'tes', 
      'message'  => 'asdasdasd'    
     ), 
     array(
      '%s', 
      '%s', 
      '%s', 
      '%s', 
      '%s', 
      '%s' 
     ) 
     ); 
     /*echo $wpdb->last_query;die; 
     echo '<pre>'; print_r($test);*/ 
    } 
+0

你還可以添加一些上下文/解釋嗎? – Robert

+0

我添加了一些上下文。請檢查一下。它在任何條件下工作, –