-1

Lines of sql code:

$uid=$_POST["uid"];
$mobile=$_POST["mobile"];
$service=$_POST["service"];
$exper=$_POST["exper"];
$range=$_POST["range"];
$af=$_POST["a_from"];
$at=$_POST["a_to"];
$min_charge=$_POST["min_charge"];

$statement=mysqli_prepare($con,"INSERT INTO service (uid,mobile,service,exper,range,a_from,a_to,min_charge) VALUES (?,?,?,?,?,?,?,?)");

mysqli_stmt_bind_param($statement,"iisiiiii",$uid,$mobile,$service,$exper,$range,$af,$at,$min_charge);

mysqli_stmt_execute($statement);

$response=array();
$response["success"]=true;

echo mysqli_error($con);
echo json_encode($response);

Everything is ok, and I don't get any syntax error when I write the code (I am using a code editor software. However, when I use it online, 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 'range,a_from,a_to,min_charge) VALUES (?,?,?,?,?,?,?,?)'

Dharman
  • 30,962
  • 25
  • 85
  • 135
Sachin
  • 71
  • 2
  • 14

1 Answers1

2

This is not obvious. range is a reserved word. It makes a lousy column name, but if you use it, you need to escape it:

INSERT INTO service (uid, mobile, service, exper, `range`, a_from, a_to, min_charge)
    VALUES (?, ?, ?, ?, ?, ?, ?, ?)

Here is the list of reserved words.

For those who are curious, RANGE is a type of partitioning. You can read about it here. I am not sure why MySQL needs to reserve this, given that many keywords are not actually reserved.

I should add: Reserved word such as FROM, TO, or GROUP are obvious, but to me RANGE is rather unexpected as a reserved word, given that many syntactic elements are not reserved, such as OFFSET, PARTITIONS, HASH (also used for partitioning), HOUR, BEGIN.

Nimantha
  • 6,405
  • 6
  • 28
  • 69
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
  • *"But RANGE is rather unexpected as a reserved word."* - I don't quite understand what you meant by that. https://dev.mysql.com/doc/refman/5.7/en/keywords.html `RANGE (R)`. – Funk Forty Niner Feb 05 '17 at 21:10
  • @Fred-ii- . . . Totally subjective. To me it is unexpected when `RANGE` and `PARTITION` are reserved but `HASH` and `PARTITIONS` are not. They are all keywords for the same clause. – Gordon Linoff Feb 05 '17 at 21:19
  • If you do this, I would escape all columns, for consistency. – Nanne Feb 06 '17 at 17:48
  • oh great , now my code working. thanks...Gordon Linoff – Sachin Feb 10 '17 at 09:28