我在下载更改时收到错误

时间:2018-03-08 15:22:12

标签: php

我有一个产品清单,我有3个下拉菜单 1.)我选择国家 2.)我选择设备 3.)我选择类型

<div class="form-group">
 Country: <select style="width:200px;" class="form-control" onchange="search_offer();" id="country" name="country">
    <option value="all">All countries</option>
                         <?php

    foreach($countries as $key => $value) {

    ?>
    <option value="<?php echo $key ?>" title="<?php echo htmlspecialchars($value) ?>"><?php echo $key ?> | <?php echo htmlspecialchars($value) ?></option>
    <?php

    }

    ?>
                          </select>
                           <script>
                                $('#country').val('<?php if (isset($_GET['country'])){echo $_GET['country'];} else { echo "all";} ?>');
                            </script>
                        </div>
                       <div style="padding-left:10px;" class="form-group">
                          Device: <select onchange="search_offer();" id="device" name="device" class="form-control">
                          <option value="all">All devices</option>
                          <option value="mobile">Mobile</option>
                          <option value="ince">Desktop</option>
                          </select>
                           <script>
                                $('#device').val('<?php if (isset($_GET['device'])){echo $_GET['device'];} else { echo "all";} ?>');
                            </script>
                        </div>
                       <div style="padding-left:10px;" class="form-group">
                          Category: <select onchange="search_offer();" id="category" name="category" class="form-control">
                        <option value="all">All Categories</option>
                        <option value="Download">Download</option>
                        <option value="Mobile Install">Mobile Install</option>
                        <option value="Pin-Submit">Pin-Submit</option>
                        <option value="Free Survey">Free Survey</option>
                        <option value="Trial Offer">Trial Offer</option>
                        <option value="Email/Zip Submit">Email/Zip Submit</option>
                        <option value="Social Action">Social Action</option>
                        <option value="Dating Site">Dating Site</option>
                        <option value="Credit Card Submit">Credit Card Submit</option>
                        <option value="Credit Score">Credit Score</option>

                          </select>
                          <script>
                                $('#category').val('<?php if (isset($_GET['category'])){echo $_GET['category'];} else { echo "all";} ?>');
                            </script>
                        </div>

我用javascript调用更改:

function search_offer(){
            var country = $('#country').val();
            var device = $('#device').val();
            var category = $('#category').val();

            top.location='status_offers_smart_links.php?id=<?php echo $_GET['id']; ?>&country='+country+'&device='+device+'&category='+category;
        }   

status_offers_smart_links.php有这个:

    if ($_GET["country"] == "all" || $_GET["device"] == "all" || $_GET["category"] == "all"){

        $where = "";
        $execute = array();
    }


    if ($_GET["country"] != "all"){ 

            $country = $_GET["country"];

            $where = "WHERE countries=:country";
            $execute = array(":country" => $country);
    } 


    if ($_GET["device"] != "all"){  

            $device = $_GET["device"];

            if($device == "mobile" ){
            $where .= " AND device LIKE :device";
            $array = array(":device" => '%'.$device.'%');
            $execute = $execute+$array;

            } else {

            $where .= " AND device=:device";
            $array = array(":device" => $device);
            $execute = $execute+$array;
            }
    } 

    if ($_GET["category"] != "all"){    

            $category = $_GET["category"];


            $where .= " AND category=:category";
            $array2 = array(":category" => $category);
            $execute = $execute+$array+$array2;

    } 

$users = $db->prepare("SELECT id,category,title,description,photo,payout,epc,countries,device FROM offers ".$where);
$users->execute($execute);
$offers = $users->fetchAll(PDO::FETCH_OBJ);

脚本工作正常,但当我选择所有国家/地区时 - &gt;桌面 - &gt;从下拉列表中收到的所有类别我收到错误:

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'AND device=?' at line 1' in /var/www/vhosts/domain.com/status_offers_smart_links.php:59 Stack trace: #0 /var/www/vhosts/domain.com/status_offers_smart_links.php(59): PDO->prepare('SELECT id,categ...') #1 {main} thrown in /var/www/vhosts/domain.com/status_offers_smart_links.php on line 59

第59行:

$users = $db->prepare("SELECT id,category,title,description,photo,payout,epc,countries,device FROM offers ".$where);

当我选择所有国家时的相同问题 - &gt;所有设备 - &gt;和类别中的东西。

如果我选择国家,设备和类别,它工作正常。 或所有国家/地区和所有设备和所有类别

任何消化?我能提供的任何其他信息!

1 个答案:

答案 0 :(得分:0)

更简单的方法是将执行变量添加到一个数组中,并将where子句添加到另一个数组中。然后你可以使用implode用“AND”连接所有这些单独的子句,并获得一个格式正确的字符串,如下所示:

$wheres = array();
$execute = array();

if ($_GET["country"] != "all"){

    $country = $_GET["country"];

    $wheres[] = "countries=:country";
    $execute[":country"] = $country;
}

if ($_GET["device"] != "all"){

    $device = $_GET["device"];

    $wheres[] = "device LIKE :device";
    if($device == "mobile" ){
        $execute[":device"] = '%'.$device.'%';
    } else {
        $execute[":device"] = $device;
    }
}

if ($_GET["category"] != "all"){

    $category = $_GET["category"];
    $wheres[] = "category=:category";
    $execute[":category"] = $category;

}

$where = !empty($wheres) ? "WHERE ".implode(' AND ', $wheres) : ''; // Creates the string IF there are options to search for
$users = $db->prepare("SELECT id,category,title,description,photo,payout,epc,countries,device FROM offers ".$where);
$users->execute($execute);
$offers = $users->fetchAll(PDO::FETCH_OBJ);