我创建了一些 if/else 语句来从 url 中获取名称,例如 http://website.com/page.php?name=Love它看起来不错并且没有错误,但由于某种原因我没有从数据库中获取数据。基本上它从 url 获取“名称”并检查它是否是允许的类别之一,如果是,它会从数据库中选择具有 st_category = 用户选择的文章。 但由于某种原因,它再次不起作用。
这是我认为导致问题的一段代码。
<?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
?>
最佳答案
=
与 ==
不同。在您的 if 语句中,您进行的是赋值而不是比较。
if ($category = "Love")
应更改为 if ($category == "Love")
(或 if ($category === “爱”)
等等...
关于PHP if/else 语句似乎不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8574460/