PHP update query with syntax error - mysql

Can anyone tell me why this doesnt work, and returns syntax error please?
$sql4 = "update apiStreetCheckGeneral
set (BBAverageSpeed, BBSuperFastBBAvailable, BBCommentary)
values ('$averagespeed', '$superfast', '$bbcommentary')
where PostCode='".$values['PostCode']."'";
CustomQuery($sql4);

This is your query:
update apiStreetCheckGeneral set (BBAverageSpeed, BBSuperFastBBAvailable, BBCommentary)
values ('$averagespeed', '$superfast', '$bbcommentary')
where PostCode='".$values['PostCode'].
I am not aware of update syntax that uses a column list or values. Set each one individually:
update apiStreetCheckGeneral
set BBAverageSpeed = '$averagespeed',
BBSuperFastBBAvailable = '$superfast',
BBCommentary = '$bbcommentary'
where PostCode='".$values['PostCode']."'"
You should learn, however, to use parameterized queries especially for update statements.

Use query like below
$sql4 = "update apiStreetCheckGeneral
set BBAverageSpeed = '$averagespeed',
BBSuperFastBBAvailable = '$superfast',
BBCommentary = '$bbcommentary'
where PostCode='".$values['PostCode']."' ";
CustomQuery($sql4);

Related

syntax error 'from' identifier is not valid input at this position

UPDATE dondathang
SET noigiaohang=diachi
FROM khachhang
WHERE dondathang.makhachhang=khachhang.makhachhang AND noigiaohang IS NULL;
I have a SQL code like above, but this is the error.
syntax error 'from' identifier is not valid input at this position.
What's wrong with my code?
FROM clause is irrelevant in UPDATE sintax. Please take a look at the documentation.
FROM Clause is not a part of UPDATE syntax, you can change the UPDATE as below
UPDATE dondathang
SET noigiaohang=diachi
JOIN khachhang ON dondathang.makhachhang=khachhang.makhachhang
WHERE noigiaohang IS NULL;
The correct syntax in MySQL is:
UPDATE dondathang d JOIN
khachhang k
ON d.makhachhang = k.makhachhang
SET d.noigiaohang = k.diachi
WHERE d.noigiaohang IS NULL;

SQL statement fails under Perl but works from the command line

I am trying to execute a script to update a database:
my $sql_hash_update = 'UPDATE user SET hash = $hash , updated = 1 WHERE id = $row[0]';
my $sth_hash_update = $dbh->prepare($sql_hash_update);
$sth_hash_update->execute();
I get the error that this is not proper syntax, but this works within SQL itself.
DBD::mysql::st execute failed: You have an error in your SQL syntax;
check the manual that corresponds to your MariaDB server version for
the right syntax to use near '[0]' at line 1 at conexion.pl line 32.
Any ideas what am I doing wrong?
You are using single quotes, so this statement
my $sql_hash_update = 'UPDATE user SET hash = $hash , updated = 1 WHERE id = $row[0]'
will not interpolate the values of $hash and $row[0] into the SQL statement. Instead they will be left as they are, and so the statement isn't valid SQL
You could simply switch to double quotes, which do interpolate, but it is best to use placeholders like this
my $sql_hash_update = 'UPDATE user SET hash = ?, updated = ? WHERE id = ?';
my $sth_hash_update = $dbh->prepare($sql_hash_update);
$sth_hash_update->execute( $hash, 1, $row[0] );
That way you avoid the risk of code injection, and you need to prepare only once for many different execute calls
Placeholders are valid wherever an expression is allowed in the SQL syntax. That means, for instance, you cannot provide a placeholder for a table name, because you couldn't put an expression there in an ordinary SQL statement
Perl does not interpolate single quotes so $row[0] is not being expanded.
You want double quotes.
However, you should also pass $row[0] as a bind parameter.
Something like:
my $sql_hash_update = 'UPDATE user SET hash = ? , updated = 1 WHERE id = ?';
my $sth_hash_update = $dbh->prepare($sql_hash_update);
$sth_hash_update->execute($hash, $row[0]);
Use double quotes instead of single quote
my $sql_hash_update = "UPDATE user SET hash = $hash , updated = 1 WHERE id = $row[0]";

Yii2 deleting records

I'm trying to delete records in Yii2 using folowing code:
$query = "DELETE `master_contacts`
FROM `master_contacts`
LEFT JOIN `master_list_contacts`
ON `master_list_contacts`.`master_contact_id` = `master_contacts`.`id`
WHERE `master_contacts`.`deleted` = 1
AND `master_list_contacts`.`id` IS NULL";
Yii::$app->db->createCommand($query);
I have Following database structure.
Query works perfectly in mysql client but I can't get it work in yii (although I didn't get any error).
Could someone tell me please how I supposed do this in yii?
If you use pure SQL you should call execute method:
$query = "DELETE `master_contacts`
FROM `master_contacts`
LEFT JOIN `master_list_contacts`
ON `master_list_contacts`.`master_contact_id` = `master_contacts`.`id`
WHERE `master_contacts`.`deleted` = 1
AND `master_list_contacts`.`id` IS NULL";
Yii::$app->db->createCommand($query)->execute();

MYSQL Error:1054 - Unknown Column

just having some trouble with an SQL update in PHP. Listed below is an extract of the function:
$captain = $this->getUserName();
$member = $textParts[1];
$memberNo = 'member1';
$sqlUpdate = 'UPDATE ajax_chat_draft_teams SET '.$memberNo.'='.$member.' WHERE captain='.$captain.'';
$result = $this->db->sqlQuery($sqlUpdate);
When the Query is reached it throws the following error:
Query: UPDATE ajax_chat_draft_teams SET member1=user WHERE captain=Oolius
Error-Report: Unknown column 'Oolius' in 'where clause'
Error-Code: 1054 error occured!
The table ajax_chat_draft_teams has 5 fields: captain, member1, member2, member3, member4
(Note: There is a record in the table where the captain is Oolius and all members are NULL).
I'm failing to see what is wrong with my SQL statement. Thanks for your time.
Try this:
$sqlUpdate = 'UPDATE ajax_chat_draft_teams SET '.$memberNo.' = "'.$member.'" WHERE captain = "'.$captain.'"';
String literals need to be surrounded in single quotes. The query should look like this:
UPDATE ajax_chat_draft_teams SET member1='user' WHERE captain = 'Oolius'
Also, consider using PDO and bind variables.
You need to put Oolius in quotes other MySQL thinks it is a column name.
Use this
$sqlUpdate = 'UPDATE ajax_chat_draft_teams SET
'.$memberNo.'="'.$member.'" WHERE captain="'.$captain.'"';
i hope it will help you.

Two database changes under the same variable

I have the code:
...SOME CODE HERE...
//inserting data order
$order = "UPDATE `database`.`table` SET `status_client` = 'PRELUAT' WHERE `flux_receptie`.`id` =$res";
$order = "UPDATE `database`.`table` SET `status_client` = 'PRELUAT' WHERE `flux_receptie`.`id` =$res";
//declare in the order variable
$result = mysql_query($order); //order executes
...SOME CODE HERE...
Ignore the DB command because it will be modified. My question is that if I can use $order for two database commands? Will $result = mysql_query($order); work properly?
Thanks friends! :)
No.
You are redefining $order in the second line, so the first time you set $order does not do anything. You will have to make two separate queries or combine them into a single MySQL statement.
By default, you are also simply not allowed to execute two queries at once for security purposes (as someone could inject a MySQL command in the middle of yours that does something nefarious). For example. setting $order to:
`UPDATE `table` SET `column`='value' WHERE (condition); UPDATE `table2` SET `column2`='value2' WHERE (condition2);
would not work either.
(following on from Nathans answer, and the comments to this)
To make two queries, simply structure:
//inserting data order
$order1 = "UPDATE `database`.`table` SET `status_client` = 'PRELUAT' WHERE `flux_receptie`.`id` =$res";
$order2 = "UPDATE `database`.`table` SET `status_client` = 'PRELUAT' WHERE `flux_receptie`.`id` =$res";
//declare in the order variable
$result = mysql_query($order1); //order executes
$resultTwo = mysql_query($order2);
This will execute both orders as defined in the variable string. I highly recommend exploring OOP MySQL with MySQLi or PDO methodology. Google it :)