【问题标题】:Foreach variable to update database wordpressForeach 变量来更新数据库 wordpress
【发布时间】:2014-12-31 10:33:26
【问题描述】:

如何给一个变量来更新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 ));

【问题讨论】:

    标签: php wordpress variables foreach


    【解决方案1】:

    好的!完毕 :) 谢谢 :) 最终代码

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

    【讨论】:

      【解决方案2】:

      错误:“无法修改标头信息 - 标头已由(输出开始于 C:\wamp\www\mtb\wp-content\themes\seebcioo\header.php:16)在 C:\wamp\www\ mtb\wp-content\themes\seebcioo\panel-adm-teams.php 第 17 行"

      第 17 行:标题(“位置:” .$_SERVER[“PHP_SELF”]);

      【讨论】:

        【解决方案3】:

        你所做的基本上是错误的。您显示表单,然后您只需更新没有值的表。提交表单后,然后更新表格。但是如果有人加载页面,并且不提交表单,那么您将再次使用 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>";
        }
        

        【讨论】:

          猜你喜欢
          • 2014-11-21
          • 1970-01-01
          • 2021-07-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-11-24
          • 1970-01-01
          相关资源
          最近更新 更多