Php 用于更新数据库wordpress的Foreach变量

Php 用于更新数据库wordpress的Foreach变量,php,wordpress,variables,foreach,Php,Wordpress,Variables,Foreach,如何给变量更新wordpress数据库 如果不是一个变量,我给出的数字是可以的,我认为这是一个错误 global $wpdb; $wpdb->teams = $wpdb->prefix.'teams'; $retrieve_data = $wpdb->get_results( "SELECT * FROM $wpdb->teams WHERE moder = 'nie'" ); foreach ($retrieve_data as $re

如何给变量更新wordpress数据库

如果不是一个变量,我给出的数字是可以的,我认为这是一个错误

    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进行更新

我认为,您还需要验证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>";
}

错误:无法修改标题信息-标题已由第17行C:\wamp\www\mtb\wp content\themes\seebcioo\header.php中的C:\wamp\www\mtb\wp content\themes\seebcioo\panel-adm-teams.php中启动的输出发送

第17行:标题位置:$_服务器[PHP_SELF]

好的!完成: 谢谢: 最终代码

  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 ));