Skip to main content
Participant
September 19, 2012
Answered

<input type="hidden" name="h_id" value="$h_id"> will not work (at least for me...)

  • September 19, 2012
  • 1 reply
  • 3321 views

Hi all - In my add new record form I have a hidden text field that I'm trying to use to insert a value into my database. This numeric value is contained in the variable $h_id which came from a $SESSION variable. I checked and it exists. The error I get from MySQL on submit is telling me that I'm trying to save a text value into a number field. That means the value in the hidden field is not saving as a number but as text 'h_id' instead of the value of 1.  Any idea of what I'm doing wrong here? I thought you could use variables in a hidden field.

This is the hidden field code:

<input type="hidden" name="h_id" value="$h_id">

Here is the MySQL error:

Cannot add or update a child row: a foreign key constraint fails (`outercircledb`.`cof_detail`, CONSTRAINT `FK_cof_detail` FOREIGN KEY (`h_id`) REFERENCES `cof_header` (`h_id`))

Regards,

Robert

This topic has been closed for replies.
Correct answer sudarshan.t

You should echo your value field. PHP cannot retrieve ID fields 'as is'.

Your code should be like this:

<?php
if(array_key_exists('h_id', $_GET)) {
     $id
= (int) $_GET['h_id'];
}
?>

<form method="get" action="yourpage.php">
     <input type="hidden" name="h_id" value="
<?php echo $id; ?>">
    
<input type="button">
</form>


1 reply

sudarshan.t
sudarshan.tCorrect answer
Inspiring
September 19, 2012

You should echo your value field. PHP cannot retrieve ID fields 'as is'.

Your code should be like this:

<?php
if(array_key_exists('h_id', $_GET)) {
     $id
= (int) $_GET['h_id'];
}
?>

<form method="get" action="yourpage.php">
     <input type="hidden" name="h_id" value="
<?php echo $id; ?>">
    
<input type="button">
</form>


RCJ1212Author
Participant
September 19, 2012

Yep - that did it - Thanks a lot.

Robert

sudarshan.t
Inspiring
September 19, 2012

You're welcome.