Jump to content

Recommended Posts

I am trying to insert details from my form to my database and i keep getting an error saying cannot insert or update a child record,

 

<?php 
require("Config.php");
class properties
{

private $connection;
function __construct()

{
//Empty constructor
}

function insert_properties($Type,$Price,$Address,$Postcode,$Photo,$IsAvailable ,$Agent_ID)
{
//$esc_Properties_ID  = mysql_real_escape_string($Properties_ID , $this->connection);
$esc_Type = mysql_real_escape_string($Type, $this->connection);
$esc_Price = mysql_real_escape_string($Price, $this->connection);
$esc_Address = mysql_real_escape_string($Address, $this->connection);
$esc_Postcode = mysql_real_escape_string($Postcode, $this->connection);
$esc_Photo = mysql_real_escape_string($Photo, $this->connection);
$esc_IsAvailable = mysql_real_escape_string($IsAvailable, $this->connection);
$esc_Agent_ID = mysql_real_escape_string($Agent_ID, $this->connection);

$sql="insert into properties('',Type,Price,Address,Postcode,Photo,IsAvailable,Agent_ID)

values ('','{$esc_Type}','{$esc_Price}','{$esc_Address}','
{$esc_Postcode}','{$esc_Photo}','{$esc_IsAvailable}','{Agent_ID}')";

$result=mysql_query($sql, $this->connection);
if(!$result)
{
die("SQL Insertion error: " . mysql_error());
}
else
{
$numofrows = mysql_affected_rows($this->connection);
return $numofrows;
}
}

function openDB()
{
$this->connection = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if(!$this->connection)
{
die("SQL Connection error: " . mysql_error());
}
$db_selected = mysql_select_db(DB_NAME, $this->connection);
if(!$db_selected)
{
die("SQL Selection error: " . mysql_error());
}
}

function closeDB()
{
mysql_close($this->connection);
}


function getResult($sql)
{
$result = mysql_query($sql, $this->connection);
if($result)
{
return $result;
}
else
{
die("SQL Retrieve Error: " . mysql_error());
}
}

} // End of Class agent class
?>

<?php
if (!$_POST) //page loads for the first time
{
?>
<form action="
<?php echo $_SERVER['PHP_SELF'] ?>" method="post">



<br /> Type:<input type="text" name="Type" /><br />
  <br />Price:<input type="text" name="Price" /><br />
  <br />Address:<input type="text" name="Address" /><br />
  <br />Postcode:<input type="text" name="Postcode" /><br />
  <br />Photo:<input type="file" name="Photo" /><br />
  <br />Is Available:<input type="text" name="IsAvailable" /><br />
  <br />AgentID:<input type="text" name="Agent_ID" /><br />

<br /> <input type="submit" value="Save" />
</form>
<?php
}
else
{

$Type = $_POST['Type'];
$Price = $_POST['Price'];
$Address = $_POST['Address'];
$Postcode = $_POST['Postcode'];
$Photo = $_POST['Photo']; 
$IsAvailable = $_POST['IsAvailable'];
$Agent_ID = $_POST['Agent_ID'];


$db1 = new properties();
$db1->openDB();
$numofrows = $db1->insert_properties($Type,$Price,$Address,$Postcode,Photo,IsAvailable,Agent_ID);
echo "jhhjk";
$db1->closeDB();
}
?>

<?php
$sql="insert into properties('',Type,Price,Address,Postcode,Photo,IsAvailable,Agent_ID)

values ('','{$esc_Type}','{$esc_Price}','{$esc_Address}','
{$esc_Postcode}','{$esc_Photo}','{$esc_IsAvailable}','{Agent_ID}')";

 

The above code, what is '' in properties('', Type... ? Is there a column name for it? If not, just leave it out.

If it's an AUTO_INCREMENT, just leave the column out of the insert.

 

<?php
$sql="insert into properties(Type,Price,Address,Postcode,Photo,IsAvailable,Agent_ID)

values ('{$esc_Type}','{$esc_Price}','{$esc_Address}','
{$esc_Postcode}','{$esc_Photo}','{$esc_IsAvailable}','{Agent_ID}')";

my error is

 

 

SQL Insertion error: Cannot add or update a child row: a foreign key constraint fails (`comreg098jayathas`.`properties`, CONSTRAINT `properties_ibfk_1` FOREIGN KEY (`Agent_ID`) REFERENCES `agents` (`Agent_ID`) ON DELETE CASCADE ON UPDATE CASCADE)

 

Oh oops, try this -

<?php
$sql="insert into properties(Type,Price,Address,Postcode,Photo,IsAvailable,Agent_ID)

values ('{$esc_Type}','{$esc_Price}','{$esc_Address}','
{$esc_Postcode}','{$esc_Photo}','{$esc_IsAvailable}','{$esc_Agent_ID}')";

 

You got the Agent_ID variable wrong.

this is the sql code i used to create the table

 

CREATE TABLE IF NOT EXISTS `properties` (

  `Properties_ID` int(4) NOT NULL AUTO_INCREMENT,

  `Type` varchar(20) NOT NULL,

  `Price` float NOT NULL,

  `Address` varchar(40) NOT NULL,

  `Postcode` varchar(7) NOT NULL,

  `Photo` varchar(20) NOT NULL,

  `IsAvailable` varchar(4) NOT NULL,

  `Agent_ID` int(4) NOT NULL,

  PRIMARY KEY (`Properties_ID`),

  UNIQUE KEY `Agent_ID` (`Agent_ID`)

) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=19 ;

CREATE TABLE IF NOT EXISTS `agents` (
  `Agent_ID` int(4) NOT NULL AUTO_INCREMENT,
  `Agent_Name` varchar(20) DEFAULT NULL,
  `Address` varchar(40) DEFAULT NULL,
  `Postcode` varchar(7) DEFAULT NULL,
  PRIMARY KEY (`Agent_ID`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=11 ;

You may also want to check your data. I had a similar problem before where I was loading data in via a text file and the only reason I had the error was that one of the entries was of the wrong data type.

 

Try inserting the data into your database directly (e.g. without php) does this work? Also, you may find that certain of your tables need to hold values before you can insert data into related tables - hence the foreign key constraint.

 

Play around with your data and your database (I assume mysql or other) and see if that is where the issue lies. It looks to me at least like there is an issue loading your data into the database rather than the issue being php related.

CREATE TABLE Properties (

  Properties_ID int(4) NOT NULL auto_increment,

  Type varchar(20) NOT NULL default 'unknown',

  Price int(8) NOT NULL,

  Address varchar(40) NOT NULL default 'unknown',

  Postcode varchar(7) NOT NULL default 'unknown',

  Photo varchar(20) NOT NULL default '',

  IsAvailable varchar(4) NOT NULL,

  Agent_ID int(4) NOT NULL,

  PRIMARY KEY (Properties_ID),

  FOREIGN KEY (Agent_ID) REFERENCES Agents(Agents_ID)

  ON UPDATE CASCADE ON DELETE CASCADE

) ENGINE=INNODB AUTO_INCREMENT=1;

CREATE TABLE Properties (

  Properties_ID int(4) NOT NULL auto_increment,

  Type varchar(20) NOT NULL default 'unknown',

  Price int(8) NOT NULL,

  Address varchar(40) NOT NULL default 'unknown',

  Postcode varchar(7) NOT NULL default 'unknown',

  Photo varchar(20) NOT NULL default '',

  IsAvailable varchar(4) NOT NULL,

  Agent_ID int(4) NOT NULL,

  PRIMARY KEY (Properties_ID),

  FOREIGN KEY (Agent_ID) REFERENCES Agents(Agents_ID)

  ON UPDATE CASCADE ON DELETE CASCADE

) ENGINE=INNODB AUTO_INCREMENT=1;

 

There you are!!! Per DML of "properties", while inserting value in properties.Agent_ID, the same Agent_Id  MUST EXISTS  in agents.Agent_Id. Did you check that?

 

yh

 

CREATE TABLE Agents (
  Agent_ID int(4) NOT NULL
  Address varchar(40) NOT NULL default 'unknown',
  Postcode varchar(7) NOT NULL default 'unknown',
  PRIMARY KEY (Agent_ID)
) ENGINE=INNODB AUTO_INCREMENT=1;

 

May be, I was not clear in my previous post. I meant the following:

 

If the insert query is trying to insert 3 as a value in Agent_Id field in "properties" table, then there must exists a value 3 in the Agent_Id field in the "agents" table. Otherwise, the error will occur.

 

Am I clear?

 

Keep at it, you'll probably find that the error is caused by something like anupamsaha is saying.

 

When you have tables associated with each other via foreign keys, you will find that one of the tables usually has to be populated first before you can enter data into the other table because the tables require the data to be there.

 

you could try removing all of the data from those tables - hopefully you don't have a lot in them (or can reinsert the data back in easily) and then try putting the data into one table and then the other. If unsuccessful try inserting data into the 2nd one first and then the other table - this may show you how one table relies on the other.

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.