Jump to content

2 WHERE's - Is this possible?


erme

Recommended Posts

Thanks for that.

 

Do you know what i've done wrong with this? (new to SQL)

 

SELECT * FROM $Table (isset($_GET['id'][0]))?"WHERE ID='".mysql_real_escape_string($_GET['id'])."'":"" AND (isset($_GET['county'][0]))?"County='".mysql_real_escape_string($_GET['county'])."'":""

yes - that's what your whole parenthesized portion of the query is: short-hand notation for an if-else conditional block. if you don't understand what if/else statements are, you need to read the PHP manual section on Control Structures. if you don't understand those, you can't possibly hope to string together a proper query based on URL variables.

OK not sure how that would work but this works

 

(isset($_GET['id'][0]))?"WHERE ID='".mysql_real_escape_string($_GET['id'])."'":""

 

its just when I add this to it that it doesn't work:

 

AND (isset($_GET['county'][0]))?"County='".mysql_real_escape_string($_GET['county'])."'":"";

 

All I want to know is what I'm doing wrong. Thanks

i'm aware of what you're trying to do; it just doesn't seem you're actually interested in learning how, you appear to just want a chunk you can copy/paste. and frankly, that doesn't do anyone any good, because if it suddenly needs to be changed, you'll just come back instead of understanding how to change it yourself. teach a man to fish and all that.

 

anyway, in the interest of solving this thread, you need (as i mentioned) an if-else statement:

 

if (isset($_GET['id']))
{
  $where_clause = "WHERE ID='{$_GET['id']}'";
}

if (isset($_GET['county']))
{
  if (empty($where_clause))
    $where_clause = "WHERE County='{$_GET['county']}'";
  else
    $where_clause .= " AND County='{$_GET['id']}'";
}

echo $where_clause;

 

now here's where your homework is: you need to find out how to get $where_clause in the correct spot to make the query work correctly. it's worth mentioning that this isn't validating the input at all, leaving your script extremely vulnerable to attacks.

Thanks for that.

 

I'm a graphics designer. I've been given this project as a one off.

 

Managed to get that code working, however it only works for one or the other e.g.

 

/code.php?id=dev002

/code.php?county=devon

 

How can I combine it so this works

 

/code.php?id=dev002&county=devon

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.