I need some help updating the price of a wp table. I am trying to do this outside of wordpress running a php script on the server.
This the table "wp_pzvy_postmeta" which has these four columns:
meta_id post_id meta_key meta_value
18538 4356 _sku GF-2070
18541 4356 _price 2.343
I need to be able to update the meta_value 2.343 based on the meta_value GF-2070, as you can see they both have the same Post_id (4356).
I do ok with php but this mysql stuff really drives me crazy.
I know how to connect to the database but after that I don't know what to do.
I guess I need to be able to retrieve the post_id based on the meta_value (GF-2070) and then use it on the UPDATE
$sql= "update `wp_pzvy_postmeta` set meta_value=5 where meta_key ='_price' and post_id = $postid";
I suggest to work with mysqli:
Create an mysqli host:
$mysqli = new mysqli("host", "user", "password", "database");
Update your $sql query:
a) Get the post_id you want:
select post_id from wp_pzvy_postmeta where meta_key = '_sku' and meta_value = #your_value#
b) Pick your expression :
update wp_pzvy_postmeta set meta_value = 5 where meta_key = '_price' and post_id = #exp_a#
c) Join everything:
Replace #your_value# with 'GF-2070'
Replace #exp_a# with a)
$sql = b)
Make your query, using the previous object:
$result = $mysqli->query($sql);
And you're ready to go! Check here for an working example :)
Related
I am running this query through phpMyAdmin in a Wordpress site:
SELECT trid FROM wp_icl_translations WHERE element_type = 'post_product' AND language_code = 'el'
This query returns a bunch of IDs. I want to collect these inside one variable and then run a foreach to update another table depending on the results I have. I do so like this:
<?php
global $wpdb;
$result = $wpdb->get_results("SELECT trid FROM wp_icl_translations WHERE element_type = 'post_product' AND language_code = 'el'");
foreach($result as $row) {
// run UPDATE code here
}
?>
Now the things I want to update come from this query:
UPDATE wp_postmeta SET meta_value = replace(meta_value, 'Specs','Specifications') WHERE post_id = $row->trid
It is a simple change of the word Specs to Specifications. But how do I write the above query inside the foreach in my code above? If I do var_dump($row->trid) I get correct results; all the IDs of the posts. So I just want to run the UPDATE query for each ID I find.
There is no point to get the data, then update it row by row. Just do it in one statement:
UPDATE wp_postmeta
SET meta_value = replace(meta_value, 'Specs','Specifications')
WHERE post_id IN
(
SELECT element_id
FROM wp_icl_translations
WHERE element_type = 'post_product'
AND language_code = 'el'
)
I have the following SQL query and would like to know if that could be done with the same functionalities as WP query, instead?
$q = "DELETE FROM wp_postmeta WHERE
(
meta_key LIKE '_mepr_stripe_product_id_q3c0oi-7e4%' OR
meta_key LIKE '_mepr_stripe_plan_id_q3c0oi-7e4%' OR
meta_key LIKE '_mepr_stripe_tax_id_q3c0oi-7e4%' OR
meta_key LIKE '_mepr_stripe_initial_payment_product_id_q3c0oi-7e4%' OR
meta_key LIKE '_mepr_stripe_onetime_price_id_%'
)
AND post_id IN ($post->ID);";
global $wpdb;
$wpdb->query($wpdb->prepare($q));
Any ideas?
Thanks
I've been working on this code for a wordpress social media site where you can visualize people of your opposite sex only if they are not your friends (if they are your friends they'll go to another page)
In the php I already can divide men from women, but now I want to also eliminate the men/women whom already are your friend
$query = "SELECT user_id FROM {$wpdb->prefix}bp_xprofile_data, WHERE field_id = 3 AND value = 'homme'";
(with this I would get only men), now the info about their friend status is in another table, I tried using WHERE EXIST to comprobe it
$query = "SELECT user_id FROM {$wpdb->prefix}bp_xprofile_data, WHERE field_id = 3 AND value = 'homme' AND EXIST (SELECT id {$wpdb->prefix}bp_friends WHERE (initiator_user_id = $user_id AND is_confirmed = 1) OR (friend_user_id = $user_id AND is_confirmed = 1)) ";
But doesn't seems to work.
I just want the user_id from the first table, but if I wanted to extract the friend status (that I dont want to extract, I just want it to corroborate my other info to cut out user_ids) I could apply this query
$already_friends = "SELECT is_confirmed FROM {$wpdb->prefix}bp_friends, WHERE initiator_user_id = $user_id OR friend_user_id = $user_id";
I don't know what is the structure of the tables you are referring to. Based on provided information this might work:
SELECT user_id
FROM {$wpdb->prefix}bp_xprofile_data
WHERE
field_id = 3 AND
value = 'homme' AND
user_id NOT IN (SELECT friend_user_id
FROM {$wpdb->prefix}bp_friends
WHERE initiator_user_id=$user_id AND is_confirmed=1) AND
user_id NOT IN (SELECT initiator_user_id
FROM {$wpdb->prefix}bp_friends
WHERE friend_user_id=$user_id AND is_confirmed=1)
I should acknowledge that this SQL statement looks poor: it is slow and it is hard to read. It should be improved if possible.
i have the following 2 queries that i was wondering if i can merge into a single statement.
$sql = "SELECT sale_qty FROM sale_device WHERE id = '$id' ";
and
$sql = "UPDATE device SET qty = $sale_qty WHERE id = $deviceId";
i want to run a select query to get the current quantity of a device from a row, then use that value minus one in a update query to set the new quantity, but don't allow it under 0
is this possible or advised to join? or would it be easier to just run 2 queries?
Yes. I think this does what you want:
UPDATE device
SET qty = (SELECT greatest(sale_qty - 1, 0)
FROM sale_device
WHERE id = '$id'
)
WHERE id = $deviceId
If I'm understanding your question correctly, I think this should work:
UPDATE Device D
JOIN Sale_Device SD ON D.Id = SD.DeviceId
SET D.qty = GREATEST(SD.sale_qty-1,0)
WHERE SD.Id = '$id'
This assumes the Sale_Device table has a DeviceId field.
If I perform the following MySQL query through PHP:
UPDATE pictures
SET category = '0'
WHERE category = '$categoryID'
AND username = '$username'";
$queryUncat = mysql_query($uncategorise) or die(mysql_error());
It works fine and any category that was equal to $categoryID gets changed to 0. However, if I perform the following:
UPDATE pictures
SET category = '0',
pictureorder = (SELECT COUNT(category) + 1 WHERE category='0' AND username='$username')
WHERE category = '$categoryID'
AND username = '$username'";
$queryUncat = mysql_query($uncategorise) or die(mysql_error());
Not only does pictureorder not equal to the count of the category row plus one, but the category no longer gets changed if equal to $categoryID. I'm not too good at figuring this out as I know only basic MySQL through PHP and am not familiar with it through its own console.
Thanks in advance for any suggestions.
This form of query is not valid. First, your subquery is missing a FROM clause. Second, you cannot select from the same table you are updating in the same query.
http://dev.mysql.com/doc/refman/5.1/en/update.html