MySQL Update Multiple Columns Issue - php

This seems to be a really simple query, but somehow I keep getting errors...
Basically, I just got a bunch of information from a user, and now I'm going to update their record in the users table in one query:
UPDATE users SET timezone = 'America/New_York', SET updates = 'NO', SET verified = 'YES' WHERE id = '1'
However, after running that, I get the following error:
"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 'SET updates = 'NO', SET verified = 'YES' WHERE id = '1'' at line 1".
Any help is much appreciated.

UPDATE users SET timezone = 'America/New_York', updates = 'NO', verified = 'YES' WHERE id = '1'

Your update syntax is wrong, you have to write syntax SET just once.
UPDATE users SET col1= value1, col2= value2, col3= value3 WHERE condition;
More information about update
UPDATE MANUAL

Set have to be used once no matter how many columns you are updating .your query will be :-
UPDATE users SET timezone = 'America/New_York', updates = 'NO',verified = 'YES' WHERE id = '1'

Related

MySQL query with PHP and WHERE specification

With this update query if the id is in chk_list then I set checked=1. I want if the id isn't in the list to set chk_list=0
$update = "UPDATE data SET checked=1 WHERE id IN($chk_list)";
$qry = $db->query($update);
Is there any simple way after WHERE to set if?
You can use IF() function to determine value to be set. Do the following:
$update = "UPDATE data SET checked = (IF(id IN ($chk_list), 1, 0))";
$qry = $db->query($update);
Note: Please use Prepared Statements to prevent SQL injection related issues
If you want to explicitly set checked as 1 or 0, then the if should be in the set area
Notice that its very dangerous to update all of the table to the extent that MySQL by default prevents this from happening, ans you should set safe updates to 0
UPDATE data SET checked = IF(id IN ($chk_list), 1,0)
What about this with CASE? Not sure pretty neat or not but seems it'll work :)
UPDATE data SET checked=
CASE
WHEN id IN ($chk_list) THEN 1
WHEN id NOT IN ($chk_list) THEN 0
END

how can i update my table column with php mysql using increments

i am creating a page that has a verification function when an order is succesful
i am trying to increase the value of a column incrementally using a variable
but i keep get an error
Error updating record: 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 '+ '57' WHERE Username = 'kokoman'' at line 2
This what i am working with for the increment
$sql = "UPDATE users
SET package = '$packagex', diamonds + $diamondx, rate = '$ratx', amount + $amountx
WHERE Username = '$usernamex'";
That keeps generating the above error
i am using that because i know if i need to increase the value of a column
i would just use "set column +1"
but now i am trying to use a variable because it has to be dynamic but i get the error
please help tanks
The SET clause needs to contain assignments. diamonds + $diamondx should be diamonds = diamonds + $diamondx, and similarly for the other columns.
You should also stop substituting variables into queries and learn to use prepared statements with parameters.
From my understanding you want add value to previously held values of columns amount and diamondsfor that, Try this:
$amountx = 1;
$diamondx = 1;
$sql = "UPDATE users
SET package = '$packagex',
diamonds = diamonds + $diamondx,
rate = '$ratx',
amount = amount + $amountx
WHERE Username = '$usernamex'";

issue with Update query not working

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).

PHP/SQL Syntax Error 1064

I'm new to PHP so bear with me.
This is the code for entering into a bookings table on my site:
http://pastie.org/8190189
And this is the error I get when I enter data into a form on the front end of the site:
http://pastie.org/8190194
I've been working at it for hours but I can't get anywhere.
I thought the problem may be with the:
SELECT u_id FROM `joom_hl_puser_role` WHERE pid = '1'
The table puser_role just connects joomla users to hotels that they can manage on the site. (I want to enter the hotel manager's joomla user number to the table).
but I'm sure that the syntax is correct.
Thanks for your help.
The problem is your use of TIME();
TIME needs a parameter passed.
mysql> select TIME();
ERROR 1064 (42000): 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 ')' at line 1
Change it to
TIME(NOW())
From the MySQL manual for TIME
TIME(expr)
Extracts the time part of the time or datetime expression expr and
returns it as a string.
This function is unsafe for statement-based replication. Beginning
with MySQL 5.5.1, a warning is logged if you use this function when
binlog_format is set to STATEMENT. (Bug #47995)
mysql> SELECT TIME('2003-12-31 01:02:03');
-> '01:02:03'
mysql> SELECT TIME('2003-12-31 01:02:03.000123');
-> '01:02:03.000123'
you are in inserting the TIME() and NOW() functions inside "double quotes". try try chaing it to look like this
$query = "INSERT INTO `#__chbookings_bookings` (`id` ,`company` ,`firstname`,`lastname`, ,`email` ,`reference_number` , `transactionid` ,`invoice_number` , `property` ,`payment_type` ,`people` ,`phonenumber` ,`checkin_date` ,`checkout_date`,`room_type` , `status` , `total_amount` ,`amount_paid` ,`pending_amount`, `date_booked`,`last_updated`, `comments`) VALUES (NULL , '".$user_id."','".$data['firstName']."', '".$data['lastName']."',
'".$data['email']."', ".TIME().",
'".$result["transaction_id"]."','".$invoice_number."',
(SELECT u_id FROM `#__hl_puser_role` WHERE pid = '".$data['property_id']."'),
0 ,'".$total_adult."', '".$data['phone']."',
'".$session->get('checkin_date')."', '".$session->get('checkout_date')."', 0, 0,
'".$final_amount."', '".$session->get('amount_payable')."',
'".$session->get('balance_amount')."',
".NOW().", ".NOW().", 'comments'";

SQL Syntax Error when using primary key with WHERE

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.

Categories