469,609 Members | 1,657 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,609 developers. It's quick & easy.

Form submit POST & php on one file problem.

For some reason the submit POST and php on one form doesn't work for me.

Here's my form code (it's dutch)

[html] <form method="POST">
<select name="locatie" size="1">
<option value="">Kies de locaties</option>
<option>Parijs</option>
<option>Lyon</option>
<option>Marseile</option>
<option>Lile </option>

</select>
<br>
<br>
<select name="prijs" size="1">
<option value="">Kies de prijzen</option>
<option value="1">0-100.000 </option>
<option value="2">100.00-200.00 </option>
<option value="3">200.000-300.000 </option>
<option value="4">300.000-400.000 </option>
<option value="5">400.000-500.000 </option>
<option value="6">500.000-600.000 </option>
<option value="7">600.000 en meer </option>

</select>
<br>
<br>
<input type="submit" name="button" id="button" value="Zoeken" />
</form>[/html]


And here's my php code :

[PHP]<?
ERROR_REPORTING(E_ALL);
include("config.php");
if(isset($_POST['button']))
{
$location = $_POST['locatie'];
$price = $_POST['prijs'];

//check what price it is
if($locatie == ''){
echo"Foute locatie gekozen, Kies aub een goede locatie.";
}

if($price == ''){
echo"Foute prijs gekozen. Kies aub een goede prijs.";
}

elseif($price == '1'){


$query = "SELECT * FROM houses WHERE location = '" . $location . "' AND price >= 0 AND price <= 100000";


}

elseif($price == '2'){
$query = "SELECT * FROM houses WHERE location = '" . $location . "' AND price >= 100000 AND price <= 200000";
}

elseif($price == '3'){
$query = "SELECT * FROM houses WHERE location = '" . $location . "' AND price >= 200000 AND price <= 300000";
}

elseif($price == '4'){
$query = "SELECT * FROM houses WHERE location = '" . $location . "' AND price >= 300000 AND price <= 400000";
}

elseif($price == '5'){
$query = "SELECT * FROM houses WHERE location = '" . $location . "' AND price >= 400000 AND price <= 500000";
}

elseif($price == '6'){
$query = "SELECT * FROM houses WHERE location = '" . $location . "' AND price >= 500000 AND price <= 600000";
}

elseif($price == '7'){
$query = "SELECT * FROM houses WHERE location = '" . $location . "' AND price <= 600000";
}

while($results = mysql_fetch_array($query))
{

echo "<table width='434' height='72' border='0'>";

echo "<tr><td width='133' height= '130'>";

echo "<img width='132' height='129' src='" . $results['Foto1'] . "'></img>";

echo "</td><td class='style5'>";

echo "Type: " . $results['type'] . "<br>Descriptie:" . $results['description'];

echo "</td><td class='style5'>";

echo "prijs: " . $results['price'] . ".-";

echo "</td></tr>";

echo "</table>";

}
}
?>[/PHP]


Any idea why it doesn't work?

Help is much appreciated.
Nov 10 '07 #1
8 1969
Atli
5,058 Expert 4TB
Hi Dave.

First of all, I'd recommend changing the <? tag in your PHP code into <?php. The short-tags version you are currently using is disabled by default in PHP and can cause problems on other servers.

As for your problem. The <form> tag is missing the 'action' property. Without that the form won't know where to send the data. So even if the data is meant to be posted to the current page, set the 'action' property.

For example:
Expand|Select|Wrap|Line Numbers
  1. <form action="thisPage.php" method="POST">
  2.  
Nov 10 '07 #2
Hi Dave.

First of all, I'd recommend changing the <? tag in your PHP code into <?php. The short-tags version you are currently using is disabled by default in PHP and can cause problems on other servers.

As for your problem. The <form> tag is missing the 'action' property. Without that the form won't know where to send the data. So even if the data is meant to be posted to the current page, set the 'action' property.

For example:
Expand|Select|Wrap|Line Numbers
  1. <form action="thisPage.php" method="POST">
  2.  
Thanks, That worked.

But now its nagging with this error for some reason:

Warning: mysql_fetch_object(): supplied argument is not a valid MySQL result resource in /home/content/C/o/l/Collussus/html/school/ict/search.php on line 213

this is my php(i left the tags on <? ?> because i know my server allows it. :

[PHP]<?
ERROR_REPORTING(E_ALL);
include("config.php");
if(isset($_POST['button']))
{
$location = $_POST['locatie'];
$price = $_POST['prijs'];

//check what price it is
if($locatie == ''){
echo"Foute locatie gekozen, Kies aub een goede locatie.<br>";
}

if($price == ''){
echo"Foute prijs gekozen. Kies aub een goede prijs.";
}

elseif($price == '1'){


$query = "SELECT * FROM houses WHERE location = '" . $location . "' AND price >= 0 AND price <= 100000";


}

elseif($price == '2'){
$query = "SELECT * FROM houses WHERE location = '" . $location . "' AND price >= 100000 AND price <= 200000";
}

elseif($price == '3'){
$query = "SELECT * FROM houses WHERE location = '" . $location . "' AND price >= 200000 AND price <= 300000";
}

elseif($price == '4'){
$query = "SELECT * FROM houses WHERE location = '" . $location . "' AND price >= 300000 AND price <= 400000";
}

elseif($price == '5'){
$query = "SELECT * FROM houses WHERE location = '" . $location . "' AND price >= 400000 AND price <= 500000";
}

elseif($price == '6'){
$query = "SELECT * FROM houses WHERE location = '" . $location . "' AND price >= 500000 AND price <= 600000";
}

elseif($price == '7'){
$query = "SELECT * FROM houses WHERE location = '" . $location . "' AND price <= 600000";
}

$results = mysql_query($query);
while($results = mysql_fetch_object($results))
{

echo "<table width='434' height='72' border='0'>";

echo "<tr><td width='133' height= '130'>";

echo "<img width='132' height='129' src='" . $results['Foto1'] . "'></img>";

echo "</td><td class='style5'>";

echo "Type: " . $results['type'] . "<br>Descriptie:" . $results['description'];

echo "</td><td class='style5'>";

echo "prijs: " . $results['price'] . ".-";

echo "</td></tr>";

echo "</table>";

}
}
?>[/PHP]
Nov 10 '07 #3
Atli
5,058 Expert 4TB
These two lines are the source of your problem:
Expand|Select|Wrap|Line Numbers
  1. $results = mysql_query($query);
  2.     while($results = mysql_fetch_object($results))
  3.  
You are overwriting the database result resource with the return value of the mysql_fetch_object() function. So on the next loop, the $results is not a valid MySQL resource, causing the function to fail and a warning to be printed.

You need to change the name of the variable you use to store the current row of data.
Nov 10 '07 #4
These two lines are the source of your problem:
Expand|Select|Wrap|Line Numbers
  1. $results = mysql_query($query);
  2.     while($results = mysql_fetch_object($results))
  3.  
You are overwriting the database result resource with the return value of the mysql_fetch_object() function. So on the next loop, the $results is not a valid MySQL resource, causing the function to fail and a warning to be printed.

You need to change the name of the variable you use to store the current row of data.

Fixed that, still same problem.
Nov 10 '07 #5
Atli
5,058 Expert 4TB
Have you check out if there was an error in your SQL?
Your code does not validate that the query executed successfully.

Try adding something like this before you try to use the result from the database:
Expand|Select|Wrap|Line Numbers
  1. if(!$result) {
  2.   echo "<pre><b>MySQL Query failed!</b>\n
  3.            <b>Error:</b> ". mysql_error() ."\n
  4.            <b>Query:</b> $query
  5.         </pre>";
  6. }
  7.  
Nov 10 '07 #6
Have you check out if there was an error in your SQL?
Your code does not validate that the query executed successfully.

Try adding something like this before you try to use the result from the database:
Expand|Select|Wrap|Line Numbers
  1. if(!$result) {
  2.   echo "<pre><b>MySQL Query failed!</b>\n
  3.            <b>Error:</b> ". mysql_error() ."\n
  4.            <b>Query:</b> $query
  5.         </pre>";
  6. }
  7.  
Fixed the first error.

Now it just gives me this error:

MySQL Query failed!

Error:

Query: SELECT * FROM houses WHERE region = 'Parijs' AND price >= 0 AND price <= 100000
Nov 10 '07 #7
Atli
5,058 Expert 4TB
Note that the variable I used in my example was called $result, while the MySQL result in your code is called $results. (Force of habit for me. I always call mine $result :P)
Nov 10 '07 #8
That fixed it. Thanks.
Nov 10 '07 #9

Post your reply

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

Similar topics

4 posts views Thread by Nomen Nescio | last post: by
2 posts views Thread by iwasinnihon | last post: by
2 posts views Thread by Martin Nadoll | last post: by
4 posts views Thread by Alex Sibilev | last post: by
2 posts views Thread by Cerebral Believer | last post: by
reply views Thread by devrayhaan | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.