mysql搜索查询,用户给出了多个参数

时间:2017-03-01 08:30:35

标签: php mysql

我正在使用php开发一个搜索选项。在那里我需要使用用户给定的标准进行搜索用户可以在用户给出id,name,status这三个参数的任意组合时进行搜索。下面是我的代码

    if(isset($_POST['search']))
    {
        echo "<script> document.getElementById('tblsearch').style.display = 'block' </script> "; 

        $serviceNumber=$_POST['serviceNumber'];
        $name=$_POST['name'];
        $pendingfrom=$_POST['pendingfrom'];
        $status=$_POST['status'];
        $datefrom=$_POST['datefrom'];
        $dateto=$_POST['dateto'];
        $searchkey='serviceNumber';




$mysqli = new mysqli("localhost", "root", "", "user_management");

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

    $query = 'SELECT * FROM user WHERE ';
    $where = array();
    $values = array();
    $types = '';

    if (!empty($_POST['serviceNumber'])) {
        $where[] = 'serviceNumber = ?';
        $values[] = $_POST['serviceNumber'];
        $types .= 'i';
    }

    if (!empty($_POST['name'])) {
        $where[] = 'Username = ?';
        $values[] = $_POST['name'];
        $types .= 's';
    }

    if (!empty($_POST['status'])) {
        $where[] = 'status = ?';
        $values[] = $_POST['status'];
        $types .= 's';
    }



      $query .= implode(' AND ',$where);
      printf("rows inserted: %d\n", $query);



  printf("rows inserted: %d\n", $values);

/* prepare statement */
if ($stmt = $mysqli->prepare($query)) {

    /* Bind variable for placeholder */

    $stmt->bind_param($types,$values);

    /* execute statement */

    $stmt->execute();
    $res = $stmt->get_result();
    $row = $res->fetch_assoc();

    printf("rows inserted: %d\n", $stmt->num_rows);

    /* close statement */
    $stmt->close();
}

/* close connection */
    $mysqli->close();
  

下面的行我感到很困惑,因为$ values是一个数组。我需要将用户给定的参数传递给它才能执行。

$stmt->bind_param($types,$values);
  

因此,为了获得正确的结果,我该如何做到这一点。

2 个答案:

答案 0 :(得分:0)

您可以使用call_user_func_array()来调用带有参数数组的函数。需要将参数存储为引用,以便传递给bind_param方法。简而言之,你需要一个数组,其中第一个值是一个字符串,其余的是对你的参数的引用。

例如:

$bindParams = [$types];
foreach ($values as $key => $value) {
    $bindParams[$key] = &$value;
}

call_user_func_array(
  [$stmt, 'bind_param'], //array with the object and the method - callable
  $bindParams
);

答案 1 :(得分:-1)

尝试使用以下内容:

     if(isset($_POST['search']))
      {
      echo "<script> document.getElementById('tblsearch').style.display = 'block' </script> "; 

    $serviceNumber=$_POST['serviceNumber'];
    $name=$_POST['name'];
    $pendingfrom=$_POST['pendingfrom'];
    $status=$_POST['status'];
    $datefrom=$_POST['datefrom'];
    $dateto=$_POST['dateto'];
    $searchkey='serviceNumber';




$mysqli = new mysqli("localhost", "root", "", "user_management");

/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}

$query = 'SELECT * FROM user WHERE ';

if (!empty($_POST['serviceNumber'])) {
   $query .="serviceNumber='$searchkey'  ";

}

if (!empty($_POST['name'])) {
          $query .="and Username='$name'  ";

}

if (!empty($_POST['status'])) {
    $where[] = 'status = ?';
    $values[] = $_POST['status'];
          $query .=" and status='$status' ";
}




/* prepare statement */
if ($stmt = $mysqli->prepare($query)) {

/* Bind variable for placeholder */

$stmt->bind_param($types,$values);

/* execute statement */

$stmt->execute();
$res = $stmt->get_result();
$row = $res->fetch_assoc();

printf("rows inserted: %d\n", $stmt->num_rows);

/* close statement */
$stmt->close();
}

     /* close connection */
$mysqli->close(); 

希望这有帮助。