WordPress.org

Ready to get started?Download WordPress

Forums

How do you update a custom table using wp functions (4 posts)

  1. phillbooth
    Member
    Posted 2 years ago #

    This kind of works, my variables get wiped in the DB though, not updated. Please help!

    global $wpdb;
    
    		if( !function_exists('get_option') )
    		{
    			require_once('wp-config.php');
    		}
    $wpdb->update("mytable",  array('mycol1' => $_REQUEST["v1"],
    'mycol2' => $_REQUEST["v2"],
    'mycol3' => $_REQUEST["v3"] ),
    array('qsId' => $_REQUEST["customer_id"] ));

    So is the second array where I specify the ID of the table row to update?

  2. NateJacobs
    Member
    Posted 2 years ago #

    A good walkthrough on interacting with the WordPress DB can be found here:
    http://wp.smashingmagazine.com/2011/09/21/interacting-with-the-wordpress-database/

    This is the format the update helper expects.
    $wpdb->update( $table, $data, $where, $format = null, $where_format = null );

    Also, if the value in your db table is getting wiped, are you sure your variables are actually set?

  3. phillbooth
    Member
    Posted 2 years ago #

    Perhaps Im having a bad day but I just cant seem to gee the update to work, everything else does $wpdb->query. $wpdb->insert etc

    $wpdb->update('questions', array( 'qsEasyContact' => $myvar), array('qsId' => $_REQUEST["customer_id"]), array("%s"), array("%d") );

  4. phillbooth
    Member
    Posted 2 years ago #

    Worked it out...

    $wpdb->update('questions', array( 'qsEasyContact' => $myvar),"WHERE qsId =".$_REQUEST["customer_id"]);

    thx anyhow

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags