php - 与您的 MySQL 服务器版本相对应的手册,以便在附近使用正确的语法

标签 php mysql

我正在尝试向数据库中插入一条记录,但出现错误。

我正在使用 PHP 和 MySQL。

我需要一个成功的结果,但我收到一个错误,这是我的代码:

 $Pname = $_POST[Pname];
    $P_Price = $_POST[P_Price];
    $P_Desc = $_POST[P_Desc];
    $P_City = $_POST[P_City];
    $P_Size = $_POST[P_Size];
    $P_Rooms = $_POST[P_Rooms];
    $P_garage = $_POST[P_garage];
    $P_Address = $_POST[P_Address];
    $P_Long = $_POST[P_Long];
    $P_Lat = $_POST[P_Lat];
    $Provinces_idProvinces = $_POST[Provinces_idProvinces];

    // array for JSON response
    $response = array();

        $result = mysql_query("INSERT INTO property(Pname,P_Price,P_Desc,P_City,P_Size,P_Rooms,P_garage,P_Address,P_Long,P_Lat,Provinces_idProvinces)
        VALUES ('".$Pname."',".$P_Price.",'".$P_Desc."','".$P_City."','".$P_Size."','".$P_Rooms."',".$P_garage.",'".$P_Address."',".$P_Long.",".$P_Lat.",".$Provinces_idProvinces."'");

     if ($result) {
            // successfully inserted into database
            $response["success"] = 1;
            $response["message"] = $result ;

            // echoing JSON response
            echo json_encode($response);
        } else {
            // failed to insert row
            $response["success"] = 0;
            $response["message"] = mysql_error();

这是我的错误:

{"success":0,"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 2"}

我检查了一切,但它仍然给我同样的错误。请帮助,现在快 3 天了。

这是 MySQL 查询:

delimiter $$

CREATE TABLE `property` (
  `idProperty` int(11) NOT NULL AUTO_INCREMENT,
  `Pname` varchar(45) DEFAULT NULL,
  `P_Price` double DEFAULT NULL,
  `P_Desc` varchar(45) DEFAULT NULL,
  `P_City` varchar(45) DEFAULT NULL,
  `P_Size` varchar(45) DEFAULT NULL,
  `P_Rooms` varchar(45) DEFAULT NULL,
  `P_garage` int(11) DEFAULT NULL,
  `P_Address` varchar(45) DEFAULT NULL,
  `P_Long` int(11) DEFAULT NULL,
  `P_Lat` int(11) DEFAULT NULL,
  `P_Sold` tinyint(1) DEFAULT '0',
  `Provinces_idProvinces` int(11) NOT NULL,
  PRIMARY KEY (`idProperty`),
  KEY `fk_Property_Provinces` (`Provinces_idProvinces`),
  CONSTRAINT `fk_Property_Provinces` FOREIGN KEY (`Provinces_idProvinces`) REFERENCES `provinces` (`idProvinces`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=latin1$$

最佳答案

您的查询有很多问题:

  1. 您没有清理用户输入的值($_GET$_POST)- 为此至少使用 mysql_real_escape_string...
  2. 您的查询包含一些空值,因此您的查询中有 ,,,... 提交后验证表单并检查每个属性是否设置了值,以其他方式将其设置为 ''(空字符串)或 0(零)或 null
  3. 你不假思索地使用了撇号和引号……这是 Yoyr 查询(代码格式不是粗体)——粗体是错误的撇号: 插入属性(Pname、P_Price、P_Desc、P_City、P_Size、P_Rooms、P_garage、P_Address、P_Long、P_Lat、Provinces_idProvinces)VALUES('".$Pname."', ".$P_Price.", '".$P_Desc. "', '".$P_City."', '".$P_Size."', '".$P_Rooms."', ".$P_garage.", '".$P_Address."', ".$P_Long .", ".$P_Lat.", ".$Provinces_idProvinces."'");

修复这三个错误/错误后你应该好了。

一些建议:

  1. 始终清理每个用户输入! (这可能不是建议)
  2. 您应该验证表单并为空字段设置默认值
  3. 与其使用如今已弃用的 mysql_* 函数,不如至少使用 mysqli_* 或 PDO。

此外,您正在使用不带引号的数组索引:

$P_Price = $_POST[P_Price];

除非P_Price是一个常量,否则你需要引用它:

$P_Price = $_POST['P_Price'];

激活错误报告以查看您导致的所有错误,然后修复它们:

error_reporting(E_ALL);
ini_set('display_errors', true);

关于php - 与您的 MySQL 服务器版本相对应的手册,以便在附近使用正确的语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11203691/

相关文章:

php - 如何在WordPress页面模板中显示分类帖子?

php - MySQL: 'XYZ' 中的未知列 'field list'

php - 这些值未发布到数据库

mysql - 如何搜索包含150+列的Mysql表?

php - UserCake 中的自定义 PHP SQL 更新功能

javascript - 添加动态输入 - 未正确附加 - 包含图片

php - yii 事件记录关系

java - 错误 DOMException : Failed to execute 'open' on 'XMLHttpRequest' : Invalid URL

即使一切正确,更新后的 MySql 触发器也不会更新

PHP PDO fatal error : Call to a member function prepare() on null