PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
include "dbconn.php";
$town = $_POST['town'];
$area = $_POST['area'];
$result = mysql_query ("SELECT * FROM lincssolicitor WHERE town = '$town' AND area LIKE '%$area%'");
if (mysql_num_rows($result)==0) { echo "Sorry, we have no registered solicitors meeting your criteria. Please try a different area of law, or a different town."; }
else {
while ($row = mysql_fetch_object($result)){
echo "<div class='librarybox' style='padding-left: 10px'>
<div class='faq_title'>$row->company</div>
<table width='670' border='0' cellspacing='0' cellpadding='0' class='bodytext'>
<tr>
<td valign='top' width='95'>Name:</td>
<td>$row->name</td>
<td>Address:</td>
<td rowspan='3' valign='top' width='275'>$row->address</td>
</tr>
<tr>
<td valign='top'>Email:</td>
<td><a href='mailto:$row->email'>$row->email</a></td>
<td> </td>
</tr>
<tr>
<td valign='top'>Telephone:</td>
<td>$row->telephone</td>
<td> </td>
</tr>
<tr>
<td valign='top'>Website:</td>
<td><a href='http://$row->website'>$row->website</a></td>
<td>Town:</td>
<td>$row->town</td>
</tr>
<tr>
<td valign='top'>Area of Work </td>
<td>$row->area</td>
<td> </td>
<td> </td>
</tr>
</table>
</div>";
}}
mysql_free_result($result);
mysql_close($sqlconn);
This is the query.
I am posting $town to the query (there's always content), but $area is something empty, as they often just want to find the results based on a TOWN only.
Problem is, when they leave $area empty, it produces nothing and just shows everything in the databse.
What am I doing wrong?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
Sorry, I edited my last post before I saw your reply.
To answer your question ambiguously: maybe.
Are all of the solicitors in your table from the same town? The town clause in your query should limit the results even if the area clause matches all of the records because the query uses "AND" instead of "OR". The only way I can see that all of the records would be returned is if all of the solicitors are from the same town.
Edit: This post was recovered from search engine cache.
Last edited by McInfo on Thu Jun 17, 2010 12:39 pm, edited 1 time in total.
if ($area == NULL) {
$result = mysql_query ("SELECT * FROM solicitor");
If you don't want it to return everything, you need to add a WHERE clause to the query. Also add a PHP condition to make sure $town is not an empty string. And remember to sanitize user input.
Edit: This post was recovered from search engine cache.
Last edited by McInfo on Thu Jun 17, 2010 12:40 pm, edited 1 time in total.
The last script you posted says, "If area is an empty string, show all records from the table; otherwise, show records that match town and area." What do you want it to say?
Edit: This post was recovered from search engine cache.
Last edited by McInfo on Thu Jun 17, 2010 12:40 pm, edited 1 time in total.