Unusual Error in inserting data into database - php

i am simply inserting the data from a form using PHP into a MYSQL Table but i am getting an unusual error.My code is
$q1="insert into product (category,image,name,desc) values ('$cat','$pname','$name','$desc')";
$res1=$con->query($q1);
if($res1)
{
some logic;
}
else
{
echo "error";
}
i am unusually getting the else part executed even if the code is sytactically correct.i have checked and verified the values of all the PHP variables.i am using OOP style PHP for database connection.Also,on executing the query in PHPmyadmin too using random values i am getting the following error:
SQL query:
INSERT INTO Product( category, image, name, DESC )
VALUES (
'dsdsd', 'sddsd', 'sd', 'it is a nice'
)
MySQL said: Documentation
#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 'desc)
VALUES
('dsdsd','sddsd','sd','it is a nice')' at line 1

DESC is a reserved key word you need to use backticks as
INSERT INTO Product( category, image, name, `DESC` )

DESC Is a keyword in MySQL, You need to put that in Back ticks ``
INSERT INTO Product( category, image, name, `DESC` )
VALUES (
'dsdsd', 'sddsd', 'sd', 'it is a nice'
)

DESC is a mysql keyword. You need to use backtick like this:-
INSERT INTO Product( category, image, name, `DESC` )

Related

PHP : Error in Query (MySQL) near WHERE clause

I am trying to Insert data from a form with the use of a query. The query ( below ) has a WHERE clause to pick a position from visitorsystem.position.
$query = "INSERT INTO visitorsystem.employee(idNumber,name,surname,position,email)
VALUES ('$idNumber','$name','$surname',SELECT positionid FROM visitorsystem.position WHERE position LIKE '%$position%','$email')";
When executed the following error is given. I have tried adding quotes and single quotes around the SELECT...WHERE clause with no luck. Any ideas if the problem is with the query itself or the SELECT...WHERE clause ?
Error in query: 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 '','fdsf',SELECT positionid FROM visitorsystem.position WHERE
position LIKE '%inf' at line 2
Change your query to :
$query = "INSERT INTO visitorsystem.employee(idNumber,name,surname,position,email)
SELECT '$idNumber','$name','$surname',positionid,'$email' FROM visitorsystem.position WHERE position LIKE '%$position%'";
First of all, learn about prepared Statements to prevent SQL injection.
Second you should add all values to the select Statement:
query = "INSERT INTO visitorsystem.employee(idNumber,name,surname,position,email)
SELECT $idNumber,'$name','$surname',positionid,'$email' FROM visitorsystem.position WHERE position LIKE '%$position%'";
Also you do not need singlequotes around $idNumber, because it is numeric

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

Am pretty new to PHP and stuch with this problem.
Am trying to insert some info from a dropdown menu into a database but keep getting this error. Please help if you can.
The php code is:
<select name="group_ID">
<?php
$pos_query ="SELECT groupName, group_ID FROM 'operations' JOIN members WHERE operations.group_ID = members.group_ID";
$pos_results = $db->query($pos_query);
for ( $i=0; $i < $pos_results->num_rows ; $i++ )
{
$pos_row = $pos_results->fetch_assoc();
echo'<option value"'.$pos_row['group_ID'].'">';
echo $pos_row['groupName'].'</option>';
}
?></select></td>
I have a table called operations and trying to join it to another table called members and insert the data into the members table.
The error is:
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 ''') SELECT 'Church Elders' FROM operations JOIN members ON ope' at line 1.
Sorry guys I posted the wrong code.
The actual code is:
$query = "INSERT INTO members(email, name, gender, dob, profile, password,)
SELECT $group_ID
FROM operations
JOIN members
WHERE operations.group_ID = '$group_ID'" ;
$result = $db->query($query);
Am trying to insert email, name, gender... group_ID into the members table and getting the group_ID from the operations table and gets the following error message:
Error Inserting Details. 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 ') SELECT Church Elders FROM operations JOIN members WHERE oper' at line 1.
Please help!!!!
No need to use single quote with table name('operations') & specify table name with row like groupName.operations, group_ID.operations.
You probably meant to use backticks in your SQL query, not single-quotes. Also, you probably meant the ON statement, rather than WHERE. WHERE is for filtering, while ON tells the JOIN statement which columns to join on.
SELECT `groupName`, `group_ID` FROM `operations` JOIN `members` ON `operations`.`group_ID` = `members`.`group_ID`

Error in PHP MySQL

I am trying to do a simple INSERT, but I keep on getting 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 'FROM,TO,ID,CURRENCY1,CURRENCY2,AMOUNT,NOTE,RATE) VALUES('test', 'test2', 'dd', '' at line 2
Here is my code:
mysql_query("INSERT INTO WIRET
(FROM,TO,ID,CURRENCY1,CURRENCY2,AMOUNT,NOTE,RATE) VALUES('$from', '$to', '$ID', '$currency1', '$currency2', '$amount','$note', '$rate') ")
or die(mysql_error());
Why am I getting this error? I copied this script from another area of my site where it works, I just changed the values.
FROM is a reserved word in MySQL (and SQL in general). If you really have a column named FROM you should wrap it with ` (backticks) so the parser knows you mean a name:
INSERT INTO WIRET (`FROM`, TO, ID, CURRENCY1, ...
If your column is named from you have to put it into "`" (backticks) because FROM is also a SQL keyword.
By putting a keyword (FROMhere) into backticks you say "this is not a SQL keyword" to the DBMS.
Example:
INSERT INTO WIRET (`FROM`,TO,ID,...

SQL Not null and php = or false

I have a loop that inserts descriptions and thumbnails into a table, but not all the items in the loop have descriptions and thumbnails. I didn't think this was a problem, but sql won't insert it, unless they have a value. I thought it might be cause by the not null thing, so i tried to change "null" to "yes".
I set it to print out the executed query and the mysql error:
Query: INSERT INTO experiments (title, dir, desc, thumbnail) VALUES('3dbox', '3dbox', '', '')
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 'desc, thumbnail) VALUES('3dbox', '3dbox', '', '')' at line 1
I've also tried this:
$d = #file_get_contents("/experiments/$sites[$i]/desc.txt") or false;
but that doesn't work either, as you can see in the query.
desc is a reserved word in Mysql. Hence, the error. You would need to change the column name to something else, or access it with backticks like this,
INSERT INTO experiments (title, dir, `desc`, thumbnail) VALUES('3dbox', '3dbox', '', '')
Here's the manual for reserved words.
desc is reserved in mysql and will have to be identified as:
INSERT INTO experiments (title, dir, `desc`, thumbnail) VALUES('3dbox', '3dbox', '', '')
You can not use "desc" as a column name because is a reserved word: http://dev.mysql.com/doc/refman/5.5/en/reserved-words.html, you need to escape it: try changin your query to this:
INSERT INTO experiments (title, dir, `desc`, thumbnail) VALUES('3dbox', '3dbox', '', '');

Inserting Metatag strings into a mysql table

I want to take meta tags from an external webpage and save it into my mysql db, although I keep getting an error. Some help would be appreciated.
$tags = get_meta_tags($_POST['url']);
if (array_key_exists("description", $tags)){
$desc = mysql_real_escape_string($tags['description']);
}
$postQ = mysql_query("INSERT INTO posts (userdesc,desc,title,url,userid) VALUES ('$userdesc','$desc','$title','$url','$userid')");
The error I keep getting is 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 'desc,title,url,userid) VALUES ('Wow this house is small','We've featu' at line 1
desc is a mysql reserved word either enclose that field name in backticks or rename the field name to something else.
eg.
mysql_query("INSERT INTO posts (userdesc,`desc`,title,url,userid)...

Categories