我需要插入数据库,但它给了我错误,我刚刚在这里看到了 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();
?>
我需要像这样的网址:localhost/php/add.php,并且它必须显示{"成功":1,"消息":true},但它不请帮助我
试试这个
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]')");
你有,"$_POST[P_Price]",重复两次。
如果这与您的其他问题有关:PHP 不会插入到 mysql 中
三个问题:
- 您在 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 语句中的名称和列数以及相应值的名称和数量。