Php 插入时出错

Php 插入时出错,php,mysql,Php,Mysql,我需要插入数据库,但它给了我错误,我刚才看到了silimar示例,但我的示例不起作用。请告诉我问题是什么: <?php $con = mysql_connect("localhost","****","****"); if (!$con) { die('Could not connect: '. mysql_error()); } mysql_select_db("properties", $con); // array for JSON response $response =

我需要插入数据库,但它给了我错误,我刚才看到了silimar示例,但我的示例不起作用。请告诉我问题是什么:

<?php
$con = mysql_connect("localhost","****","****");
if (!$con)
 {
 die('Could not connect: '. mysql_error());
 }
mysql_select_db("properties", $con);

// 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
        ('$_POST[Pname]','$_POST[P_Price]','$_POST[P_Price]','$_POST[P_Desc]','$_POST[P_City]','$_POST[P_Size]','$_POST[P_Rooms]','$_POST[P_garage]','$_POST[P_Address]','$_POST[P_Long]','$_POST[P_Lat]','$_POST[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"] = "Oops! An error occurred.";

        echo $response["success"];

        // echoing JSON response
        echo json_encode($response);
    }

 mysql_close();

?>
试试这个

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
        ('$_POST[Pname]','$_POST[P_Price]','$_POST[P_Desc]','$_POST[P_City]','$_POST[P_Size]','$_POST[P_Rooms]','$_POST[P_garage]','$_POST[P_Address]','$_POST[P_Long]','$_POST[P_Lat]','$_POST[Provinces_idProvinces]')");

您有“$\u POST[p\u Price]”,重复两次。

如果这与您的其他问题有关:

三个问题:

  • 在mysql创建表模式中,使用P_Siz而不是P_Size
  • 所有非字符串字段周围不应有单引号。这是告诉MySQL将它们转换为字符串。如果该字段位于int字段中,则无法插入
  • 我已更正此中的问题(已删除外键关联)

    请尝试以下插入:

    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 ('$_POST[Pname]',$_POST[P_Price],'$_POST[P_Desc]','$_POST[P_City]','$_POST[P_Size]','$_POST[P_Rooms]',$_POST[P_garage],'$_POST[P_Address]',$_POST[P_Long],$_POST[P_Lat],$_POST[Provinces_idProvinces])");
    

    您必须注意sql语句中的名称和列数,以及名称和相应值的数量。

    像这样:“success”:0,“message”:Oops!发生错误,请返回堆栈溢出!请不要对新代码使用
    mysql.*
    函数。它们不再得到维护,社区已开始恢复。看到了吗?相反,你应该学习并使用或。如果你不能决定,将帮助你做出选择。如果您想了解,。这是您的实际数据库密码吗?您的代码也很容易受到SQL注入的攻击。请务必实现我在上面的注释中所写的内容以否定它。出于调试目的,您应该认真研究更改$response[“message”]=“Oops!发生错误。”;对$response[“message”]=mysql_error();它给了我同样的错误:0{“success”:0,“message”:“Oops!发生了一个错误。”}如果我需要一个积极的结果,请更改
    $response[“message”]=“Oops!发生了一个错误。”
    $response[“message”]=mysql_error()我得到了这个错误:0{“success”:0,“message”:“您的SQL语法有错误;请检查与您的MySQL服务器版本对应的手册,以便在第4行“}”附近使用正确的语法。它仍然给了我同样的问题