SQL Statement to update multiple columns with multiple where conditions - html

So I have the following SQL statement:
db.exec("UPDATE products SET product_description = '#{fj_description}' AND personalization = '#{fj_personalization}' AND product_photo = '#{fj_product_photo}' AND order_information = '#{fj_order_information}' WHERE campaign_name = '#{camp_name}' AND product_type = 'fleecejacket'")
All of the variables are returning the correct text that's retrieved from an HTML input field, so it seems to be something wrong with the sql statement. When I try to update the database, I get this error:
PG::InvalidTextRepresentation at /update_products
ERROR: invalid input syntax for type boolean: "soft, midweight fleece" LINE 1: UPDATE products SET product_description = 'soft, midweight f... ^

Try using comma instead AND:
"UPDATE products
SET product_description = '#{fj_description}',
personalization = '#{fj_personalization}',
product_photo = '#{fj_product_photo}',
order_information = '#{fj_order_information}'
WHERE campaign_name = '#{camp_name}'
AND product_type = 'fleecejacket'"

Related

What's wrong with this SQL query WHERE AND clause?

Previously, this was working:
$patient_story_set_photos = $wpdb->get_results('SELECT * FROM wp_before_after WHERE patientID = '.$post->ID.' AND patient_display = 1');
However, when I try to add another AND condition like this:
$patient_story_set_photos = $wpdb->get_results('SELECT * FROM wp_before_after WHERE patientID = '.$post->ID.' AND patient_display = 1 AND period_taken = '.$set->period_taken);
I get the following error on screen:
WordPress database error: [Unknown column '1hour' in 'where clause']
SELECT * FROM wp_before_after WHERE patientID = 8175 AND patient_display = 1 AND period_taken = 1hour
Can't see why there's a problem, are you not allowed to use multiple AND conditions in SQL?
The problem is not the AND, the problem is your 1hour, 1hour unquoted means a reference to an object (database, table) named 1hour, you need to quote '1hour'.
If you write
SELECT * FROM wp_before_after
WHERE patientID = 8175
AND patient_display = 1
AND period_taken = '1hour'
you will compare the field periodtaken to a string (CHAR,VARCHAR,TEXT) equal to '1hour'.
I assume period_taken is a field typed CHAR,VARCHAR or TEXT
Before anything, DO NOT CONCATENATE SQL STRINGS nowadays it is a MUST (see how to do it properly https://stackoverflow.com/a/60496/3771219)
The problem you are facing is because, I presume, that the period_taken field is some sort of Char/Varchar/String field and when you are filtering by a "Stringy" field you must sorround your literals values with single quotes:
SELECT *
FROM wp_before_after
WHERE patientID = 8175
AND patient_display = 1
AND period_taken = '1hour'
Hope this help

Can't update two tables in one Query

I've got two Queries to Update two tables:
First Table
UPDATE user_info SET `location` = ".$locationid.", `looking_for` = ".$lookingfor." WHERE `user_info`.`user_id` = ".$infoid.";
Second Table
UPDATE user_personality SET `personality` = '".$changedescription."' WHERE `user_personality`.`user_info_id` = ".$infoid.";
And I'm trying to merge those two Queries, using the same statement.
UPDATE user_info, user_personality
SET user_info.location = ".$locationid.", user_info.`looking_for` = ".$lookingfor.", user_personality.personality = '".$changedescription."'
WHERE `user_info`.`user_id` = ".$infoid."
AND `user_personality`.`user_info_id` = ".$infoid."
I'm not receiving any error message, but is not updating.
What am I doing wrong?
Thanks.
Just a guess...
"
UPDATE user_info i
JOIN user_personality p
ON p.user_info_id = i.user_id
SET i.location = $locationid
, i.looking_for = '$lookingfor'
, p.personality = '$changedescription'
WHERE i.user_id = $infoid;
";
If you set the 2 table fields equal to each other in the where clause it should work, so I believe you'd change your where clause to:
WHERE `user_info`.`user_id` = `user_personality`.`user_info_id`
AND `user_info`.`user_id` = ".$infoid."
MySQL definitely supports updating multiple tables, so the where clause that works for a multi table select statement should also work for an update.

sql update with join

I am trying to update some rows in a table from another row in a different table.
this is the sql I have so far:
UPDATE nymb_posts
JOIN nymb_postmeta
ON nymb_postmeta.post_id = nymb_posts.ID
WHERE nymb_postmeta.meta_key = "_wp_attached_file"
AND nymb_posts.post_type = "attachment"
AND nymb_posts.post_parent = "0"
SET nymb_posts.Guid = nymb_postmeta.meta_value
I just get an "error in your SQL syntax".
If I remove the WHERE clause there is no error. If I make it a SELECT insead of an UPDATE the WHERE clause works.
What is wrong with the WHERE clause?
The set goes before the where:
UPDATE nymb_posts JOIN
nymb_postmeta
ON nymb_postmeta.post_id = nymb_posts.ID
SET nymb_posts.Guid = nymb_postmeta.meta_value
WHERE nymb_postmeta.meta_key = "_wp_attached_file" AND
nymb_posts.post_type = "attachment" AND
nymb_posts.post_parent = "0";
Use set before where clause then it will work

Unexpected T_Variable MYSQL UPDATE VALUES WHERE AND

I am trying to update my table "user" column "img1_name", where the column "blogger_id" equals to $blogger_id and "hotel_id" column equals to $hotel_id.
$sql = "UPDATE user (img1_name) VALUES ('".$img1_name."')
WHERE (blogger_id) = (".$blogger_id.") AND (hotel_id) = ("$hotel_id")";
But it shows error below
Parse error: syntax error, unexpected T_VARIABLE in
/home3/sunangel/public_html/lib/functions/functions.php on line 1593
Can I know how should I edit the line such that it does its function?
You have gone parenthesis crazy and have wrong syntax for your UPDATE statement...
Try this:
$sql = <<<EOT
UPDATE user
SET img1_name = '{$img1_name}'
WHERE blogger_id = {$blogger_id}
AND hotel_id = {$hotel_id}
EOT;
See MySQL documentation for proper UPDATE syntax - http://dev.mysql.com/doc/refman/5.6/en/update.html
Try this :
$sql = "UPDATE user SET img1_name = '".$img1_name."' WHERE blogger_id = ".$blogger_id." AND hotel_id = " . $hotel_id;
Here is SQL UPDATE syntax :
UPDATE your_table_name
SET column_name1 = "your_value",
column_name2 = "another_value"
WHERE your_filter_column = "your_filter_value"

updating a column with avg data from another table column

i wrote a command like this to update a column in one table with avg of columns from another table.. its giving errors
UPDATE college_rating,products set
property1_avg = avg(college_rating.rating1),
property2_avg = avg(college_rating.rating2),
property3_avg = avg(college_rating.rating3),
property4_avg = avg(college_rating.rating4),
property5_avg = avg(college_rating.rating5),
property6_avg = avg(college_rating.rating6),
property7_avg = avg(college_rating.rating7),
property8_avg = avg(college_rating.rating8),
property9_avg = avg(college_rating.rating9),
property10_avg = avg(college_rating.rating10),
property11_avg = avg(college_rating.rating11),
property12_avg = avg(college_rating.rating12),
property13_avg = avg(college_rating.rating13),
property14_avg = avg(college_rating.rating14),
property15_avg = avg(college_rating.rating15)
where products.alias = concat(college_rating.property1,'-',college_rating.property2,'-',college_rating.property3)
group by college_rating.property1,college_rating.property2, college_rating.property3
The MySQL multi-table update syntax does not allow the use of group by.
You can accomplish what you are trying to do by moving the aggregation into a sub-query and joining to that sub-query in the multi-table-update instead.
Something like this should work:
update products p
inner join (
select concat(property1,'-',property2,'-',property3) as alias,
avg(rating1) as property1_avg,
avg(rating2) as property2_avg,
avg(rating3) as property3_avg,
avg(rating4) as property4_avg,
avg(rating5) as property5_avg,
avg(rating6) as property6_avg,
avg(rating7) as property7_avg,
avg(rating8) as property8_avg,
avg(rating9) as property9_avg,
avg(rating10) as property10_avg,
avg(rating11) as property11_avg,
avg(rating12) as property12_avg,
avg(rating13) as property13_avg,
avg(rating14) as property14_avg,
avg(rating15) as property15_avg
from college_rating
group by property1,property2, property3
) as r on r.alias = p.alias
set p.property1_avg = r.property1_avg,
p.property2_avg = r.property2_avg,
p.property3_avg = r.property3_avg,
p.property4_avg = r.property4_avg,
p.property5_avg = r.property5_avg,
p.property6_avg = r.property6_avg,
p.property7_avg = r.property7_avg,
p.property8_avg = r.property8_avg,
p.property9_avg = r.property9_avg,
p.property10_avg = r.property10_avg,
p.property11_avg = r.property11_avg,
p.property12_avg = r.property12_avg,
p.property13_avg = r.property13_avg,
p.property14_avg = r.property14_avg,
p.property15_avg = r.property15_avg;
What is the error that you get? And you need to have a WHERE clause unless you want the UPDATE query to apply to ALL the records
I think you'd need to use sub queries, and I'm not sure if you can update two tables like that in MySQL, at least not without prefixing the attributes.