wpdb更新函数为每个表列获取“未定义索引”错误

时间:2015-07-27 20:51:20

标签: sql-update wordpress wpdb

我正在开发一个允许我添加和删除自定义表记录的插件。添加和删​​除记录工作正常。

我还希望能够更新记录。这是我无法弄清楚我做错了什么。

如果有人能指出我正确的方向,那么这是我的更新代码(我目前专注于“更新所选记录”脚本:

function url_update() {
    global $wpdb;
    $id = $_GET["id"];
    $launchname=$_POST["launchname"];
    $url1 = $_POST["url1"];
    $url2 = $_POST["url2"];
    $urluser = $_POST["urluser"];
    $urlpwd = $_POST["urlpwd"];
    $wpfirstname = $_POST["wpfirstname"];
    $wplastname = $_POST["wplastname"];
    $wpemail = $_POST["wpemail"];
    $activated = $_POST["activated"];

    //update selected record
    if(isset($_POST['update'])){
        $wpdb->update('launcher', 
            array(
                'id' => $id,
                'launchname' => $launchname,
                'url1' => $url1,
                'url2' => $url2,
                'urluser' => $urluser,
                'urlpwd' => $urlpwd,
                'wpfirstname' => $wpfirstname,
                'wplastname' => $wplastname,
                'wpemail' => $wpemail,
                'activated' => $activated
                ),
            array( 'ID' => $id ), //this line fixed the issue
            array('%d','%s','%s','%s','%s','%s','%s','%s','%s','%s'));
    }

    //delete
    else if(isset($_POST['delete'])){   
        $wpdb->query($wpdb->prepare(
            "DELETE FROM wp_tincan_launcher WHERE id = %d",$id
            ));
    }

    else{
    //selecting row to update   
        $selected_record = $wpdb->get_results($wpdb->prepare(
            "SELECT id,launchname,url1,url2,urluser,urlpwd,wpfirstname,
            wplastname,wpemail,activated from launcher where id=%d",$id
            ));
        foreach ( $selected_record as $s ){
            $launchname=$s->launchname;
            $url1=$s->url1;
            $url2=$s->url2;
            $urluser=$s->urluser;
            $urlpwd=$s->urlpwd;
            $wpfirstname=$s->wpfirstname;
            $wplastname=$s->wplastname;
            $wpemail=$s->wpemail;
            $activated=$s->activated;
        }
    }

我认为我的代码是正确的,但是,我真的很感激一些直接解决我的代码的反馈。

编辑:感谢Pekka指出我没有发现错误结果。 我从WP获得的错误是“注意:未定义的索引:数据库中的每一列。”

最好的问候

1 个答案:

答案 0 :(得分:0)

I resolved it myself by changing my update query to include a new line as follows: array( 'ID' => $id ), before the types line.

None of the links I was led to, not any of the generic advice I was provided was of any help what-so-ever.

I've edited the original code to reflect the complete working code.