• Anyone know how to update a value in the “posts” table of the WP database from a random page?

    My form points to a page on submit and processes the data and I also want it to insert a new value into my posts table… not having much luck.

    <?php
    
    $newvalue = $_GET["count"];
    
    	global $wpdb;
    	$wpdb->update( $wpdb->posts, array( 'post_title' => $posttitle ), array( 'likecount' => $newvalue; )
    	);
    ?>
Viewing 15 replies - 1 through 15 (of 15 total)
  • i personally always just write the mysql statement myself, rather than using the wpdb functions.

    $wpdb->query("UPDATE $wpdb->posts SETlikecount= $newvalue WHEREpost_title= '$posttitle'");

    that shouhld work

    Thread Starter brad8985

    (@brad8985)

    Thanks I will give it a go.

    Thread Starter brad8985

    (@brad8985)

    still no luck. This is from a static page in my template folder if that helps.

    Thread Starter brad8985

    (@brad8985)

    This is driving me crazy. The following code works on the single.php page but as soon as I put it on a static page it does not work…

    <?php
    $id = $_GET["ID"];
    $newvalue = $_GET["count"];
    
    	global $wpdb;
    	$wpdb->query("UPDATE $wpdb->posts SET likecount=$newvalue WHERE ID='$id'");
    ?>

    I have checked the variables and they are fine. I also tried with static values instead of variables and no luck. What is changing when I move this code to a static page. I have used global $wpdb; which I thought was all that was needed to use this hook on a static page.

    In your frist example, you are not telling the query what row to update (no ID given). Try –

    <?php
    $id = $_GET["ID"];
    $newvalue = $_GET["count"];
    
    global $wpdb;
    $wpdb->update(
        $wpdb->posts,
        array(
            'post_title' => $posttitle,
            'likecount' => $newvalue
        )
        array( 'ID' => $id )
    );
    ?>
    Thread Starter brad8985

    (@brad8985)

    Thanks duck_boy. Still no luck. I don’t need both the post title AND the ID as I am only updating the “likecount”. So I only need the ID OR the post_title to identify the correct row.

    Nevertheless I tried your sample and it unfortunately has not worked… not sure why.

    The following does work when placed on the single.php template but as soon as I place it on a blank .php page that has been created purely for this purpose it does nothing…

    <?php
    $id = $_GET["ID"];
    $newvalue = $_GET["count"];
    
    	global $wpdb;
    	$wpdb->query("UPDATE $wpdb->posts SET likecount=$newvalue WHERE ID='$id'");
    ?>
    Thread Starter brad8985

    (@brad8985)

    Would including the WP header files in the php page help?

    lol i think we were assuming that you were on a wordpress template, or had included the wp-blog-header.php file. lol. insert a require statement at the very top of the page, similar to this:

    define('WP_USE_THEMES', false);
    require('./wp-blog-header.php');

    if your file is in the root of your server, this should work fine. if not, you’ll need to adjust the path accordingly.

    Thread Starter brad8985

    (@brad8985)

    Still not working… even tried creating a custom page template so I KNOW everything is linked correctly but I am still not getting any results.

    Thread Starter brad8985

    (@brad8985)

    OK. I have this…

    <?php
    
    define('WP_USE_THEMES', false);
    require('../.././../wp-blog-header.php');
    
    $id = $_GET["ID"];
    $newvalue = $_GET["count"] + 1;
    
    echo $newvalue;
    
    global $wpdb;
    $wpdb->update(
        $wpdb->posts,
        array('likecount' => $newvalue)
        array( 'ID' => $id )); ?>

    Note that the “echo $newvalue;” is just to make sure the variables are being passed correctly, which they are, but they do not echo unless I delete everything except for…

    $id = $_GET["ID"];
    $newvalue = $_GET["count"] + 1;
    
    echo $newvalue;
    Thread Starter brad8985

    (@brad8985)

    Sorry the forum had a fit and double posted and I cannot delete so I am editing this post to read as this.

    check your require statement again. there seems to be a missing period. also, if the code you have doesnt work, try doing a raw mysql command, as mentioned in an earlier post.

    Thread Starter brad8985

    (@brad8985)

    Yea I did fix that just pasted wrong code… Ah yes I will try a different command but I am concerned as to why the page is breaking with the code at the start and at the end. As soon as I delete those two chunks the variable echos fine…

    perhaps we have been going about this incorrectly. why are you pointing your form to a “static” php page, and why not to the form page itself? if we do this, then all of the database information is accessible (since wordpress is loaded), and you can do a “thank you” message, and it’s all self-contained in that one page.

    Thread Starter brad8985

    (@brad8985)

    Its a processing page… as in the link or form submit (either) points to the processing page at which point the DB is updated and then the user is redirected to the page. Ajax is an option but as far as I know you still have the processing page setup in the same way.

Viewing 15 replies - 1 through 15 (of 15 total)
  • The topic ‘wpdb on a separate page’ is closed to new replies.