By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,204 Members | 1,212 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,204 IT Pros & Developers. It's quick & easy.

Problems passing a hidden variable

P: 40
I'm pretty new to PHP and MySQL. I have managed to create a form that inserts data to MySQL and to create a form to search the DB. Now here is my problem. I am unsuccessful coding a hidden variable in an update page. Here is what I have:

search_ir.php (this page searches the DB and lists incident_id's found. User then enters the incident_id of the row to update):

Expand|Select|Wrap|Line Numbers
  1. ....
  2. <FORM METHOD="POST" ACTION="edit_ir.php">
  3. <h3>Enter the Incident ID for the Report You Need to Edit:</h3>
  4. <table border="0" cellpadding="0">
  5. <tr>
  6. <td><INPUT TYPE=TEXT NAME="incident_id" VALUE="" SIZE=6 MAXLENGTH=6></td>
  7. <td><INPUT TYPE=SUBMIT VALUE="Edit"></td>
  8. <td><INPUT TYPE=RESET VALUE="Reset"></td>
  9. </tr>
  10. </table>
  11. </FORM>
  12. ....
  13.  
edit_ir.php (this page receives the incident_id entered above and displays the data - some of the data has been completed, but other fields are inputted on this page to then update the row in the db on submit):

Expand|Select|Wrap|Line Numbers
  1. ...
  2. <?php
  3. $db = mysql_connect("localhost", "root", "no2nt"); mysql_select_db("etest",$db);
  4. $result = mysql_query("SELECT * FROM jos_incidents_combined WHERE incident_id = '$_REQUEST[incident_id]'",$db);
  5.     while ($myrow = mysql_fetch_array($result))
  6.     {
  7.     echo $myrow["incident_id"];
  8. ...
  9. <FORM NAME = "rm_edit_ir" METHOD="POST" ACTION="ir_updated.php">
  10. ...
  11. <INPUT TYPE=HIDDEN NAME="incident_id" VALUE="<?php echo $_REQUEST[incident_id]; ?>">
  12. <INPUT TYPE=SUBMIT VALUE="Submit Form" >
  13.  
  14. <INPUT TYPE=RESET VALUE="Reset Form">
  15. </FORM>
  16. ...
  17.  
ir_updated.php (updates the db with the data from above code):

Expand|Select|Wrap|Line Numbers
  1. ...
  2. $sql = "UPDATE jos_incidents_combined SET
  3.         assess_ortho        = '$_REQUEST[assess_ortho]',
  4.         rescore_morse       = '$_REQUEST[rescore_morse]'
  5.         WHERE incident_id = '$_REQUEST[incident_id]'";
  6. ...
  7.  
  8.  
In ir_updated.php, I get the error:
"Notice: Use of undefined constant incident_id - assumed \'incident_id\' in /usr/local/php_classes/edit_ir.php on line 238"

line 238 in edit_ir.php is this:
Expand|Select|Wrap|Line Numbers
  1. <INPUT TYPE=HIDDEN NAME="incident_id" VALUE="<?php echo $_REQUEST[incident_id]; ?>">
  2.  
Should I be passing something else instead of ="<?php echo $_REQUEST[incident_id]; ?>, or am I not referring to the variable properly?

TIA,

jej1216
May 31 '07 #1
Share this Question
Share on Google+
4 Replies


pbmods
Expert 5K+
P: 5,821
Should I be passing something else instead of ="<?php echo $_REQUEST[incident_id]; ?>, or am I not referring to the variable properly?
You're getting this notice because you didn't put 'incident_id' in quotes. When you use an unquoted string literal, PHP first checks to see if there is a constant by that name. Now granted, it's unlikely that the next version of PHP is going to have an incident_id constant, but just to be on the safe side (and to get rid of that notice, you should do this instead [note the quotes around incident_id]):

Expand|Select|Wrap|Line Numbers
  1. <?php echo $_REQUEST['incident_id']; ?>
  2.  
May 31 '07 #2

P: 40
You're getting this notice because you didn't put 'incident_id' in quotes. When you use an unquoted string literal, PHP first checks to see if there is a constant by that name. Now granted, it's unlikely that the next version of PHP is going to have an incident_id constant, but just to be on the safe side (and to get rid of that notice, you should do this instead [note the quotes around incident_id]):

Expand|Select|Wrap|Line Numbers
  1. <?php echo $_REQUEST['incident_id']; ?>
  2.  
Thanks! I figured it was some small but huge code error on my part!
Just so I understand your explanation, though --- by not using the single quotes I was confusing PHP? It seems from the notice I got that PHP assumed the correct value. But, the update failed until I followed your code correction. Also, once I was in ir_updated.php, the update sql had to not use the single quotes - I guess at that point it was then a constant????
May 31 '07 #3

pbmods
Expert 5K+
P: 5,821
Thanks! I figured it was some small but huge code error on my part!
Just so I understand your explanation, though --- by not using the single quotes I was confusing PHP? It seems from the notice I got that PHP assumed the correct value. But, the update failed until I followed your code correction. Also, once I was in ir_updated.php, the update sql had to not use the single quotes - I guess at that point it was then a constant????
When you used $_POST[incident_id], PHP first looked for a constant named 'incident_id'. When it didn't find that, it used the literal 'incident_id' instead.

Suppose you did this instead:

Expand|Select|Wrap|Line Numbers
  1. define('incident_id', 5);
  2.  
  3. $array = array(
  4.     incident_id => 10,
  5.     'incident_id' => 99
  6. );
  7.  
  8. print($array[incident_id] . ' / ' . $array['incident_id']);
  9.  
The above code would output "10 / 99".

Things get a little confusing when you use array keys in strings, but that's outside the scope of this post. For more information, take a look at the PHP manual.
May 31 '07 #4

P: 40
Thanks for the help - I really appreciate it. Hopefully, in the near future, my posts will be for more complex issues rather than for newbee mistakes.
May 31 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.