Ilja
Ilja

Reputation: 46547

PHP if / else statements don't seem to work

I've created some if / else statements to get name from url like http://website.com/page.php?name=Love It seems to look good and trows no errors, but for some reason I am not getting data from the database. Basically it gets 'name' from url and checks of it is one of allowed categories, if yes it selects article from database that has st_category = to what user selected. But than again for some reason it doesn't work.

Here is a snippet of code that I think causes the problem.

       <?php
        $category = preg_replace('#[^a-z]#i', '', $_GET["name"]);

        if ($category = "Love") {
        $st_category = "Love";
        }
        else if ($category = "Work") {
        $st_category = "Work";
        }
        else if ($category = "Money") {
        $st_category = "Money";
        }
        else if ($category = "Kids") {
        $st_category = "Kids";
        }
        else if ($category = "Health") {
        $st_category = "Health";
        }
        else if ($category = "Friends") {
        $st_category = "Friends";
        }
        else if ($category = "Education") {
        $st_category = "Education";
        }
        else if ($category = "Other") {
        $st_category = "Other";
        }
        else {
        header("Location: http://www.inelmo.com/");
        exit;
        }

$sql = mysql_query("SELECT * FROM stories WHERE showing = 1 AND st_category = '$st_category' ORDER BY st_date DESC LIMIT 10") or die (mysql_error("There was an error in connection"));
        //And another stuff here to display article
?>

Upvotes: 0

Views: 241

Answers (5)

Marco Pace
Marco Pace

Reputation: 3870

You have used a single "=" in every if. The correct syntax is with "==" or "===", like:

<?php
    $category = preg_replace('#[^a-z]#i', '', $_GET["name"]);

    if ($category == "Love") {
        $st_category = "Love";
    }
    else if ($category == "Work") {
        $st_category = "Work";
    }
    ...
?>

Upvotes: 4

Spikey21
Spikey21

Reputation: 439

In your if-statements you used the = while you had to used the == sign. With the = you assign a value to a variable on the left, like $sum = 1 + 2; you wanted is $sum==3.

Upvotes: 0

DaveRandom
DaveRandom

Reputation: 88697

That could be tidied up to much less code, much more maintainable, using in_array().

$categories = array(
  'Love',
  'Work',
  'Money',
  'Kids',
  'Health',
  'Friends',
  'Education',
  'Other'
);

$category = preg_replace('#[^a-z]#i', '', $_GET["name"]);

if (!in_array($category, $categories)) {
  header("Location: http://www.inelmo.com/");
  exit;
}

$sql = mysql_query("SELECT * FROM stories WHERE showing = 1 AND st_category = '$category' ORDER BY st_date DESC LIMIT 10") or die (mysql_error("There was an error in connection"));

And this also fixes the problem that @matino rightly pointed out, which is that you were assigning and not comparing.

Upvotes: 7

mustafa
mustafa

Reputation: 745

Please use double equal sign like

if($foo=="foo1")

Upvotes: 0

matino
matino

Reputation: 17735

= is not the same as ==. In your if statements you are doing assignments not comparison.
if ($category = "Love") should be changed to if ($category == "Love") (or to if ($category === "Love") and so on...

Upvotes: 9

Related Questions