UPDATE SET coalesce in MYSQL - php

I have this problem in PHP I can't solve:
$nIDEmp=$_GET["idEmp"];
$dniEmp=$_GET["dniEmp"];
$sql="UPDATE empleados
SET
dniEmp= coalesce($dniEmp, dniEmp) WHERE nIDEmp=$nIDEmp";
So, this SQL QUERY DO works, I tryied it into my database with no problems, BUT, SQL keeps throwing me the following error:
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 ' dniEmp) WHERE nIDEmp=1' at line 3
I just can't figure out what i'm doing wrong.
Thank you in advance.
EDIT: I hardcoded NULL into the SQL QUERY and suddenly the code worked
SO, php isn't sending a null value to the sql query?
How do i solve this?

Solution: after doing the query, check (in PHP) for all the values to be null, if they are null, make the variable = "NULL" like the following:
$nIDEmp=$_GET["idEmp"];
$dniEmp=is_null($_GET["dniEmp"])?"NULL":$_GET["dniEmp"]; //<- Here is the solution
$sql="UPDATE empleados
SET
dniEmp= coalesce($dniEmp, dniEmp) WHERE nIDEmp=$nIDEmp";

Related

Insert query not working even though it is right

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 2
i get the above error when running my php file.
my insert query is
$updateUserCanvas="insert into user_canvas(cns_id,course_id,context_id,email_id,resource_id) values(".$canvasId.",".$courseId.",'".$contextId."','".$email."','".$resourseid."')";
cns_id,course_id are integer datatype and context_id,email is varchar and resource_id is text datatype
I searched for the problem and tried adding mysql_real_escape_string
$updateUserCanvas="insert into user_canvas(cns_id,course_id,context_id,email_id,resource_id) values(".$canvasId.",".$courseId.",'".mysql_real_escape_string($contextId)."','".mysql_real_escape_string($email)."','".mysql_real_escape_string($resourseid)."')";
But still not working. i dont know where am mistaking.any help is greatly appreciated.Thanks.
Try This
$updateUserCanvas="insert into user_canvas(cns_id,course_id,context_id,email_id,resource_id) values('".$canvasId."','".$courseId."','".addslashes($contextId)."','".addslashes($email)."','".addslashes($resourseid)."')";
It's something wrong with your $courseId, it may be some string instead of int. Try this string:
$updateUserCanvas="insert into user_canvas (cns_id,course_id,context_id,email_id,resource_id) values ('{$canvasId}','{$courseId}','{$contextId}','{$email}','{$resourseid}')";

Insert image blob into mysql database from a mysql data row using PHP

I am using following Insert statement to insert Blob row read from one database into another. (there is data when i echo the same insert statement).
UPDATE:
"INSERT INTO co_registration_picture_evidence_blb
(_URI, _CREATOR_URI_USER, _CREATION_DATE, _LAST_UPDATE_URI_USER, _LAST_UPDATE_DATE,
_TOP_LEVEL_AURI, VALUE) VALUES('".$imageRow['_URI']."','".$imageRow['_CREATOR_URI_USER']."','"
.$imageRow['_CREATION_DATE']."','".$imageRow['_LAST_UPDATE_URI_USER']."','".
$imageRow['_LAST_UPDATE_DATE']."','".$imageRow['_TOP_LEVEL_AURI']."'".
$imageRow['VALUE']."')"
I get 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 '' at line 3
Update: Now i get this 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 '?PNG\r\n\Z\n\0\0\0\rIHDR\0\0\0?\0\0\0?\0\0\0????\0\0%iCCPICC Profile\0\0x??M' at line 3
Can anyone tell what's wrong with the syntax? my guess is that i should wrap VALUE column that is of type LongBlob (that holds an image) to some encoding function. (all data fields are already mysql_real_escape_string() filtered).
Any input would be really appreciated.
Regards.
You seem to be missing a , '
INSERT INTO co_registration_picture_evidence_blb
(_URI, _CREATOR_URI_USER, _CREATION_DATE, _LAST_UPDATE_URI_USER, _LAST_UPDATE_DATE,
_TOP_LEVEL_AURI, VALUE) VALUES('".$imageRow['_URI']."','".$imageRow['_CREATOR_URI_USER']."','"
.$imageRow['_CREATION_DATE']."','".$imageRow['_LAST_UPDATE_URI_USER']."','".
$imageRow['_LAST_UPDATE_DATE']."','".$imageRow['_TOP_LEVEL_AURI']."', '".
$imageRow['VALUE']."')
What did I change?
'".$imageRow['_TOP_LEVEL_AURI']."'".
'".$imageRow['_TOP_LEVEL_AURI']."', '".

error in mysql insert query in MYSQL 5.6.12

I tried to use the following code to insert,
$op=$_POST["ans"];
$username=$_GET["username"];
mysql_query("insert into $username values('Q3','$op')")
or die(mysql_error());
But I got 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 'values('Q1','Wrong')' at line 1
Why am I getting this error? How can I fix it?
Your query structure is not making any sense. You're inserting into $username? That's not the name of the table, is it?
mysql_query("INSERT INTO `tablename` values('Q3','" . mysql_real_escape_string($op) . "')") or die(mysql_error());
Always be very careful to escape any and all user data being put into your queries, and please, please stop using mysql_query in new code.

Error in SQL syntax

When using the below command
$query=$comm->prepare("DELETE FROM ? WHERE id = ?");
I am receiving 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 '? WHERE id = ?' at line 1
if i remove ? and replace it with table name the code is working properly. Please Help
? is used for parameters, which can change. Why are you using ? for the table name? It remains constant.
Table names cannot be parametrized. Since you supply the table name, and not the user (right?), it should be safe to concatenate/interpolate normally.

MySQL insert with mbox format

Maybe it's my query, but I don't think so. I'm attempting to import messages parsed from an mbox format into MySQL, but MySQL fails when I do it through PHP or manually through phpMyAdmin. Any thoughts?
$sql='INSERT INTO `listserv_mbox` ("message-id", "mbox")
VALUES ("'.mysql_real_escape_string($structure->headers['message-id']).'"
,"'.mysql_real_escape_string($message_base64).'")';
// Run our MySQL query
$db->Execute($sql);
This code looks correct to me, so I'm totally lost on why I cannot import this data for whatever reason. The error I keep getting is:
1064: 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 '"message-id", "mbox") VALUES ("<012c01c69c88$98e38250$31780b4b#dtadavid>","RnJvb' at line 1
The query looks like this. I couldn't get it to work before, so I decided to try base64_encode() on the message itself, but still it doesn't work. Here's the base64_encod()'d query.
INSERT INTO `listserv_mbox` ("message-id", "mbox")
VALUES ("<012c01c69c88$98e38250$31780b4b#dtadavid>"
,"RnJvbSBkYXZpZC53YWxsaXNAZHRhaG91LmNvbSBGcmkgSnVuIDMwIDE1OjA3OjQyIDIwMDYKUmVjZWl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udGVudC1UeXBlOiB0ZXh0L3BsYWluOwoJY2hhcnNldD0iaXNvLTg4NTktMSIKQ29udGVudC1UcmFuc2Zlci1FbmNvZGluZzogcXVvdGVkLXByaW50YWJsZQoKdGhpcyBpcyBhIHRlc3QKLS0tLS0tPV9OZXh0UGFydF8wMDBfMDEyOV8wMUM2OUM1RS5BRkYwN0M3MApDb250ZW50LVR5cGU6IHRleHQvaHRtbDsKCWNoYXJzZXQ9Imlzby04ODU5LTEiCkNvbnRlbnQtVHJhbnNmZXItRW5jb2Rpbmc6IHF1b3RlZC1wcmludGFibGUKCjwhRE9DVFlQRSBIVE1MIFBVQkxJQyAiLS8vVzNDLy9EVEQgSFRNTCA0LjAgVHJhbnNpdGlvbmFsLy9FTiI+CjxIVE1MPjxIRUFEPgo8TUVUQSBodHRwLWVxdWl2PTNEQ29udGVudC1UeXBlIGNvbnRlbnQ9M0QidGV4dC9odG1sOyA9CmNoYXJzZXQ9M0Rpc28tODg1OS0xIj4KPE1FVEEgY29udGVudD0zRCJNU0hUTUwgNi4wMC4yOTAwLjI5MTIiIG5hbWU9M0RHRU5FUkFUT1I+CjxTVFlMRT48L1NUWUxFPgo8L0hFQUQ+CjxCT0RZIGJnQ29sb3I9M0QjZmZmZmZmPgo8RElWPjxGT05UIGZhY2U9M0RBcmlhbCBzaXplPTNEMj50aGlzIGlzIGEgPQp0ZXN0PC9GT05UPjwvRElWPjwvQk9EWT48L0hUTUw+CgotLS0tLS09X05leHRQYXJ0XzAwMF8wMTI5XzAxQzY5QzVFLkFGRjA3QzcwLS0=")
INSERT INTO `listserv_mbox` ("message-id", "mbox"
That should be
INSERT INTO `listserv_mbox` (`message-id`, `mbox`
` instead of "
Maybe value is just too long for type of field "message_id"

Categories