cant get this to work not sure if i have done it right or missing some thing what should happen you select from the box then the results shown but nothing happening <form name="search" method="post" action="<?=$PHP_SELF?>"> <Select NAME="location"> <Option VALUE="devon">Devon</option> <Option VALUE="cornwall">Cornwall</option> <Option VALUE="wales">Wales</option> </Select> <input type="hidden" name="searching" value="yes" /> <input type="submit" name="search" value="Select" /> </form> <?php // Connects to your Database mysql_connect("6", "c", "3") or die(mysql_error()) ; mysql_select_db("users") or die(mysql_error()) ; //Retrieves data from MySQL $data = mysql_query("SELECT * FROM members WHERE region='hire' and parklocation='location'") or die(mysql_error()); //Puts it into an array while($info = mysql_fetch_array( $data )) { $email = $info['email']; //Outputs the image and other data Echo "<img src=http://www.uk-caravan-hire.com/images/".$info['pname'] ." alt=\"Image\" align=\"left\" width=\"150px\" height=\"100px\" hspace=\"10px\" > <br>"; Echo "<b>Name:</b> ".$info['name'] . " <br>"; echo "<b>Email : </b><a href=mailto:$email>Click Here.</a><br>"; Echo "<b>Contact:</b> ".$info['contact'] . " <br>"; Echo "<b>Park Name:</b> ".$info['parkname'] . " <br>"; Echo "<b>Park Location:</b> ".$info['parklocation'] . " <br>"; Echo "<b>Details:</b> ".$info['caravandetails'] . " <br>"; if(empty($info[web])) { $web = "Not Provided" ; } else { $web = "<a href=$info[web]>Visit My Website</a> "; } Echo "<b>Web site : </b>$web<br>"; Echo "<b>Availability:</b> ".$info['avail'] . " <hr>"; } ?> PHP:
This looks wrong to me... $data = mysql_query("SELECT * FROM members WHERE region='hire' and parklocation='location'") or die(mysql_error()); PHP: That will try to select members with a region literally equal to the word 'hire' and a location literally equal to the word 'location'. Clearly, you won't have locations called "location" so it will return nothing. What it should do is this... $data = mysql_query("SELECT * FROM members WHERE region='hire' and parklocation='".$_POST['location']."'") or die(mysql_error()); PHP: which will insert the value from the form into the query.
$data = mysql_query("SELECT * FROM members WHERE region='hire' and parklocation='".mysql_real_escape_string($_POST['location'])."'") or die(mysql_error()); PHP:
thanks for info it worked sort of when the page loads it already has info which should not be there and after a query if you run another the first one is still there must need more work but thanks Doug if you want to see what i mean check it out www.uk-caravan-hire.com then for hire
Could you post the database table structure & data along with your current script? That'd help me work out what's going on.
ok here is the code <form name="search" method="post" action="<?=$PHP_SELF?>"> <Select NAME="location"> <option value="*">All</option> <Option VALUE="devon">Devon</option> <Option VALUE="cornwall">Cornwall</option> <Option VALUE="wales">Wales</option> <option value="scotland">Scotland</option> </Select> <input type="hidden" name="searching" value="yes" /> <input type="submit" name="search" value="Search" /> </form> <?php // Connects to your Database mysql_connect("6", "c", "3") or die(mysql_error()) ; mysql_select_db("c") or die(mysql_error()) ; //Retrieves data from MySQL $data = mysql_query("SELECT * FROM members WHERE region='hire' or region='both' and parklocation ='".mysql_real_escape_string($_POST['location'])."'") or die(mysql_error()); //Puts it into an array while($info = mysql_fetch_array( $data )) { $email = $info['email']; //Outputs the image and other data Echo "<img src=http://www.uk-caravan-hire.com/images/".$info['pname'] ." alt=\"Image\" align=\"left\" width=\"150px\" height=\"100px\" hspace=\"10px\" > <br>"; Echo "<b>Name:</b> ".$info['name'] . " <br>"; echo "<b>Email : </b><a href=mailto:$email>Click Here.</a><br>"; Echo "<b>Contact:</b> ".$info['contact'] . " <br>"; Echo "<b>Park Name:</b> ".$info['parkname'] . " <br>"; Echo "<b>Park Location:</b> ".$info['parklocation'] . " <br>"; Echo "<b>Details:</b> ".$info['caravandetails'] . " <br>"; if(empty($info[web])) { $web = "Not Provided" ; } else { $web = "<a href=$info[web]>Visit My Website</a> "; } Echo "<b>Web site : </b>$web<br>"; Echo "<b>Availability:</b> ".$info['avail'] . " <hr>"; } ?> PHP: file to big to attach
Your updated SQL statement has a problem - adding the OR has changed how it works. Change it to: $data = mysql_query("SELECT * FROM members WHERE (region='hire' or region='both') and parklocation ='".mysql_real_escape_string($_POST['location'])."'") or die(mysql_error()); Code (markup):
thanks buddy that works ok now can you tell me how to make it show all if required i put * cheers Doug
You just need an if statement to change the SQL query, this should do the trick: //Retrieves data from MySQL $strLocation = mysql_real_escape_string($_POST['location']); if ($strLocation == "*") { $data = mysql_query("SELECT * FROM members WHERE region='hire' OR region='both'") or die(mysql_error()); } else { $data = mysql_query("SELECT * FROM members WHERE (region='hire' OR region='both') AND parklocation='$strLocation'") or die(mysql_error()); } PHP: Another slightly shorter variation: //Retrieves data from MySQL $strLocation = mysql_real_escape_string($_POST['location']); $strQuery = "SELECT * FROM members WHERE (region='hire' OR region='both')"; if ($strLocation != "*") { $strQuery .= " AND parklocation='$strLocation'"; } $data = mysql_query($strQuery) or die(mysql_error()); PHP: Sorry, had a typo in 2nd PHP snippet.