You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE `id_product` = 9' at line 1
UPDATE `ps_product` SET `price` = WHERE `id_product` = 9
The value for price is missing in your sql:
UPDATE `ps_product` SET `price` = WHERE `id_product` = 9
^^^^
You need to pass empty value with in single or double quotes or any values
UPDATE `ps_product` SET `price` = '' WHERE `id_product` = 9
The Problem in your query is that you have "price = WHERE ". As you didnt paste your whole code there I guess that the variable which holds the value with to which Price shall be set is empty.
Thus you should control that variable and see why it is empty (in case the whole query is a string with no variables involved then you forgot the value to which Price shall be set).
If Price is meant to be empty you will have to set it to an empty value via either using ='', =null or =0 depending on what empty is represented by the field (and its type).
Related
I'm working a query for notifications about new messages. When a user send a message and the receiver has not still read it, the column read has value 0. Of course when the receiver clicks on the message and reads it the database update the column read with value = 1.
My problem is that using this query:
$check_notification = $wpdb->get_results("
select *
from {$wpdb->prefix}vp_pms_messages
join {$wpdb->prefix}vp_pms_group_users
on {$wpdb->prefix}vp_pms_messages.id = {$wpdb->prefix}vp_pms_group_users.message_id
where {$wpdb->prefix}vp_pms_group_users.to_username = '{$session_uid}'
and {$wpdb->prefix}vp_pms_group_users.read = '0'
group
by {$wpdb->prefix}vp_pms_group_users.group_id");
{$wpdb->prefix}vp_pms_group_users.read = '0' is not showing any message while if I change the value with '1', I get all the messages read.
It's not a database issue because I see through my phpmyadmin that there are messages with values 0 and messages with value 1.
Is it a formatting problem?
As you are comparing Zero as a String it might creating a problem, try to type cast vp_pms_group_users.read to integer.
So replace 9 number line with this.
and CAST({$wpdb->prefix}vp_pms_group_users.read AS UNSIGNED) = 0
If that field only contains digits then, I'll personality suggest to change the datatype of vp_pms_group_users.read from varcharto tinyint(1) so that you dont have to typecast all the time. (only if it is in development stage, otherwise ignore this suggestion).
Need to update a field in database, but when I make a request, it writes me "0 rows affected" although the image field is, and it is empty.
UPDATE `oc_product` SET `image`= 'no_image.png' WHERE `image`='';
Your query is checking for a value of an empty string, although from what you have shown the value you are actually looking to change has a value of NULL. These are two different values, NULL isn't equal to ''. As such if you are looking to replace items that have a NULL value you need to change your where statement for that (WHERE image IS NULL):
UPDATE `oc_product` SET `image`= 'no_image.png' WHERE `image` IS NULL;
Did select query returns any rows
select image from oc_product WHERE image='';
If so add other field confition as well to check whether it is getting affected ot not example
UPDATE oc_product SET image= 'no_image.png' WHERE (image= "" AND image IS NULL) and other_column ='column value';
Hope this helpful for you :)
I have optional input provided as the 'type' value. I'm trying to prevent the statement from updating the type if it is null data and continue to update the equipment value.
Im tempted to create seperate prepare statements dependant on PHP check of null, but then found the SQL ISNULL(). I havnt used it before and am unsure how to use it within a prepare, unless there is a better way of achieving this?
PHP:
$update = $db -> prepare("UPDATE room
SET type = ISNULL(#:type,:type), equipment = :equipment
WHERE room_id = :room_id");
$update -> bindParam(":room_id", $room_id);
$update -> bindParam(":type", $data['type']);
$update -> bindParam(":equipment", $equipmentList);
$update -> execute();
I get a 'SQLSTATE[42000]: Syntax error or access violation: 1582' when attempting the above.
UPDATE:
UPDATE room
SET type = IFNULL(:type, type), equipment = :equipment
WHERE room_id = :room_id
Ok this correct syntax fixes errors, thanks to eggyal! The problem now is that null :type values are updated and the database valuetype(which is a enum) is not used instead of the null. I double checked in php before SQL that the :type values is null.
Why is IFNULL still returning a null?
Use bindValue instead of bindParam, you are not altering the value of parameter, therefore you don't need to pass it by reference.
if the data type in MySQL can contain null then you can tell it that you are sending a NULL value. Code:
$update -> bindValue(":type", $data['type'], is_null($data['type'] ? PDO::PARAM_NULL : PDO::PARAM_STR);
If your target column cannot contain null then you do not need to execute the query at all.
MySQL's ISNULL() function takes only a single argument and returns either 1 or 0 indicating whether that argument was null or not, respectively. It is equivalent to the IS NULL comparison operator.
MySQL does have a 2-argument IFNULL() function, which may have been what you intended to use (though the arguments you've applied are nonsense)?
UPDATE room
SET type = IFNULL(:type, type),
equipment = :equipment
WHERE room_id = :room_id
If :type is not NULL, its value will be used; else the incumbent value of the type column will be used instead (yielding no change).
You can use the COALESCE() function. It takes as many arguments as you give it and returns the first not-null argument it encounters if any. Otherwise it returns NULL.
UPDATE room
SET type = COALESCE(:type, type), equipment = :equipment
WHERE room_id = :room_id
I want not update a record when the variable is empty when executing a UPDATE query in SQL. However, when the variable is filled, the record should be updated.
So in example, the myAge field in the database have currently a value of 20 (type int).
After executing the following query, the record should still be 20.
$age = '';
db_con->query("UPDATE info SET myAge = ".$age." WHERE account_id = 1");
Ps: I know I could check if the variable is empty with PHP, but I was wondering If this could be archieved within SQL?
You could use an IF construct in SQL to check if the value is empty:
$age = mysql_real_escape_string($age);
db_con->query("UPDATE info SET myAge = IF('".$age."' = '', myAge, ".$age.") WHERE account_id = 1");
If the passed PHP variable is empty, you set the old myAge.
Checking in PHP makes more sense, you might save a database query.
The way you are passing that variable to your database is potentially dangerous. If you didn't know about mysql_real_escape_string, look it up NOW.
Better yet, start using a database wrapper that escapes values for you.
I would add the condition into the where, if you only care about one field.
It is not clear what you mean by empty. That could be either NULL or a blank string. My guess is that myAge is a number, so NULL would be "empty":
UPDATE info
SET myAge = ".$age."
WHERE account_id = 1 AND myAge IS NOT NULL;
You can also do this in the SET, if you like:
UPDATE info
SET myAge = (CASE WHEN myAge IS NOT NULL THEN ".$age." END)
WHERE account_id = 1;
This is necessary if you have multiple columns that you want to update like this. I much prefer CASE over IF(), because CASE is ANSI standard and available in most databases.
I am trying to use UPDATE with my MySQL-Database. I use the following SQL code:
$sql = "UPDATE ToDo
SET Checked = -1
WHERE Index = 1";
When I use this code i get the following error message: "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Index = 1' at line 3"
But when I use
$sql = "UPDATE ToDo
SET Checked = -1
WHERE Text = 'asdf'";
Everything works.
My database has one table named "ToDo" with 3 collumns: Index(int, primary key, auto_increment), Checked(bool) and Text(text).
Can't you "WHERE" a primary key or did i forget something else?
Hope you can help me.
Try adding the backticks:
UPDATE ToDo
SET `Checked` = -1
WHERE `Index` = 1";
Index is a reserved word :
http://dev.mysql.com/doc/refman/5.0/en/reserved-words.html
index is a reserved word for MySQL. You need to esacpe the name by adding backticks like this:
$sql = "UPDATE ToDo
SET Checked = -1
WHERE `Index` = 'asdf'";
In order to make sure MySQL understands that you are talking about a column name and not the reserved word, you can always address the column name tablename.columnname.
In SELECT queries also using shortcuts is possible:
UPDATE ToDo SET ToDo.Checked = -1 WHERE ToDo.Index = 1
SELECT u.Index FROM users u
Also I would recommend not to use camel cases in tables and columns. This proved to be a source for errors and has no real benefit most of the time.
Boolean is 0 or 1, not -1. Try using 0 or 1 for Checked and let us know what happens.