I just created a php file to update a field in mysql db, my update query is where i have an issue, i want to set 'user_id' to be the user_id of the current user that is logged in so the update will go to the appropriate user. I haven't been able to get this to work. I feel its because i haven't declared the current user earlier in the code, how can i do this?
Here is the line:
$sql = "UPDATE wp85_usermeta SET `meta_value` = `meta_value`+65000 WHERE `user_id` = user_id AND `meta_key` = 'mycred_default'"
Here is the full code:
<?php
$servername = "localhost";
$username = "sparyqmr_wp324";
$password = "(S8p3uV-2t";
$dbname = "sparyqmr_wp324";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "UPDATE wp85_usermeta SET `meta_value` = `meta_value`+65000 WHERE `user_id` = user_id AND `meta_key` = 'mycred_default'";
if ($conn->query($sql) === TRUE) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . $conn->error;
}
$conn->close();
?>
Try to use Session variables. Session varibles are globals variables which are stored on the server for each user.
If a user log ins, you can store his userid in a session varible. If the session is started, you can access this varible in all your documents.
You can start a session with:
session_start();
You can set a session variable, with the following code:
$_SESSION['userid'] = 'your_user';
Logged in user info is in WP Session Cookies .
Example:
foreach ($_COOKIE as $k => $v) {
if ( preg_match( "/wordpress_logged_in/", $k ) ) {
// DO SOMETHING WITH THIS COOKIE
} // end if matched login cookie
} // end foreach
I was able to solve this adding the following to the top of my code
require ('wp-blog-header.php');
global $wpdb;
global $current_user;
get_currentuserinfo();
$skrivID = $current_user->ID;
using $skrivID later in the code automatically returned the user id of the logged in user
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.