error in mysql insert query in MYSQL 5.6.12 - php

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.

Related

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

PHP database related error, and MySQL error

This is the error I receive when I type in code further down:
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 'order(order_date,cust_firstname,cust_lastname,cust_add,cust_city,cust_state,indi' at line 1
The code for this error is:
<?php
include "db.php";
$cust_firstname=$_POST['name'];
$cust_lastname=$_POST['lastname'];
$cust_add=$_POST['add'];
$cust_city=$_POST['city'];
$cust_state=$_POST['state'];
$cust_country=$_POST['country'];
$cust_zip=$_POST['pincode'];
$cust_phone=$_POST['mobile'];
$cust_email=$_POST['email'];
$sql=mysql_query("INSERT INTO order(order_date,cust_firstname,cust_lastname,cust_add,cust_city,cust_state,$cust_country,cust_zip,cust_phone,cust_email)values(now(),'$cust_firstname','$cust_lastname','$cust_add','$cust_city','$cust_state','$cust_country','$cust_zip','$cust_phone','$cust_email')")or die(mysql_error());
header("location:done.php");
?>
What I'm wondering, is what this error really means and what I have to do to fix it?
Order is a reserved keyword. You need to wrap it in ticks:
$sql=mysql_query("INSERT INTO `order` (order_date,
try this
$sql=mysql_query("INSERT INTO `order` (order_date,cu.....
order is reserved key word in mysql
EDIT:
change this
$cust_country
to
cust_country
in insert statment columns

Mysql error when calling stored procedure

I create a stored procedure, and I get an error when I call it.
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 ''results0.79123800+1345985910.html')' at line 1
This is how I call it from my php code:
mysql_query("CALL lastscan($task_id,'$file_name')") or die(mysql_error());
I have the sp in my database..
If it is a quote thing, how do I escape the variable I put inside, without modified the stored procedure?
You have incorrect syntax here, use this please:
mysql_query("CALL lastscan('" .$task_id. "', '" .$file_name. "')") or die(mysql_error());

MYSQL Syntax - Insert statement

Struggling with a simple insert command, i'm getting the 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 'All In:
The Poker Movie, tells the story of poker focusing on why one of our nat'
at line 2"
Basically passing film information into a table, here is the code -
$query1 = "INSERT INTO Films_Info (Films_ID,FilmName, FilmRelease, Synopsis,Poster,CritScore,AudScore,Director,Studio,IMDB,date_added_db)
VALUES ('',$Film_Name', '$Film_Release','$filmsynopsis','$film_image','$film_critic','$film_audience','$film_director','$film_studio','$film_imdbID','')";
$runquery1 = mysql_query($query1)or die(mysql_error());
Thanks guys
It looks like that you are missing an ' before $Film_Name. Can you add the missing apostrophe?
If you have phpmyadmin enabled on you server, you can paste the code into the SQL-Field to get syntax highlighting on the SQL query.

Categories