简体   繁体   English

如何修复PHP中的MySql语法错误?

[英]How to fix this MySql syntax error in PHP?

I'm making data installing form and trying to save those data in db. 我正在制作数据安装表单,并尝试将这些数据保存在db中。
And I'm getting this given below error. 我得到下面给出的错误。
error You have an error in your SQL syntax; 错误您的SQL语法有错误; check the manual that corresponds to your MySQL server version for the right syntax to use near 'desc, total) VALUES ('MM20', '520.00' , 'S.Hashan' ,'522.00' , 'hgASKCHkbzmbk' ' at line 1 检查与您的MySQL服务器版本对应的手册以获取正确的语法,以在第1行的'desc,total'附近使用VALUES('MM20','520.00','S.Hashan','522.00','hgASKCHkbzmbk'
Actually I'm sending six records. 实际上,我正在发送六条记录。 'MM20', '520.00' , 'S.Hashan' ,'522.00' , 'hgASKCHkbzmbk' ,'5200.00' But only five is sending as the error showing. 'MM20','520.00','S.Hashan','522.00','hgASKCHkbzmbk','5200.00',但只有五个正在发送,显示错误。 Given below is my source code. 下面给出的是我的源代码。 Please help me. 请帮我。

<?php

$con = mysql_connect("localhost", "root", "");

if ($con) {
$db = mysql_select_db('mobile', $con);
} else {
 die('Could not connect: ' . mysql_error());
  }

 if (array_key_exists('save_data', $_POST)) {

 $model_no = mysql_real_escape_string($_POST['model_no']);
 $service_fee = mysql_real_escape_string($_POST['service_fee']);
 $sp_name = mysql_real_escape_string($_POST['sp_name']);
 $sp_price = mysql_real_escape_string($_POST['sp_price']);
 $desc = mysql_real_escape_string($_POST['desc']);
 $total = mysql_real_escape_string($_POST['total']);

 $query ="INSERT INTO mobile_rep (model_no, service_fee, sp_name, sp_price, desc,      total)
 VALUES ('{$model_no}', '{$service_fee}' , '{$sp_name}' ,'{$sp_price}' , '{$desc}' ,    '{$total}')";

$result = mysql_query($query);
if ($result) {
    echo "1 record added";
} else {
    echo "error";
    echo '<br/>'.  mysql_error();
}    
  }
      mysql_close($con);

?> ?>

Modify Your Query : 修改您的查询:

$query ="INSERT INTO mobile_rep (`model_no`, `service_fee`, `sp_name`, `sp_price`, `desc`,      `total`) VALUES ('{$model_no}', '{$service_fee}' , '{$sp_name}' ,'{$sp_price}' , '{$desc}' ,    '{$total}')";

DESC is Reserved word in MySQL. DESC是MySQL中的保留字。 Reserved words are permitted as identifiers if you quote them. 如果引用保留字,则可以将它们用作标识符。

basically,this is on behalf of @今 草 顿 웃 so that the question can be closed. 基本上,这代表@今草顿웃,这样就可以解决问题了。

desc is a reserve keyword so you need to escape it. desc是一个保留关键字,因此您需要对其进行转义。 eg. 例如。 ..,sp_name, sp_price, desc , ... ..,sp_name,sp_price, desc ,...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM