如何正确搜索PHP

时间:2017-09-23 15:44:37

标签: php mysql

我的查询有什么问题?我想要发生的是,当我在订单ID中搜索数字时,输出将是我在搜索栏中输入的订单ID。正确发生的事情是当我输入一个数字时,所有“已接受”和“已发送”都包括在内。

这是我现在的代码。

<?php 
session_start();

$order_id = $_SESSION['order_id'];

if (!isset($_SESSION['user_id'])) {
    header("Location: index.php");
}
$_SESSION['navMenu'] = "order";
ini_set('display_errors', 'On');

require_once 'includes/database.php';
include_once 'system_menu.php'; 
include_once 'ordermodal.php';
include_once 'ordermodal2.php';

$sql2 = "SELECT * FROM cart_tbl";
date_default_timezone_set("Asia/Hong_Kong"); 

$sql = "SELECT * FROM order_tbl WHERE order_status = 'Accepted' or order_status = 'Dispatched' or order_status = 'Pending'";
/*** * SET UP COMBO BOX FOR SEARCH */
$comboBox = isset($_REQUEST['comboBoxVal']) ? trim($_REQUEST['comboBoxVal']) : '';
$search_by = isset($_REQUEST['search_by']) ? addslashes($_REQUEST['search_by']) : 0;
$orders= null;

if ($comboBox != '') { 
    switch ($search_by) { 
        case 1://Order ID
            $sql .= " AND order_id LIKE '%{$comboBox}%' ";
            break;
        case 2://Driver Number
            $sql .= " AND driver_number LIKE '%{$comboBox}%' ";
            break; 
    } 
}
$carts = mysqli_query(connection2(), $sql2);
$orders = mysqli_query(connection2(), $sql);
$search_filters = array(1 => 'Order ID', 2 => 'Driver Number');

1 个答案:

答案 0 :(得分:0)

查询构造有很多错误。

例如,在case 1语句末尾case 2break放在case语句中时,if ($comboBox != '') { switch ($search_by) { case 1://Order ID $sql .= " AND order_id LIKE '%{$comboBox}%' "; break; case 2://Driver Number $sql .= " AND driver_number LIKE '%{$comboBox}%' "; break; } $search_filters = array(1 => 'Order ID', 2 => 'Driver Number'); 不会同时执行 echo $sql; (define a 1) (define b 2) (list a b) (1 2) (list ’a ’b) (a b) (list ’a b) (a 2)

(* (+ 23 45) (+ x 9)) 
(define (fact n) (if (= n 1) 1 (* n (fact (- n 1)))))

另一方面,最后你试图绑定两个持有者。

{{1}}

但是,我想知道它是如何运作的?

最好的选择是,调试构造的SQL以确定您的查询是否正确构造。

当您要执行构造的SQL时。添加以下代码:

{{1}}