|
Posted by Jerry Stuckle on 12/17/78 11:53
tallalex85@gmail.com wrote:
> Hi,
>
> I'm quite new to MySQL and php so please go easy. Thanks!
>
> I'm trying to design a very basic php script which displays the
> contents of the table, then I want to enable the user to filter out
> certain results. So I went about writing a MySQL query something like
> .....WHERE gender="$gender" AND group="$group" and then wrote a
> form which sets the variables $gender and $group. Now is there a way of
> setting $gender and $group to something that would display the whole
> table?
>
> And is this the right way of going about this? Or is there a better
> way... Infact does anyone know of a good site that might guide me in
> creating such a script?
>
> Thanks
>
> Alex
>
Alternatively, check to see if $gender and $group are set. Build your
query dynamically and only use them if they are set, i.e. (Assumes
gender and query are strings):
$genset = false;
$query = 'SELECT ...';
if (isset($gender)) { // Or however you wish to test
$query .= " WHERE gender='$gender'";
$genset = true;
}
if (isset($group)) {
if ($genset) {
$query .= " AND ";
else
$query .= " WHERE ";
$query .= "group='$group'";
}
Or something similar.
And yes, you do need to ensure $gender and $group are validated to
prevent SQL injection attacks.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Navigation:
[Reply to this message]
|