Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New to the CF scene
    Join Date
    Jun 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    PHP can't query with $_POST form previous page


    Been doing this all week and can't seem to get pass this issue.

    1. Page 1(HTML page): I have a option box that has a user select a option then submit. Submitting sends the keyword 'choice' with the value that corresponds to the option the user chooses.

    2. Page 2(PHP Page): I $_POST[choice] data from previous and put into $id then use it in a query. By using this method, it fails to display the picture(blob) that it queries. However, if I specify a value to $id with "$id='2'; it works fine and I get the pictures to display. When I use "$id=$_POST[choice], the pictures are not displayed but instead has a small red x in the upper corner.

    Can anyone point out what is going wrong. I tried several ideas but was unable to make it work. I know for a fact that it works because when I specify the values, it works fine. It is only when I try to use post in the php page is when the pictures are not showing.

    Here is the code below:
    =========================================================
    Page1.html:
    Code:
    <html>
    <head>
    <title>Selection Page</title>
    </head>
    
    <body>
        Make a selections:<br>
        <form action="page2.php" method="post" name="name">
        	<select name="choice">
            	<option value="2">Number 2</option>
                <option value="3">Nunber 3</option>
            </select>
            <input type="submit" name="submit" value="Update">
        </form>
    </body>
    </html>
    ================================================
    Page2.php
    Code:
    <?
    if (! @mysql_connect("localhost","username","Password")) {
            $errmsg = "Cannot connect to database";
            }
    
    $id = 2;//using this works fine
    //$id = $_POST[choice]; using this does not.
    
    echo $id;
    @mysql_select_db("db");
    
    
    if ($_REQUEST[set] == 1) {	
    	$gotten = @mysql_query("select * from storepic where id='$id'");
    	
    	if ($row = @mysql_fetch_assoc($gotten)) {
            $bytes = $row[indoorpic];
    		} else {
            $bytes = $row[defualtpic];
    		}
        header("Content-type: image/jpeg");
        print $bytes;
    }
    ?>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Picture</title>
    </head>
    
    <body>
    test
    <table width="453" border="1" cellpadding="5%" align="center">
      <tr align="center" valign="top">
        <td width="435">
        <center><img src=?set=1 width=300><br>
        </td>
       </tr>
    </table>
    </body>
    </html>
    ========================================================
    If anyone can help me figure what went wrong. It would be most helpful.

    rockyroad

  • #2
    Regular Coder
    Join Date
    Jun 2007
    Location
    Los Angeles
    Posts
    545
    Thanks
    81
    Thanked 5 Times in 5 Posts
    Probably $id is an integer data type.

    Try this:

    PHP Code:
    $id = (int)$_POST[choice]; 
    or this

    PHP Code:
    $id $_POST["choice"]; 
    RalphF
    Business Text Messaging Services
    https://www.MobileTextingService.com

  • #3
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,530
    Thanks
    45
    Thanked 259 Times in 256 Posts
    Uh... you're not quoting a literal string.

    Should be

    PHP Code:
    $_POST['choice'
    EDIT: Just noticed rfresh's second code block.

  • #4
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    Quote Originally Posted by Keleth View Post
    Uh... you're not quoting a literal string.

    Should be

    PHP Code:
    $_POST['choice'
    It should be for the sake of correctness, but it will still work without the quotes. This is a side-effect that can break in the future. See the PHP man page on arrays, specifically the "Why is $foo[bar] wrong?" section, where it says:
    This is wrong, but it works. The reason is that this code has an undefined constant (bar) rather than a string ('bar' - notice the quotes). PHP may in future define constants which, unfortunately for such code, have the same name. It works because PHP automatically converts a bare string (an unquoted string which does not correspond to any known symbol) into a string which contains the bare string . For instance, if there is no defined constant named bar, then PHP will substitute in the string 'bar' and use that.
    There's a lot of sloppy code and sloppy tutorials out there that do it incorrectly.

    What the OP really needs to do is debug. Dump the $_POST array, remove error suppression, print the query string, and check the query result.
    Are you a Help Vampire?

  • #5
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,530
    Thanks
    45
    Thanked 259 Times in 256 Posts
    Quote Originally Posted by tomws View Post
    It should be for the sake of correctness, but it will still work without the quotes. This is a side-effect that can break in the future.
    That's very true, but when code doesn't work how you expect it to, generally trying to make sure its semantically correct helps

  • #6
    New to the CF scene
    Join Date
    Jun 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Tried your suggestions.

    All,

    Thanks for the replies. So I have tried the following:

    (int)$_POST[choice]
    (int)$_POST['choice']
    (int)$_POST["choice"]
    $_POST[choice]
    $_POST['choice']
    $_POST["choice"]

    It's all getting the value option from the first page. I know this because I had the code echo the $id value on the page to ensure that it is captured from the previous page.

    All of you are correct and it all works. The values are captured. I think it has to do with the <img src=> section. I just have strange feeling that the problem may be that the code does not pass the value in $id to the query when calling the page to retrieve the img from the DB.

    The DB [blob]image is fine and prints perfectly when $id is forced to 2 with $id = 2. With $id = $_POST[choice] and <img src=> is called, the id seems to be dropped and there are no pictures printed.

    It may be a sequence issue that the html codes are being executed first before the $id = $_POST[choice] is captured in the PHP code.

    Any more ideas?Thanks in advance.

  • #7
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    Check the last sentence from my previous post:
    What the OP really needs to do is debug. Dump the $_POST array, remove error suppression, print the query string, and check the query result.
    Are you a Help Vampire?

  • #8
    New to the CF scene
    Join Date
    Jun 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Quotes from previous post.

    Hey tomws,

    As stated "Dump the $_POST array, remove error suppression, print the query string, and check the query result. "

    Do you mean to tell me that I should use:

    1.var_dump($_POST) in the beginning of the code.
    2.Then to remove error suppression(@).
    3.Then when I get to the query portion of the code, print out the code before executing to make sure the query is correct.
    4.Then see if the query produced the image I wanted after executing.

    I think that was what you are telling me.

    I'll give it a shot and see where it takes me.

    rockyroad

  • #9
    New to the CF scene
    Join Date
    Jun 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks to everyone who helped me with suggestions. I finally figured what was wrong with the code. It wasn't the syntax or expressions in the code. I just didn't send the other variable value over the url as I do with [gim]. After adding an additional variable by appending after[gim] everything turned out ok.

    THANKS to all that helped out. I really appreciate it.

    rockyroad


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •