Skip to main content
Participant
September 19, 2012
해결됨

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

  • September 19, 2012
  • 1 답변
  • 3320 조회

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

이 주제는 답변이 닫혔습니다.
최고의 답변: 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 답변

sudarshan.t
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>


RCJ1212작성자
Participant
September 19, 2012

Yep - that did it - Thanks a lot.

Robert

David_Powers
Inspiring
September 19, 2012

Opps, my appologies. First time on the forum for me.

Regards,

Robert


No problems. Thanks for fixing it so quickly, and welcome to the forum.