Solved

Need help with $wpdb->update

Posted on 2011-09-08
4
708 Views
Last Modified: 2012-05-12
I need to increment, by 1, the numbers in two different fields of a table I have in my wordpress database.

I'm not sure how to use $wpdb->update

I'd like to do something like this -

In table 'adshare', increase the counts in fields 'overall' and 'auth_count' by 1, where field 'author' = chris

My attempt (doesn't work)

$wpdb->update( 'adshare', "overall = overall+1, auth_count = auth_count+1","author = 'chris'");

Of course, the 'overall = overall+1' is based on mysql_query("UPDATE share SET....   and this 'update' thing in wp might not do that. Do I need to get the value of 'overall' first, then add the 1, then update?  I'm trying to keep this as efficient as possible, and that would add connections.
0
Comment
Question by:St_Aug_Beach_Bum
  • 3
4 Comments
 
LVL 7

Assisted Solution

by:boon86
boon86 earned 500 total points
ID: 36508771
can you paste full code of your php with its query so that we can see what can we do here?
0
 

Author Comment

by:St_Aug_Beach_Bum
ID: 36510805
That's pretty much it, it uses the wpdb function in wordpress, here: http://codex.wordpress.org/Class_Reference/wpdb 

I can pull data out of the database like this:

----------------
$test=$wpdb->get_var("SELECT auth_count FROM adshare WHERE author = 'chris'");
echo $test;
---------------

Just don't know how to update/increment like I am trying to do.
0
 

Accepted Solution

by:
St_Aug_Beach_Bum earned 0 total points
ID: 36511420
Ok, I've got it figured out :)

$wpdb->query("UPDATE adshare SET overall = overall+1 , auth_count = auth_count+1 WHERE author = '$author'");
0
 

Author Closing Comment

by:St_Aug_Beach_Bum
ID: 36534725
Thanks for your help
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses how to create an extensible mechanism for linked drop downs.
The purpose of this video is to demonstrate how to set up the permalinks on a WordPress Website. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: mywebsite.com/wp-login.php : Go t…
The viewer will learn how to dynamically set the form action using jQuery.

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question