...

View Full Version : PHP can't query with $_POST form previous page



rockyroad
06-27-2010, 07:08 PM
:mad:
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:


<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


<?
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

rfresh
06-27-2010, 07:15 PM
Probably $id is an integer data type.

Try this:



$id = (int)$_POST[choice];


or this



$id = $_POST["choice"];

Keleth
06-27-2010, 07:17 PM
Uh... you're not quoting a literal string.

Should be


$_POST['choice']

EDIT: Just noticed rfresh's second code block.

tomws
06-28-2010, 05:19 PM
Uh... you're not quoting a literal string.

Should be


$_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 (http://php.net/manual/en/language.types.array.php), 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.

Keleth
06-28-2010, 05:23 PM
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 :)

rockyroad
07-02-2010, 09:17 PM
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.

tomws
07-02-2010, 10:12 PM
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.

rockyroad
07-04-2010, 09:40 AM
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

rockyroad
07-04-2010, 11:25 AM
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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum