Forum Moderators: coopster

Message Too Old, No Replies

PHP searching MySQL database for estate agents site

Building my own estate agents web site

         

grandman

12:50 pm on Oct 5, 2005 (gmt 0)

10+ Year Member



I have a search form for site users to search a MySQL db to find property. The form has three parts.
1) search form (user interface)
2) Form handler script (to be run from cgi bin)
3) Response page (shows search results)

I'm just learning php. The data from the form comes through to the script in an array:
Array ( [$choose] => Array ( [0] => rent ) [$counties] => Array ( [0] => cambridgeshire ) [$cities] => Whittlesey [$type] => Array ( [0] => detached_house ) [$bed] => Array ( [0] => more ) [$lounge] => Array ( [0] => 2 ) [$value] => Array ( [0] => 450001 to 750000 ) [$Submit] => Submit )

I think I need to use the explode syntax, to first change this to information I can use, but not sure how? then I think I would need to add each piece of info to a vars. Can ayone explain how to do this. At present have tried to do what I said, it does nothing?

DG)

jatar_k

3:52 pm on Oct 5, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld grandman,

is that the contents of the $_POST array?

grandman

5:38 pm on Oct 5, 2005 (gmt 0)

10+ Year Member



Hello jatar_k;

Yes that is the result os ($_POST['']);
As far as the array is concerned eack item is actually singular. The form handling script [what there is of it] looks like this at present.

<?php
//connect
require_once('link_conn_use_.php');
//response page
include_once('link_homes_response_headder.php');
//declare vaiables
$post = ($_POST["post"]);
$classic = ($_POST["classic"]);
$choose = ($_POST["choose"]);
$counties = ($_POST["counties"]);
$cities = ($_POST["cities"]);
$type = ($_POST["type"]);
$bed = ($_POST["bed"]);
$lounge = ($_POST["lounge"]);
$value = ($_POST["value"]);
$submit = ($_POST["submit"]);

//check query and source of query
if (isset($_GET['source']))
if ($_GET['source'] == 'link_homes_form_mainsearch_body_index.php')

//check a choice has been made
if (isset($_POST['choose'])){

$choose = stripslashes($_POST['choose']); //clean up any slashes
$choose = trim($_POST['choose']); // clean up any under scores and spaces
}
else
{
$choose = NULL;
}
$open = $_POST['choose']; //assign choose to a new variable
if($open[0] == 'rent')
{
mysql_select_db (house_rent_link) OR die ('Could not select the database: '. mysql_error());
// connect to rent database
}
elseif($open[0] == 'buy')
{
mysql_select_db (house_sales_link) OR die ('Could not select the database: '. mysql_error());
// connect to buy database
}
else
{
($_POST['no'] = '<p><b>You must select either rent or buy.<b/></p>'); // if no db selected issue error message
}

echo ($_POST['no']);

?>

I've been trying to get the error message $no to print in the response headder, no luck with that either,

DG)

jatar_k

6:43 pm on Oct 5, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



what I need to understand is what is the html for this element?

$_POST["choose"]

radio? checkbox?

I am seeing what looks like a logic issue. I assume also that is the real control var for what is going to happen in your script.

grandman

7:11 pm on Oct 5, 2005 (gmt 0)

10+ Year Member



Hello jatar_k,

I've not even thought about constructing the WHERE clause as yet [not successfully anyway] ($_POST['choose']); is just the name of the first select field. The form consists of seven select fields, named:
1, choose
2, county
3, city
4, type
5, bed
6, lounge
7, value

It is a single select [multiple option selection no].
There are two db's one for house sales, another for rentals. A selection must be made in the first field [named $choose] to activate the search form, as this performs the connection to one of the db's.

David

[edited by: jatar_k at 7:14 pm (utc) on Oct. 5, 2005]
[edit reason] no urls thanks [/edit]

jatar_k

7:32 pm on Oct 5, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



well first thing I notice is this

<select class="logolaaddchoose" name="choose[]">
<option value="choose">First</option>
<option value="rent">Rent</option>
<option value="buy">Buy</option>
</select>

your naming is wrong, it shouold be

<select class="logolaaddchoose" name="choose">

you don't need an array notation there as there will only ever be a single selection. Try changing that, it should change the manner in which your $_POST array is formatted

grandman

8:46 pm on Oct 5, 2005 (gmt 0)

10+ Year Member



Hello jatar_k

This is the new array produced by that change:
Array ( [choose] => rent [counties] => cheshire [cities] => Runcorn [type] => detached_house [bed] => 3 [lounge] => 2 [value] => 260001 to 300000 [Submit] => Submit ) There no longer an index, so how would I now refer to this whithin the script. Refer my script posted:
open = $_POST['choose']; //assign choose to a new variable
if($open[0] == 'rent')
index=[0]

David

grandman

11:51 am on Oct 6, 2005 (gmt 0)

10+ Year Member



Hello jatar_k,

This is what I have so far:

<?php
//connect
require_once('link_conn_use_.php');
//response page
include_once("link_homes_response_headder.php");
//declare vaiables
$p1 = ($_POST["post"]);
$cl = ($_POST["classic"]);
$ch = ($_POST["choose"]);
$co = ($_POST["counties"]);
$ci = ($_POST["cities"]);
$ty = ($_POST["type"]);
$be = ($_POST["bed"]);
$lo = ($_POST["lounge"]);
$va = ($_POST["value"]);
$su = ($_POST["submit"]);

$ch = '[choose]';
$co = '[counties]';
$ci - '[cities]';
$ty = '[type]';
$be ='[bed]';
$lo = '[lounge]';
$va = '[value]';
$su = '[submit]';

$errormessage = ($_POST['errormessage']);
$errormessage = NULL;

//check query and source of query
if (isset($_GET['source']))
if ($_GET['source'] == 'link_homes_form_mainsearch_body_index.php')

//check a choice has been made
if (isset($_POST['choose'])){

$choose = stripslashes($_POST['choose']); //clean up any slashes
$choose = trim($_POST['choose']); // clean up any under scores
}
else
{
$choose = NULL;
}
$open = $_POST['choose']; //assign choose to a new variable
if($open == 'rent')
{
mysql_select_db (house_rent_link) OR die ('Could not select the database: '. mysql_error());
// connect to rent database
}
elseif($open == 'buy')
{
mysql_select_db (house_sales_link) OR die ('Could not select the database: '. mysql_error());
// connect to buy database
}
else
{
($_POST['no'] = '<p><b>You must select either rent or buy.<b/></p>'); // if no db selected issue error message
}

echo ($_POST['no']);

//set up error reporting system
if (isset($co)){
$co = stripslashes($co);
$co = trim($co);
}
else
{
$co = NULL;
}
echo 'you must select a county';

if (isset($ci)){
$ci = stripslashes($ci);
$ci = trim($ci);
}
else
{
$ci = NULL;
}
echo 'you must select a county first, then a town.';

if (isset($ty)){
$ty = stripslashes($ty);
$ty = trim($ty);
}
else
{
$ty = NULL;
}
echo 'You must select a house type';

if (isset($be)){
$be = stripslashes($be);
$be = trim($be);
}
else
{
$be = NULL;
}
echo 'You must select the number of bedrooms you require';

if (isset($lo)){
$lo = stripslashes($lo);
$lo = trim($lo);
}
else
{
$lo = NULL;
}
echo 'You must select the number of bedroom you require';

if (isset($va)){
$va = stripslashes($va);
$va = trim($va);
$va = number_format($va);
$va = round($va);
}
else
{
$va = NULL;
}
echo 'You must select a price range for your new home';

?>

I wondered if you could confirm I'm on the right lines?

Also I can't get any error messages to print out in the response headder, is it not possible to have a third page in this? They keep printing on the form handler under the response page...
David

jatar_k

4:27 pm on Oct 6, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



to answer the first question

There no longer an index, so how would I now refer to this whithin the script. Refer my script posted:
$open = $_POST['choose']; //assign choose to a new variable
if($open[0] == 'rent')

that's why I saw it the above is just strange, it should be

open = $_POST['choose'];
if($open == 'rent')

for single values passed from a form you shouldn't need an array index as it should not be an array

this appears at the top
include_once("link_homes_response_headder.php");

that seems to be the response page, all of the echo'ing of error messages comes after that include therefore they are being output exactly wher you told them to be. ;)

what I normally do is append all of my error strings to a variable, then include my content. Inside the content I check for the existence of the error var and output if it is not empty

for example
I would initialize the error var at the top

$errormsg = '';

then a check would be like so

if (isset($ci)){
$ci = stripslashes($ci);
$ci = trim($ci);
} else {
$ci = NULL;
$errormsg = '<br>you must select a county first, then a town.';
}

then in my content I would have

if (!empty($errormsg)) {
echo '<p class="error">',$errormsg;
}

that's how I do it

grandman

6:21 pm on Oct 6, 2005 (gmt 0)

10+ Year Member



Hello jatar_k,

Thank you very much for your help in this matter. One other question, before I start wasing my time, if you please. If I have no array, then I shouldn't have to loop through one, so does that mean, that all I have to do is something like:
if $co == ' '; && $open == 'rent'{
$co = $query;
}

$query = "SELECT field_name FROM table_name where()";

Thank you,

David

jatar_k

6:35 pm on Oct 6, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I think so, it's really hard to say because the logic of the script is kinda murky

grandman

6:54 pm on Oct 6, 2005 (gmt 0)

10+ Year Member



Hello jatar_k,

When you say logic, I'm not sure I understand?
The sell db has four tables:
1/ owners_sellers
2/ property_for_sale
3/ apartments
4/ rooms
The reason for the apartments is they can have many different types, ie penthouse, ground-floor, top-floor, etc.

The room table is because a house has many rooms,
lounge, dinning, bed, kitchen etc

and each room many attributes
state of decor, south facing etc, height, width, length etc.

The reantal db has those plus
5/ long let
6/ short let
7/ holiday let
8/ seasons

I split these even though I'm not certain it was necessary, but just felt it kept the db clean.

The site is for a web based only estate agents, so people will be up-loading their own properties, and I also wanted to try and limit the poetic licemce when it come to describing their own homes...

Is this what you mean by logic?

David

grandman

7:42 pm on Oct 6, 2005 (gmt 0)

10+ Year Member



Hello jatar_k,

Sorry to come back, I must be doing something wrong! can't get the error reporting to work at all? A;; error messages have stopped, in the source I have put the opening <?php errormsg;?>

David

jatar_k

10:22 pm on Oct 6, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



<?php errormsg;?>

bad syntax

<?php echo errormsg;?>

grandman

9:32 am on Oct 7, 2005 (gmt 0)

10+ Year Member



Hello jatar_k,

I've altered the script, still not completely sure what you meant by "logic murky"

<bunch of variable tests - jatar_k>

$errormsg = '<p><b>You must select either rent or buy.<b/></p>'; // if no db selected issue error message
$errormsg = '<br />you must select a county';
$errormsg = '<br />you must select a county first, then a town';
$errormsg = '<br /> You must select a house type';
$errormsg = '<br /> You must select the number of bedrooms you require';
$errormsg = '<br /> You must select the number of reception rooms in your new home';
$errormsg = '<br />You must select a price for your new home';
?>

Have moved all error messsages to end of script, if you look at this part:

//set up error reporting system
if (isset($co)){
$co = stripslashes($co);
$co = trim($co);
}
elseif
{
$co = NULL;

}
elseif
{
$co = '' && $open = 'rent';//set variable for rent query
$co = $c0q;
}
elseif
{
$co = '' && $open = 'buy';//set variable for buy query
$co = $coq1;
}

I am trying to find away of saying, If they have selected a county, and have nominated the rent db, then search the rent db to find a match for there selection. BTW the error repoting still will not work, get no error messages?

Have basic response page:

<body id="body">
<div id="wrapnew">
<div id="dispalyheadder" >
<div id="displayheadderinner" class="headdertextaddnew1" >
<p>THANK YOU FOR USING LINK HOMES 2000 TO FIND YOUR NEW HOME. PLEASE CALL AGAIN</p>
</div>
</div>
<div id="rightsidemain1">
<?php if (!empty($_POST['errormsg'])) {
echo ($_POST['errormsg']);
}

?>
</div>
</div>
Have also tried echo $errormsg [removed ($_POST replced with $errormsg]

David

[edited by: jatar_k at 3:55 pm (utc) on Oct. 7, 2005]
[edit reason] reduced code dump [/edit]

jatar_k

4:00 pm on Oct 7, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



alright I will break it down for you, it will take a couple posts here so none are too big

all of this code makes no sense to me

$ch = '[choose]';
$co = '[counties]';
$ci - '[cities]';
$ty = '[type]';
$be ='[bed]';
$lo = '[lounge]';
$va = '[value]';
$su = '[submit]';

//check query and source of query
if (isset($_GET['source']))
if ($_GET['source'] == 'link_homes_form_mainsearch_body_index.php')

there is no point to it and the syntax is not right on those ifs

jatar_k

4:48 pm on Oct 7, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



you do your assignments from the $_POST array at thr top but then farther down in the script you are still working wiht post vars

ie

if (isset($_POST['choose'])) {
$choose = stripslashes($_POST['choose']);
$choose = trim($_POST['choose']);
} else {
$choose = NULL;
}

at this point you now have $choose and $ch both with similar values, one unvalidated, one validated

and neither is used any where later in the script because you then assign the same post var to $open

$open = $_POST['choose']; //assign choose to a new variable

which is also unvalidated

you have multiple occurrences of improper else if syntax

} elseif {

you need to have a conditional statement with them, especially when there are multiple else if's attached to the same original if.

this chunk
$errormsg = '<p><b>You must select either rent or buy.<b/></p>'; // if no db selected issue error message
$errormsg = '<br />you must select a county';
$errormsg = '<br />you must select a county first, then a town';
$errormsg = '<br /> You must select a house type';
$errormsg = '<br /> You must select the number of bedrooms you require';
$errormsg = '<br /> You must select the number of reception rooms in your new home';
$errormsg = '<br />You must select a price for your new home';

just reassigns new values to the $errormsg var over and over. if you want to append use .= and those strings should be assigned in the test to see if the var is empty as opposed to just a big unrelated chunk

assigning a NULL value to vars that are already not set is unnecessary

$ty = NULL;

there are a couple of instances of that, those whole chunks don't make too much sense either

if (isset($lo)){
$lo = stripslashes($lo);
$lo = trim($lo);
} else {
$lo = NULL;
}

you could trim it before the test to make sure they didn't just submit a space. Then in the else statement you could append your error string to your error var since that means the value is missing.

those are a few issues. You can't test your script at the moment because it's behaviour is random.

grandman

7:23 pm on Oct 7, 2005 (gmt 0)

10+ Year Member



Hello jatar_k,

Thank you for you're assistance,

I've altered the script as follows:

<?php
$error = ($_POST['error']);
$error = '';
//connect
require_once('link_conn_use_.php');
//response page
include_once("link_homes_response_headder.php");
//declare vaiables
$p1 = ($_POST["p1"]);
$cl = ($_POST["cl"]);
$ch = ($_POST["ch"]);
$co = ($_POST["co"]);
$ci = ($_POST["ci"]);
$ty = ($_POST["ty"]);
$be = ($_POST["be"]);
$lo = ($_POST["lo"]);
$va = ($_POST["va"]);
$su = ($_POST["su"]);

$ch = '[choose]';
$co = '[counties]';
$ci - '[cities]';
$ty = '[type]';
$be ='[bed]';
$lo = '[lounge]';
$va = '[value]';
$su = '[submit]';

//check query and source of query
if (isset($_GET['source']));
if ($_GET['source'] == 'link_homes_form_mainsearch_body_index.php'){

//check a choice has been made
if (isset($ch)){
$ch = stripslashes($ch); //clean up any slashes
$ch = trim($ch); // clean up any under scores
}
elseif($ch == 'rent')
{
mysql_select_db (house_rent_link) OR die ('Could not select the database: '. mysql_error());
// connect to rent database
}
elseif ($ch == 'buy'){

mysql_select_db (house_sales_link) OR die ('Could not select the database: '. mysql_error());
// connect to buy database
}else{
$ch = NULL;
$error = 'You must select either buy or rent from first field';
}
if ($ch == 'rent'){
$ch = $cq1;
$cq1 = ($_POST['cq1']);
}
if ($ch == 'buy'){
$ch = $cq2;
$cq2 = ($_POST['cq2']);
}

if (isset($co)){
$co = stripslashes($co);
$co = trim($co);
}
elseif ($co = NULL){
$error = 'You must select a county first.';
}elseif($co = ''){
$cq1 && $co = $query1;
}else{
$cq2 && $co = $query2;
}

if (isset($ci)){
$ci = stripslashes($ci);
$ci = trim($ci);
}
elseif ($ci = NULL){
$error ='First select a county, then a town. To help you find you\'re new home';
}elseif($ci = ''){
$cq1 && $ci = $query3;
}else{
$cq2 && $ci = $query4;
}

if (isset($ty)){
$ty = stripslashes($ty);
$ty = trim($ty);
}
elseif($ty = NULL){
$error = 'Please select the type of property, you are searching for';
}elseif($ty = ''){
$cq1 && $ty = $query5;
}else{
$cq2 && $ty = $query6;
}

if (isset($be)){
$be = stripslashes($be);
$be = trim($be);
}
elseif($be = NULL){
$error = 'Please select the number of bedrooms, you would like in you\'re new home';
}elseif($be = ''){
$cq1 && $be = $query7;
}else{
$cq2 && $be = $query8;
}

if (isset($lo)){
$lo = stripslashes($lo);
$lo = trim($lo);
}
elseif($lo = NULL){
$error = 'Please select the number of lounges you\'re new home should have';
}elseif($be = ''){
$cq1 && $lo = $query9;
}else{
$cq2 && $lo = $query10;
}

if (isset($va)){
$va = stripslashes($va);
$va = trim($va);
$va = number_format($va);
$va = round($va);
}
elseif($va = NULL){
$error = 'Most important! how much do you wish to pay for you\'re new home?';
}elseif($va = ''){
$cq1 && $va = $query11;
}else{
$cq2 && $va = $query12;
}
}
?>
See next post for explanation/

grandman

7:30 pm on Oct 7, 2005 (gmt 0)

10+ Year Member



Hello jatar_k,

The top of script I'm trying to:
1/ register globles for those vars
2/ assign the names as they come from the form to the same vars. I thought you had to do this?

The code:if (isset($_GET['source']));
if ($_GET['source'] == 'link_homes_form_mainsearch_body_index.php'){
I'm am trying to use this to verify that all info comming to the script is from the form. I'm also using all of the issets and stripslashes for this purpose, I realise that the information is hard coded into the form, but want as much protection as possible. It is also possible that I have the error messages in the wrong place, but dont know where else to place them?

Finally all the $query's I hope to use in running MySQL query when writing the where clause?

David