Hi,

i have been trying in vain all day to assign child name to a parent id in an innodb table with foreign key. I can retrieve the parent id fine, but the problem is when i try to insert child name, PHP doesn't seem to be passing any values .... i would really appreciate someone having a look please

<?php include("database.php"); 

$id = $_GET['id']; 
$query = "SELECT * FROM parent WHERE id=$id"; 	
$result = mysql_query($query) or die(mysql_error());	
	
$row = mysql_fetch_array( $result ); // Get selected entry  ?>	

<form name="test_fk" action="test_exec.php" method="POST">		

<table > 

<tr>	
  <td valign="top"><label for="id"> Parent id </label></td>	
  <td><input type="text" disabled="disabled" name="id" id="id" value="<?php echo $row['id']; ?>" /> </td>	
</tr>
<tr>	
  <td valign="top"><label for="child_name"> Child name </label></td>	
  <td><input type="text" name="child_name" id="child_name" value="" /> </td>	
</tr>

<tr>
    <td>&nbsp;</td>
    <td><input name="Submit" type="submit" value="Save "/></td>
</tr> 

</table>

</form>

and this is the code that i can't get to work ... passing the parent_id to the child table ... i know its something to do with the values, i just don't know what/how ...

include("database.php"); 

if(isset($_POST[Submit])){ 
  	
  $id = $_GET['id']; 
  $id = $_POST['parent_id'];
	
  $query = "INSERT INTO child (id, parent_id, child_name) VALUES ('','$_POST[parent_id]','$_POST[child_name]')";	
  

  echo $query; 
  
  $result = mysql_query($query) or die('MySQL query failed. '); 

  } // End else 
	
   echo $child_name;

Recommended Answers

All 6 Replies

You didn't give $_POST[parent_id] any Post value actually ... ! Here's the proper code :

<tr>	
  <td valign="top"><label for="id"> Parent id </label></td>	
  <td><input type="text" disabled="disabled" name="parent_id" id="parent_id" value="<?php echo $row['id']; ?>" /> </td>	
</tr>

The HTTP_POST variable is assigned to the form.input BY name (not id).

Thank you MindSter. I have changed the parent_name, but i still get the same MySQL error, parent_id is not getting any value

INSERT INTO child (id, parent_id, child_name) VALUES ('', '','Test name')
MySQL query failed

and this is my INSERT query

if(isset($_POST[Submit])){ 
  
  $id = $_GET['id']; 
  $parent_id = $_POST['id']; 
		
  $query = "INSERT INTO child (id, parent_id, child_name) VALUES ('',   '$_POST[parent_id]','$_POST[child_name]')";	
	
  echo $query; 
  $result = mysql_query($query) or die('<br /><br /> MySQL query failed'); 

  } // End else 
	
  echo $child_name;

I think the problem is with

$id = $_GET['id']; 
  $parent_id = $_POST['id'];

The MySQL error:

Cannot add or update a child row: a foreign key constraint fails (`widss`.`child`, CONSTRAINT `child_ibfk_1` FOREIGN KEY (`parent_id`) REFERENCES `parent` (`id`) ON DELETE CASCADE)

Well, this narrows it down ...
Just noticed the disabled attribute; disabled elements, as far as I know, do not submit their value .. therefor, I suggest you replace the disabled="disabled" whit readonly="readonly". I guess this will work.
Or, if you keep it disabled, you could try adding this to the form:

<input type="hidden" name="parent_id" id="parent_id" value="<?php echo $row['id']; ?>" />

(of course now, you have to rename the old PARENT_ID input field).

Thanks MindSter, it worked! :)

Glad I could help :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.