0

我有一个脚本可以查询我的数据库,并且需要能够使用四个选项中的一个或全部进行查询。选项是开始 sku、结束 sku(这是一个选项,因为必须有一个搜索范围)、创建者和创建日期。我知道我可以使用许多 if..elseif 语句来做到这一点,但我必须认为有更好更简单的方法。到目前为止,这是我的代码:

$start =mysql_real_escape_string ($_POST['skuStart']);
$end = mysql_real_escape_string($_POST['skuEnd']);
$source = mysql_real_escape_string($_POST['source']);
$processDate = mysql_real_escape_string($_POST['processDate']);

if(!empty($start) && !empty($end) && empty($source) && empty($processDate)){
$result = $conn->query("Select * from inventory where sku >= $start and sku <= $end");

 } elseif (empty($start) && empty($end) && !empty($source) && empty($processDate)){
$result = $conn->query("Select * from inventory where created_by = '$source'");

 } elseif (empty($start) && empty($end) && empty($source) && !empty($processDate)) {
$result = $conn->query("Select * from inventory where date_process = '$processDate'");

} elseif(!empty($start) && !empty($end) && !empty($source) && empty($processDate)){
$result = $conn->query("Select * from inventory where sku >= $start and sku <= $end and created_by = '$source'");

} else {
$result = $conn->query("Select * from inventory where sku >= '$start' and sku <= '$end' and created_by = '$source' and date_process = '$processDate'");
}

while($row = $result->fetch_assoc())
    {

        $skuArray[$x] = $row['sku'];
        $isbnArray[$x] = $row['isbn13'];
        $qtyArray[$x] = $row['quantity'];
        $defectArray[$x] = $row['defect_id'];
        $sourceArray[$x] = $row['source_id'];
        $featureArray[$x] = $row['feature_id'];
        $locationArray[$x] = $row['location_id'];
        $processDateArray[$x] = $row['date_process'];
        $bookTypeArray[$x] = $row['book_type_id'];
        $createdByArray[$x] = $row['created_by'];
        $modifiedByArray[$x] = $row['modified_by'];

        $x++;

这种非常粗略的形式正在起作用,但是有没有办法简化 if elseif ?

注意:我知道我应该使用 PDO 来防止 sql 注入,但我还没有完全了解这一点,所以我正在使用它。

4

4 回答 4

2
<?php
$db = new PDO('mysql:host=localhost;dbname=my_db', 'user', 'passwd');

$sql = "SELECT * FROM INVENTORY ";

$where  = array ();
$params = array ();
if (! empty($start) && ! empty($end) {
    $where[] = " sku >= :start AND sku <= :end ";
    $params['start'] = $start;
    $params['end']   = $end;
}
if (! empty($source)) {
    $where[] = " created_by = :source ";
    $params['source'] = $source;
}
if (! empty($processDate)) {
    $where[] = " date_process = :date_process ";
    $params['date_process'] = $processDate;
}

if (! empty($where)) {
    $sql .= ' WHERE ' . join (' AND ', $where);
}

$statement = $db->prepare($sql);
$statement->execute($params);

while ($row = $statement->fetch(PDO::FETCH_ASSOC)) {
   // ...
}
于 2012-07-31T12:39:03.213 回答
1

抛开您已经知道的问题 - 切换到 PDO 并使用绑定参数 - 您可以使用以下内容使您的代码更清晰一些:

<?php

$start          = mysql_real_escape_string ($_POST['skuStart']);
$end            = mysql_real_escape_string($_POST['skuEnd']);
$source         = mysql_real_escape_string($_POST['source']);
$processDate    = mysql_real_escape_string($_POST['processDate']);
$where          = array();

if(!empty($start))
{
    $where[] = 'sku >= ' . $start;
}

if(!empty($end))
{
    $where[] = 'sku <= ' . $end;
}

if(!empty($source))
{
    $where[] = 'created_by = \''.$source .'\'';
}

if(!empty($processDate))
{
    $where[] = 'date_process = \''.$processDate .'\'';
}

$query = 'SELECT * FROM inventory';

if(count($where))
{
    $query .= ' WHERE ' . implode(' AND ', $where);
}

$result = $conn->query($query);

while($row = $result->fetch_assoc())
    {

        $skuArray[$x] = $row['sku'];
        $isbnArray[$x] = $row['isbn13'];
        $qtyArray[$x] = $row['quantity'];
        $defectArray[$x] = $row['defect_id'];
        $sourceArray[$x] = $row['source_id'];
        $featureArray[$x] = $row['feature_id'];
        $locationArray[$x] = $row['location_id'];
        $processDateArray[$x] = $row['date_process'];
        $bookTypeArray[$x] = $row['book_type_id'];
        $createdByArray[$x] = $row['created_by'];
        $modifiedByArray[$x] = $row['modified_by'];

        $x++;
于 2012-07-31T12:36:34.423 回答
1
$q = array();
if (!empty($start))
  $q[] = "sku >= '$start'";
if (!empty($end))
  $q[] = "sku <= '$end'";
#...
$q = join(') AND (', $q);
$q = "SELECT * FROM inventory WHERE ($q)";

或者你可以更进一步:

$params = array(
   'skuStart' => "sku >= '##'",
   'skuEnd' => "'sku <= '##'",
   #...
);

$q = array();
foreach($params as $k=>$sql) {
   $v = mysql_real_escape_string($_POST[$k]);
   if (!empty($v)) {
     $sql = tr_replace('##', $v, $sql);
     $q[] = $sql;
   }
}
$q = join(') AND (', $q);

然后您可以添加用于验证的类型,以及其他可以增强元数据并构建您的小型框架的东西......

每当您觉得自己在重复自己时,您应该检查现有的解决方案(库/框架)或构建您的解决方案... DRY

于 2012-07-31T12:33:12.937 回答
0

您可以使用查询生成器。如果您不使用任何框架,例如https://github.com/jstayton/QueryBuilder (最初创建于 google)。

于 2012-07-31T12:40:08.190 回答