Jump to content

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

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.