简体   繁体   English

PHP if-then-else语句不起作用

[英]PHP if-then-else statement not working

My url is something such as: "inventory.php?sorting=1" and so forth. 我的网址是诸如“ inventory.php?sorting = 1”之类的内容。 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))

Why not just use the field name as the GET variable? 为什么不只使用字段名称作为GET变量?

$sortField = $_GET['sorting'];
// Ensure we don't get any SQL injection:
$validFields = array('year', 'make', 'miles' ... 'pricecash');


$sql = "select * from vehicles";

if(in_array($sortField, $validFields)){
    $sql .= ' ORDER BY ' . $sortField .' DESC';
}

mysql_query($sql);

and then access the page using inventory.php?sorting=year etc. 然后使用venture.php?sorting = year等访问页面。

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. 这使URL更具可读性,易懂性,并且意味着您只需将新字段添加到数组中即可支持新字段,而无需编写新的切换案例。

You need to replace $sorting with $_GET["sorting"] 您需要将$sorting替换$sorting $_GET["sorting"]

but, also: 但是也:

Would it not be a better idea to use the switch statement? 使用switch语句不是更好的主意吗?

switch($_GET["sorting"]{
    case 1:
    $result = mysql_query("select * from vehicles ORDER BY year DSC");
    break;
case 2:

etc. 等等

Short answer : Replace $sorting with $_GET["sorting"] , or add $sorting = $_GET['sorting']; 简短答案 :将$sorting替换$sorting $_GET["sorting"]或添加$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. 长答案 :很久以前, register_globals用于自动使URL参数显示为变量。 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). 这会导致很多安全问题(上面的链接包含一个示例),因此最终默认情况下将其关闭(PHP 4.2.0)。 In PHP 6, this option no longer exists. 在PHP 6中,此选项不再存在。 Thus, you need to explicitly access URL GET parameters through $_GET or $_REQUEST . 因此,您需要通过$_GET$_REQUEST显式访问URL GET参数。

As an alternative, you can explicitly import your URL parameters into local variables by using the import_request_variables command. 或者,您可以使用import_request_variables命令将URL参数显式导入到局部变量中。

And to make it nicer, you can do this: 为了使它更好,您可以执行以下操作:

$sortBy = '';
switch($_GET["sorting"]{
  case 1:
    $sortBy = 'year';
    break;
  case 2:
    $sortBy = 'make';
    break;
  //...
}  

if(!empty($sortBy)) {
  $result = mysql_query('select * from vehicles ORDER BY ' . $sortBy . ' DSC');
}
else {
  $result = mysql_query('select * from vehicles');
}

This way, you only have to change your query at one point if you have to change it someday. 这样,如果某天必须更改查询,则只需要在某一点更改它即可。

Is there some 有一些

$sorting = $_GET['sorting'];

somewhere in your code? 您代码中的某处? It won't get it's value automatically. 它不会自动获得其价值。

在代码开头添加此行。

$sorting = $_REQUEST['sorting'];

Why not use switch : 为什么不使用switch

switch ($sorting) {
    case 1:
        $result = mysql_query("select * from vehicles ORDER BY year DSC");
        break;
    case 2:
        $result = mysql_query("select * from vehicles ORDER BY make DSC");
        break;
    // ...
    default:
        $result = mysql_query("select * from vehicles");
        break;
}

Also, make sure $sorting is assigned: 另外,确保分配了$sorting

$sorting = $_GET['sorting']; // Place somewhere before the switch

You need to get the $sorting variable from the $_GET array. 您需要从$ _GET数组中获取$ sorting变量。 I would also rewrite it as a switch statement like this: 我也将其重写为这样的switch语句:

switch($_GET['sorting'])
{
  case 1:
    $result = mysql_query("select * from vehicles ORDER BY year DSC");
  brek;

  case 2:
    $result = mysql_query("select * from vehicles ORDER BY make DSC");
  break;

  ...

  default:
    $result = mysql_query("select * from vehicles");
  break;
}

you can use $_GET['sorting'] or $_REQUEST['sorting'] if it could come in by either get or post , but why not do this? 如果可以通过getpost $_REQUEST['sorting'] $_GET['sorting']$_REQUEST['sorting'] ,则可以使用它,但为什么不这样做呢?

$query = "SELECT * FROM `vehicles`";

$sort_values = array( 1 => 'year', 'make', 'miles', 'downpay', 'pricepay', 'pricecash' );
$sort_number = $_GET['sorting'];
if( $sort_number <= count($sort_values) ) {
    $query .= " ORDER BY `{$sort_values[ $sort_number ]}` DESC";
}

$result = mysql_query($query);

note that the 1 => portion of the array is because you 1-indexed your list of queries. 请注意,数组的1 =>部分是因为您对查询列表进行了1索引。
the reason for the <= portion of the if statement is for that reason too -- if you 0-indexed it, you would just use < . if语句的<=部分的原因也是出于这个原因-如果您对它进行0索引,则只需使用<

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. 使用数组意味着您不必复制/粘贴任何代码(重复编写$result = mysql_query(...);等),并且在需要显示的情况下向表中添加新列几乎毫不费力。更多信息。

One might even fetch the column names from the database directly and avoid ever touching this code again. 甚至可以直接从数据库中获取列名,并避免再次触摸此代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM