PHP if-then-else 语句不起作用
我的网址是这样的:“inventory.php?sorting=1”等等。页面加载正常,但无法正确显示信息。
mysql_connect("localhost","user","pass");
mysql_select_db("database");
if ($sorting == 1){
$result = mysql_query("select * from vehicles ORDER BY year DSC");
}
elseif ($sorting == 2){
$result = mysql_query("select * from vehicles ORDER BY make DSC");
}
elseif ($sorting == 3){
$result = mysql_query("select * from vehicles ORDER BY miles DSC");
}
elseif ($sorting == 4){
$result = mysql_query("select * from vehicles ORDER BY downpay DSC");
}
elseif ($sorting == 5){
$result = mysql_query("select * from vehicles ORDER BY pricepay DSC");
}
elseif ($sorting == 6){
$result = mysql_query("select * from vehicles ORDER BY pricecash DSC");
}
else {
$result = mysql_query("select * from vehicles");
}
while($r=mysql_fetch_array($result))
My url is something such as: "inventory.php?sorting=1" and so forth. Page loads fine but does not display the information properly.
mysql_connect("localhost","user","pass");
mysql_select_db("database");
if ($sorting == 1){
$result = mysql_query("select * from vehicles ORDER BY year DSC");
}
elseif ($sorting == 2){
$result = mysql_query("select * from vehicles ORDER BY make DSC");
}
elseif ($sorting == 3){
$result = mysql_query("select * from vehicles ORDER BY miles DSC");
}
elseif ($sorting == 4){
$result = mysql_query("select * from vehicles ORDER BY downpay DSC");
}
elseif ($sorting == 5){
$result = mysql_query("select * from vehicles ORDER BY pricepay DSC");
}
elseif ($sorting == 6){
$result = mysql_query("select * from vehicles ORDER BY pricecash DSC");
}
else {
$result = mysql_query("select * from vehicles");
}
while($r=mysql_fetch_array($result))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
为什么不直接使用字段名称作为 GET 变量呢?
然后使用 inventory.php?sorting=year 等访问页面。
这使得 URL 更具可读性、可预测性,并且意味着您只需将新字段添加到数组中即可支持新字段,而无需编写新的 switch case。
Why not just use the field name as the GET variable?
and then access the page using inventory.php?sorting=year etc.
This makes the URL more readable, predicatable and means you can support new fields by just adding them to the array without needing to write new switch cases.
您需要将
$sorting
替换为$_GET["sorting"]
但同时:
使用
switch
不是一个更好的主意吗陈述?ETC。
You need to replace
$sorting
with$_GET["sorting"]
but, also:
Would it not be a better idea to use the
switch
statement?etc.
简短回答:将
$sorting
替换为$_GET["sorting"]
,或添加$sorting = $_GET['sorting' ];
到代码顶部。长答案:很久以前,register_globals用于自动使 URL 参数显示为变量。这会导致很多安全问题(上面的链接包含一个示例),因此最终被默认关闭(PHP 4.2.0)。在 PHP 6 中,此选项不再存在。因此,您需要通过
$_GET
或$_REQUEST
显式访问 URL GET 参数。作为替代方案,您可以使用 import_request_variables 命令。
Short answer: Replace
$sorting
with$_GET["sorting"]
, or add$sorting = $_GET['sorting'];
to the top of your code.Long answer: A long time ago, register_globals was used to automatically make URL parameters appear as variables. This lead to a lot of security problems (the above link contains an example), so it was eventually turned off by default (PHP 4.2.0). In PHP 6, this option no longer exists. Thus, you need to explicitly access URL GET parameters through
$_GET
or$_REQUEST
.As an alternative, you can explicitly import your URL parameters into local variables by using the import_request_variables command.
为了让它变得更好,你可以这样做:
这样,如果有一天你必须改变你的查询,你只需要在某一时刻改变它。
And to make it nicer, you can do this:
This way, you only have to change your query at one point if you have to change it someday.
有一些地方吗
你的代码中 ?它不会自动获取它的值。
Is there some
somewhere in your code? It won't get it's value automatically.
在代码开头添加此行。
Add this line at the start of your code.
您需要从 $_GET 数组中获取 $sorting 变量。我还将其重写为 switch 语句,如下所示:
You need to get the $sorting variable from the $_GET array. I would also rewrite it as a switch statement like this:
为什么不使用
switch
:另外,请确保分配了
$sorting
:Why not use
switch
:Also, make sure
$sorting
is assigned:如果可以通过
get
或传入,则可以使用
,但为什么不这样做呢?$_GET['sorting']
或$_REQUEST['sorting']
>post请注意,数组的
1 =>
部分是因为您对查询列表进行了 1 索引。if 语句的
<=
部分也是出于这个原因——如果您对其进行 0 索引,则只需使用<
即可。看起来可能还不是这样,但您很快就会发现尝试找到编写更少代码的方法是值得的。使用数组意味着您不必复制/粘贴任何代码(重复编写
$result = mysql_query(...);
等),并且几乎可以毫不费力地将新列添加到表中,如果您需要显示更多信息。人们甚至可以直接从数据库中获取列名,并避免再次接触此代码。
you can use
$_GET['sorting']
or$_REQUEST['sorting']
if it could come in by eitherget
orpost
, but why not do this?note that the
1 =>
portion of the array is because you 1-indexed your list of queries.the reason for the
<=
portion of the if statement is for that reason too -- if you 0-indexed it, you would just use<
.It may not seem like it yet, but you'll quickly find out that it's worth it to try and find ways to write less code. Using the array means you don't have to copy / paste any code (repeatedly writing
$result = mysql_query(...);
, etc) and it is virtually effortless to add new columns to your table, should you ever need to display more information.One might even fetch the column names from the database directly and avoid ever touching this code again.