Problems passing a hidden variable

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jej1216
    New Member
    • Aug 2006
    • 40

    Problems passing a hidden variable

    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):

    [code=html]
    ....
    <FORM METHOD="POST" ACTION="edit_ir .php">
    <h3>Enter the Incident ID for the Report You Need to Edit:</h3>
    <table border="0" cellpadding="0" >
    <tr>
    <td><INPUT TYPE=TEXT NAME="incident_ id" VALUE="" SIZE=6 MAXLENGTH=6></td>
    <td><INPUT TYPE=SUBMIT VALUE="Edit"></td>
    <td><INPUT TYPE=RESET VALUE="Reset"></td>
    </tr>
    </table>
    </FORM>
    ....
    [/code]

    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):

    [code=php]
    ...
    <?php
    $db = mysql_connect(" localhost", "root", "no2nt"); mysql_select_db ("etest",$db );
    $result = mysql_query("SE LECT * FROM jos_incidents_c ombined WHERE incident_id = '$_REQUEST[incident_id]'",$db);
    while ($myrow = mysql_fetch_arr ay($result))
    {
    echo $myrow["incident_i d"];
    ...
    <FORM NAME = "rm_edit_ir " METHOD="POST" ACTION="ir_upda ted.php">
    ...
    <INPUT TYPE=HIDDEN NAME="incident_ id" VALUE="<?php echo $_REQUEST[incident_id]; ?>">
    <INPUT TYPE=SUBMIT VALUE="Submit Form" >

    <INPUT TYPE=RESET VALUE="Reset Form">
    </FORM>
    ...
    [/code]

    ir_updated.php (updates the db with the data from above code):

    [code=php]
    ...
    $sql = "UPDATE jos_incidents_c ombined SET
    assess_ortho = '$_REQUEST[assess_ortho]',
    rescore_morse = '$_REQUEST[rescore_morse]'
    WHERE incident_id = '$_REQUEST[incident_id]'";
    ...

    [/code]

    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:
    [code=html]
    <INPUT TYPE=HIDDEN NAME="incident_ id" VALUE="<?php echo $_REQUEST[incident_id]; ?>">
    [/code]

    Should I be passing something else instead of ="<?php echo $_REQUEST[incident_id]; ?>, or am I not referring to the variable properly?

    TIA,

    jej1216
  • pbmods
    Recognized Expert Expert
    • Apr 2007
    • 5821

    #2
    Originally posted by jej1216
    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]):

    [code=php]
    <?php echo $_REQUEST['incident_id']; ?>
    [/code]

    Comment

    • jej1216
      New Member
      • Aug 2006
      • 40

      #3
      Originally posted by pbmods
      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]):

      [code=php]
      <?php echo $_REQUEST['incident_id']; ?>
      [/code]
      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????

      Comment

      • pbmods
        Recognized Expert Expert
        • Apr 2007
        • 5821

        #4
        Originally posted by jej1216
        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:

        [code=php]
        define('inciden t_id', 5);

        $array = array(
        incident_id => 10,
        'incident_id' => 99
        );

        print($array[incident_id] . ' / ' . $array['incident_id']);
        [/code]

        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.

        Comment

        • jej1216
          New Member
          • Aug 2006
          • 40

          #5
          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.

          Comment

          Working...