dongmei5168 2014-03-21 14:51
浏览 93

解决INSERT INTO语句的语法错误[重复]

This question already has an answer here:

I am getting this error when I try to input data into my form:

"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 'Order (Name, Address, Credit_card_number, Product_id, //Delivery_method) VALUES ' at line 1"

I have checked the database name, table name and attribute names over and over and I cannot find the problem.

This is my form for adding to the database:

<html>
<body>

<form action="insert.php" method="post">
  <p>Name:
    <input type="text" name="name">
  </p>
  <p>Address:
    <input type="text" name="address">
  </p>
  <p>Credit Card Number: 
    <input type="text" name="credit_card_number">
  </p>
  <p>Product Order ID: 
    <input type="text" name="product_id">
  </p>
  <p>Delivery Method: 
    <input type="text" name="delivery_method">
  </p>
  <p>
    <input type="submit">
  </p>
</form>

</body>
</html>

This is my insert.php file:

<?php
$con=mysqli_connect("localhost","root","","order_page");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$sql="INSERT INTO Order (Name, Address, Credit_card_number, Product_id, Delivery_method)
VALUES 
('$_POST[name]','$_POST[address]','$_POST[credit_card_number]','$_POST[product_id]','$_POST[delivery_method]')";

if (!mysqli_query($con,$sql))
  {
  die('Error: ' . mysqli_error($con));
  }
echo "1 record added";

mysqli_close($con);
?>
</div>
  • 写回答

2条回答 默认 最新

  • dongliao6777 2014-03-21 14:53
    关注

    ORDER is a reserved keyword in MySQL. If you are going to name a table ORDER you must wrap it in ticks when using it:

    $sql="INSERT INTO `Order` (Name, Address, Credit_card_number, Product_id, Delivery_method)
    VALUES  ('$_POST[name]','$_POST[address]','$_POST[credit_card_number]','$_POST[product_id]','$_POST[delivery_method]')";
    

    FYI, you are wide open to SQL injections

    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法