MySQLi Inconsistent Syntax Error - php

I am trying to enter data into my MySQL database using the following query
UPDATE `Customer Table` SET `ID`=$ID, `First name`='$FirstName',`Last name`='$LastName',`Home phone number`=$HomePhoneNumber,`Mobile phone number`=$MobilePhoneNumber,`House number`=$HouseNumber,`House name`='$HouseName',`Street name`='$StreetName',`Town name`='$TownName',`Post code`='$PostCode' ,`Notes`='$Notes' WHERE ID=$ID
This is working fine when I'm calling it from one PHP file but not working when I'm calling it from an API PHP file.
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 '`House number`=0,`House name`='43',`Street name`='Westbury',`Town name`='We' at line 1
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 '`House number`=0,`House name`='3',`Street name`='Close',`Town name`='Thorn' at line 1
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 '`House number`=0,`House name`='flat',`Street name`='2 road',`Town ' at line 1
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 '`House number`=0,`House name`='39',`Street name`='valley',`Town name`='',`' at line 1
Can anyone see where the error is as I'm struggling to find it. All of the data types are correct from what I can see.
Thanks in advance, Luke.

Try this. It seems that your variable $MobilePhoneNumber contains string part within it something like +
"UPDATE `Customer Table` SET `ID` = '$ID', `First name` = '$FirstName',
`Last name`= '$LastName',`Home phone number`= '$HomePhoneNumber',
`Mobile phone number` = '$MobilePhoneNumber',`House number` = '$HouseNumber',
`House name`='$HouseName',`Street name`='$StreetName',`Town name`='$TownName',
`Post code`='$PostCode',`Notes`='$Notes' WHERE ID=$ID"

Gordon Linoff suggestions was best, $MobilePhoneNumber was not being read from my XML file.+ "<MobilePhoneNumber>" + MobileNumber + "</MobileNumber> was being written instead of + "<MobilePhoneNumber>" + MobileNumber + "</MobilePhoneNumber>

Related

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

I'm new in PHP and MySQL and I got a little problem with my project I'm working on.
Whenever I try to get data
$sql = "
SELECT s.id_study_group
, s.id_schedule
, m.name_major
, s.class_
, ts.name_subject
, s.time
, r.name_room
, s.day
, s.semester
FROM tbl_schedule as s
, tbl_major as m
, tbl_room as r
, tbl_subject as ts
WHERE s.id_major = m.id_major
and s.id_subject = ts.id_subject
and s.id_room = r.id_room
and s.id_lecturer =".$this->session->userdata('id_lecturer');
but I get the following error message:
Error Number: 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 '' at line 3
which makes me confused is:
the first time I run this program the program works, then I do not change the code and I the run back this program I got error like this
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
Put quote, so that you may not run into syntax error, if $this->session->userdata('id_lecturer') returns null
From
s.id_lecturer=".$this->session->userdata('id_lecturer');
To
s.id_lecturer='".$this->session->userdata('id_lecturer')."'";
So it becomes like below, if you use heredoc, heredoc syntax is much cleaner and it is really useful for multi-line strings to avoid quoting issues, you can also use active_record :
$lect_id = $this->session->userdata('id_lecturer');
$sql = <<<SQL
SELECT s.id_study_group,
s.id_schedule,
m.name_major,
s.class_,
ts.name_subject,
s.time,
r.name_room,
s.day,
s.semester
FROM
tbl_schedule as s,
tbl_major as m,
tbl_room as r,
tbl_subject as ts
WHERE
s.id_major=m.id_major and
s.id_subject=ts.id_subject and
s.id_room=r.id_room and
s.id_lecturer="$lect_id"
SQL;
Use this instead
$sql = "SELECT s.id_study_group,s.id_schedule,m.name_major,s.class_,ts.name_subject,s.time,r.name_room,s.day,s.semester
FROM tbl_schedule as s, tbl_major as m, tbl_room as r, tbl_subject as ts
WHERE s.id_major=m.id_major and s.id_subject=ts.id_subject and s.id_room=r.id_room and s.id_lecturer='".$this->session->userdata('id_lecturer')."'";

You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use

I keep getting the 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 'Password='evertonblues' Forename='Josh' Surname='Edmondson'
Date of Birth='199' at line 1"
error when running my update query.
$result = mysqli_query($con, "UPDATE Users SET Username='".$newUsername."' Password='".$newPassword."' Forename='".$newForename."' Surname='".$newSurname."' `Date of Birth`='".$newDateofBirth."' Address='".$newAddress."' `Post Code`='".$newPostcode."' Email='".$newEmail."' `Phone Number`='".$newPhonenumber."' WHERE `User ID`='".$newUserid."';");
You forgot a bunch of commas:
..snip... SET Username='".$newUsername."' Password='".$newPassw
^-- and many others
Use query as below:
"UPDATE Users SET `Username`='".$newUsername."', `Password`='".$newPassword."', `Forename`='".$newForename."', `Surname`='".$newSurname."', `Date of Birth`='".$newDateofBirth."', `Address`='".$newAddress."', `Post Code`='".$newPostcode."', `Email`='".$newEmail."', `Phone Number`='".$newPhonenumber."' WHERE `User ID`='".$newUserid."';");
Also space in Date of Birth

how to rectify this "SQL syntax" in php&mysql

i'm trying to create a tagging sys demo here. And i'm stumped why i get this 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 '#ok )' at line 1
here is my code:
$insert=mysqli_query($conn, 'insert into `hashtag` (posts) values ('.$data1.')') or die(mysqli_error($conn));
any help would be appreciated.
In mysqli query you don't have to put $conn .
Just remove it and in query put like this '".$data1."' And check.

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']."', '".

Trying to insert IP address but get an error

I'm trying to insert IP addresses into LastIP(An unsigned integer)
INSERT INTO user_entry (UPC, StateID, StoreID,CityID,Price,Count,LastIP) VALUES (885909301378,1,1,1,170,0,INET_ATON(127.0.0.1))
Error:
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 '.0.1))' at line 1
You need to add quotes:
INSERT INTO user_entry
(UPC, StateID, StoreID,CityID,Price,Count,LastIP) VALUES
(885909301378,1,1,1,170,0,INET_ATON("127.0.0.1"))
Source: Manual

Categories