0

I'm developing a plugin that lets me add and delete custom table records. Adding and deleting records works fine.

I also want to be able to update the records. This is where I cannot figure out what I'm doing wrong.

Here is my update code if anyone can point me in the right direction (I'm currently focused on the "update selected record" script:

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

I think my code is correct, however, I would really appreciate some feedback that directly addresses my code.

EDIT: Thanks to Pekka for pointing out that I did not identify the error results. The Error I'm getting from WP is "Notice: Undefined index: each column in the DB."

Best Regards

Dennis
  • 79
  • 11
  • "Does not work" isn't a good problem description. How exactly does it fail, what errors are you getting? Have you done any test outputs to see whether the variables contain the correct values? – Pekka Jul 27 '15 at 20:55
  • Thanks for pointing that out Peeka. I've updated the issue to indicate my error is "Notice: Undefined index: each column in the DB." So for every item, I get this error. – Dennis Jul 28 '15 at 12:30
  • Google the error message, you'll find the solution; or see here: http://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php – Pekka Jul 28 '15 at 14:25
  • Could not find anything on Google that had to do with "Unknown column in 'where clause'" that actually addressed updating tables in an array. And the link your suggested also had nothing at all. – Dennis Jul 28 '15 at 16:31

1 Answers1

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.

Dennis
  • 79
  • 11