PHP没有插入到mysql中

时间:2012-06-25 14:04:21

标签: php mysql

我正在尝试插入mysql,但它给了我一个错误,这里是我的代码:

$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, P_Sold, Provinces_idProvinces)
    VALUES('http://10.0.2.2/images/pic3.jpg',98000,'beautifull house','Durban','7m',1,2,'L-377 Umlazi','30.863226','-29.971518',0,'1'");

 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();

它返回消息“Oop!发生错误”我不明白它是怎么发生的

和我的mysql

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_Siz varchar(45) DEFAULT NULL,
  P_Rooms varchar(45) DEFAULT NULL,
  P_garage int(11) DEFAULT NULL,
  P_Address varchar(45) DEFAULT NULL,
  P_Long float (10,6) DEFAULT NULL,
  P_Lat float (10,6) 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
);

3 个答案:

答案 0 :(得分:3)

我认为你并没有用括号结束价值观。

$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, P_Sold, Provinces_idProvinces)
VALUES('http://10.0.2.2/images/pic3.jpg',98000,'beautifull house','Durban','7m',1,2,'L-377 Umlazi','30.863226','-29.971518',0,'1')");

答案 1 :(得分:0)

你有省份_idProvinces为'1'这是一个字符串,而不是你的表所描述的int。

试试这个:

$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, P_Sold, Provinces_idProvinces)
    VALUES('http://10.0.2.2/images/pic3.jpg',98000,'beautifull house','Durban','7m',1,2,'L-377 Umlazi','30.863226','-29.971518',0,1)");

答案 2 :(得分:0)

三个问题:

  1. 您的mysql create table schema
  2. 中有P_Siz而不是P_Size 在你的价值陈述之后
  3. 你错过了尾随)
  4. 在Provinces_idProvinces
  5. 之后删除单引号

    我已更正此 DEMO (已删除的外键关联)中的问题