1

How can I translate to PHP syntax something like that:

if(isset($_GET['q'])//but q=is not empty or <2 because in this case redirect to) { do this } else { do this one }

I hope it's not too massy.

Thanks.

updated question

Why this code does not redirect ?

    if(isset($_GET['q']))
{ 
    if(!empty($_GET['q']) || $_GET['q']>2)
    {

    $q = $_GET['q'];
    $q = mysql_real_escape_string($q);
    $sql = $db->prepare ('SELECT * FROM t WHERE a = :a');
    $sql->bindParam(':a',$q);
    $sql->execute();

    }

    else

    {

      header("Location:somepage.php");

    }

} else {

$sql = $db->prepare ('SELECT * FROM t ORDER BY b');
$sql->execute();

}
5
  • How does your URL querystring look like ? Commented Feb 11, 2014 at 10:03
  • @ShankarDamodaran href="./page.php?q=<? php echo $i; ?>" Commented Feb 11, 2014 at 10:05
  • There is a space between <? and php .. Remove that and give a shot. Commented Feb 11, 2014 at 10:06
  • @ShankarDamodaran no way Commented Feb 11, 2014 at 10:08
  • Your code redirects if your URL is like page.php?q= which means there is no value passed for q Commented Feb 11, 2014 at 10:15

3 Answers 3

2

Do like this

<?php
if(isset($_GET['q']))
{
    if(!empty($_GET['q']) || $_GET['q']<2)
    {
        header("location:somepage.php");
    }
    else
    {
        echo "Cannot be redirected";
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Your code redirects if your URL is like page.php?q= which means there is no value passed for q ...because that is how the code logic is written. If you want the logic to be act the other way change the !empty to empty on the first if statement.
2

What? Do you mean the code for the "text" you have?

if(isset($_GET['q']) && (!empty($_GET['q']) || $_GET['q'] < 2)) {
  // redirect here using header("location: foo.php") or some other function if you are using some framework.
}
else {
  /otherwise do something else.
}

Update to your updated question:

Because $_GET['q'] isn't empty? Do a var_dump of $_GET so we, and you, know what's in there. It's impossible to tell otherwise. But I think your code is doing exactly what you have told it to do. You just don't have the complete picture of what you want to accomplish.

Comments

1

I assume q is a number here.

if (isset($_GET['q']) {
    if (empty($_GET['q'] || $_GET['q'] < 2)) {
        // do redirect here
    } else {
        // do mysql here
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.