2014-11-04 59 views
-4

如何給變量更新wordpress數據庫?更新數據庫wordpress的foreach變量

如果不是一個變量,我給號碼是好的,我認爲這是一個錯誤。

global $wpdb; 
    $wpdb->teams = $wpdb->prefix.'teams'; 
    $retrieve_data = $wpdb->get_results("SELECT * FROM $wpdb->teams WHERE moder = 'nie'"); 

    foreach ($retrieve_data as $retrieved_data){ 
     echo "<form method='post'><table><tr>"; 
     echo "<td>".$retrieved_data->id."</td>"; 
     echo "<td>".$retrieved_data->nazwa."<input type='hidden' name='id'>".$retrieved_data->id."</input><input type='submit' value='OK' /></td>"; 
     echo "</tr></table></form>"; 
    } 

    $id = $_POST['id']; 
    settype($id, 'int'); 
    $wpdb->update('teams', array('moder' => 'tak'), array('id' => $id)); 

回答

0

好的!完成:) thx :) 最終代碼

global $wpdb; 
$table = $wpdb->prefix . "teams"; 
$retrieve_data = $wpdb->get_results("SELECT * FROM $table WHERE moder = 'nie'"); 

foreach ($retrieve_data as $retrieved_data) { 
    echo "<form method='post'><table><tr>"; 
    echo "<td>".$retrieved_data->id."</td>"; 
    echo "<td>".$retrieved_data->nazwa."<input type='text' name='id' value='$retrieved_data->id' /><input type='submit' name='test' value='OK' /></td>"; 
    echo "</tr></table></form>"; 
} 

print_r($_POST); 
$id = $_POST['id']; 
$table = $wpdb->prefix . "teams"; 
$wpdb->update($table, array('moder' => 'tak'), array('id' => $id)); 
0

你所做的基本上是錯誤的。你顯示錶格,然後你只需更新你的表格而沒有任何值。提交表單後,更新表格。但是,如果有人加載頁面,並且不提交表單,那麼您將再次更新爲0.

我想,您還需要驗證id字段。

嘗試這種在這條路上:

global $wpdb; 
$wpdb->teams = $wpdb->prefix . 'teams'; 

//Do this before any output in the buffer... 

//Check is there is a $_POST["id"]. And move this update block to the top of your file 
if (isset($_POST["id"])) { 
    $id = $_POST['id']; 
    settype($id, 'int'); 
    $wpdb->update('teams', array('moder' => 'tak'), array('id' => $id)); 
    //redirect the user to somewhere 
    header ("Location: " . $_SERVER["PHP_SELF"]); 
} 


$retrieve_data = $wpdb->get_results("SELECT * FROM $wpdb->teams WHERE moder = 'nie'"); 

foreach ($retrieve_data as $retrieved_data) { 
    echo "<form method='post'><table><tr>"; 
    echo "<td>" . $retrieved_data->id . "</td>"; 
    echo "<td>" . $retrieved_data->nazwa . "<input type='hidden' name='id'>" . $retrieved_data->id . "</input><input type='submit' value='OK' /></td>"; 
    echo "</tr></table></form>"; 
} 
0

錯誤:「無法修改標題信息 - 已經由(輸出發送頭開始在C:\ WAMP \ WWW \ MTB \可溼性粉劑內容\主題\ seebcioo \「header.php:16)in line 17」C:\ wamp \ www \ mtb \ wp-content \ themes \ seebcioo \ panel-adm-teams.php「 $ _SERVER [ 「PHP_SELF」]);