PHP: using input=hidden type data from form to get MySQL data

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • captainmerton
    New Member
    • May 2007
    • 12

    PHP: using input=hidden type data from form to get MySQL data

    I am new to PHP but have been using it for about a week. I'm having no trouble using html forms to recall data from a MySQL table when the input type=text but i cant seem to find a way of recalling the data from the MySQL table when the input type=hidden.

    Here's the form code:

    [PHP]
    <?php $action = $_REQUEST['action'];
    $epic = $_REQUEST['epic']; ?>

    <TR>
    <TD>

    <?php echo $action; ?>

    <INPUT TYPE="HIDDEN" NAME="action" VALUE="

    <?php
    echo $action; ?>

    "></TD>
    <TD>

    <?php
    echo $epic; ?>

    <INPUT TYPE="HIDDEN" NAME="epic" VALUE="

    <?php
    print $epic; ?>

    " SIZE="4"></TD>
    <TD><INPUT TYPE="TEXT" NAME="volume" SIZE="5"></TD>
    <TD> </TD>
    <TD> </TD>
    </TR>
    </TABLE>
    <P><INPUT TYPE="SUBMIT" VALUE="Get Quote"</P>
    </FORM>
    [/PHP]

    And the DB call:

    <?php $getquote=@mysq l_query("SELECT EPIC,SharePrice FROM SharePrices WHERE EPIC = '$epic'");

    The variable $epic is passed through first time in the URL from another page and is then displayed in the form as text but stored as hidden data as i dont want the user the ability to change it. The data comes back fine if I remove the where clause from the select or plug in a value eg. EPIC=ABC or set $epic=ABC before the query. The problem however is the data contained in $epic after it is passed through. i have used echo to display it and it looks ok. i have used various string function to remove spaces etc but all to no avail. The field EPIC is defined as CHAR(4) on the MySQL table and is the primary key.

    Anybody got any help?

    Cheers.
    Last edited by Atli; May 12 '07, 12:47 AM. Reason: Added code tags
  • MMcCarthy
    Recognized Expert MVP
    • Aug 2006
    • 14387

    #2
    This question is being moved to the PHP forum.

    ADMIN

    Comment

    • Motoma
      Recognized Expert Specialist
      • Jan 2007
      • 3236

      #3
      My typical approach to situations like this is to set my MySQL query to a variable before I perform the query. That way I can see what, exactly my query is, and this allows me to copy and paste EXACTLY what is being sent via PHP.

      Try this:

      [PHP]
      <?php
      $qry = "SELECT EPIC,SharePrice FROM SharePrices WHERE EPIC = '$epic'";
      echo $qry;
      $getquote=@mysq l_query($qry);
      [/PHP]

      The other thing that I should mention is that all error messages returned from your mysql_query() call are being suppressed, so you will not know if there is an error in your query. Remove the @ sign if you feel there may be an issue with the MySQL syntax.

      Originally posted by captainmerton
      I am new to PHP but have been using it for about a week. I'm having no trouble using html forms to recall data from a MySQL table when the input type=text but i cant seem to find a way of recalling the data from the MySQL table when the input type=hidden.

      Here's the form code:

      <?php $action = $_REQUEST['action'];
      $epic = $_REQUEST['epic']; ?>

      <TR>
      <TD>

      <?php echo $action; ?>

      <INPUT TYPE="HIDDEN" NAME="action" VALUE="

      <?php
      echo $action; ?>

      "></TD>
      <TD>

      <?php
      echo $epic; ?>

      <INPUT TYPE="HIDDEN" NAME="epic" VALUE="

      <?php
      print $epic; ?>

      " SIZE="4"></TD>
      <TD><INPUT TYPE="TEXT" NAME="volume" SIZE="5"></TD>
      <TD> </TD>
      <TD> </TD>
      </TR>
      </TABLE>
      <P><INPUT TYPE="SUBMIT" VALUE="Get Quote"</P>
      </FORM>

      And the DB call:

      <?php $getquote=@mysq l_query("SELECT EPIC,SharePrice FROM SharePrices WHERE EPIC = '$epic'");

      The variable $epic is passed through first time in the URL from another page and is then displayed in the form as text but stored as hidden data as i dont want the user the ability to change it. The data comes back fine if I remove the where clause from the select or plug in a value eg. EPIC=ABC or set $epic=ABC before the query. The problem however is the data contained in $epic after it is passed through. i have used echo to display it and it looks ok. i have used various string function to remove spaces etc but all to no avail. The field EPIC is defined as CHAR(4) on the MySQL table and is the primary key.

      Anybody got any help?

      Cheers.

      Comment

      • captainmerton
        New Member
        • May 2007
        • 12

        #4
        Thanks for for the help. Took your advice and assigned my query to a variable first and also removed the @. Now i'm printing out th query I see that if assign a value to $epic before the query:

        $epic=ABC;

        I always get my data back and the query shows as:

        SELECT EPIC,SharePrice FROM SharePrices WHERE EPIC = 'ABC'

        However if i remove $epic=ABC; the query looks like:

        SELECT EPIC,SharePrice FROM SharePrices WHERE EPIC = ' ABC '

        with a space before and after the ABC. i tried TRIM($epic) but this made no difference. Any ideas?

        Comment

        • captainmerton
          New Member
          • May 2007
          • 12

          #5
          echo substr($epic,4, 3);

          This shows the data as ABC. I havent added spaces in front of this as far as I am aware anywhere. TRIM doesnt seem to remove the spaces.

          Comment

          • pbmods
            Recognized Expert Expert
            • Apr 2007
            • 5821

            #6
            Check the source that it outputs. It looks like you should have a whole bunch of [undesired] newline characters in there.

            Comment

            • captainmerton
              New Member
              • May 2007
              • 12

              #7
              There seems to be 4 blanks spaces in front of the variable $epic when its recalled from a url using REQUEST. However it has &amp to split up the variables. Is it just coinidence that this is 4 long. How can i strip out the blank spaces?

              Comment

              • pbmods
                Recognized Expert Expert
                • Apr 2007
                • 5821

                #8
                Originally posted by captainmerton
                There seems to be 4 blanks spaces in front of the variable $epic when its recalled from a url using REQUEST. However it has &amp to split up the variables. Is it just coinidence that this is 4 long. How can i strip out the blank spaces?
                Originally posted by captainmerton
                [HTML]
                <INPUT TYPE="HIDDEN" NAME="action" VALUE="



                <?php

                echo $action; ?>



                "></TD>
                [/HTML]
                Should be:

                [HTML]
                <INPUT TYPE="HIDDEN" NAME="action" VALUE="<?php echo $action; ?>"></TD>
                [/HTML]

                etc.

                Tabs and newlines in an input's value field count, just like they do in between <textarea> tags.

                Comment

                • captainmerton
                  New Member
                  • May 2007
                  • 12

                  #9
                  It works. Many Thanks pbmods.

                  Comment

                  • pbmods
                    Recognized Expert Expert
                    • Apr 2007
                    • 5821

                    #10
                    Originally posted by captainmerton
                    It works. Many Thanks pbmods.
                    You are many welcome. But be careful; they spend really quickly!

                    Comment

                    • Motoma
                      Recognized Expert Specialist
                      • Jan 2007
                      • 3236

                      #11
                      pbmods for the win!

                      Originally posted by captainmerton
                      It works. Many Thanks pbmods.

                      Comment

                      • Kailash12345
                        New Member
                        • Mar 2017
                        • 1

                        #12
                        how can i send input hidden data one page to another page and then send to database? can somebody help me?

                        Comment

                        Working...