user1480473
user1480473

Reputation: 15

Error when inserting

I need to insert into database, but it giving me errors , I just saw silimar example here but mine is not working please tell me what is the problem :

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

?>

I need to but url like this : localhost/php/add.php , And it must display {"success":1,"message":true} , but it does not please help me

Upvotes: 0

Views: 195

Answers (3)

mdakic
mdakic

Reputation: 186

You have to take care about names and number of columns and names and number of corresponding values in sql statement.

Upvotes: 0

Mike Mackintosh
Mike Mackintosh

Reputation: 14245

If this is related to your other question: PHP does not insert into mysql

Three issues:

  1. You have P_Siz instead of P_Size in your mysql create table schema
  2. All non-string fields should not have single quotes surrounding them. This is telling MySQL to cast them as strings. If the field in an int field, it will fail to insert.

I've corrected the issues in this DEMO (removed foreign key association)

Try the following INSERT:

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])");

Upvotes: 0

jcho360
jcho360

Reputation: 3759

Try this

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]')");

you have ,'$_POST[P_Price]', repeated twice.

Upvotes: 1

Related Questions