2017-03-01 67 views
1

我正在開發一個使用php的搜索選項。在那裏,我需要使用用戶給定條件的用戶可以搜索做搜索時,用戶給ID,名稱,狀態這三種parameters.below的組合是我的代碼用戶給出多個參數的mysql搜索查詢

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(); 

below line i was going confused,because $values is an array.i need to pass user given paremeters to this in order to execute.

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

so in order to get correct results how do i need to do this.

+2

您可以'foreach' $ values'數組併爲循環的每次迭代綁定適當的參數。 http://php.net/manual/en/pdostatement.bindparam.php –

回答

-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(); 

希望這有助於。

+0

謝謝你,我會看到 – shashini

+0

哇它的工作很好回答 – shashini

+0

這將打開你SQL注入。是一種可怕的做事方式... – motanelu

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 
); 
+0

警告:參數2到mysqli_stmt :: bind_param()應該是引用,值在C:\ xampp \ htdocs \ User_managemet \ search中給出.php on line 331 致命錯誤:未捕獲錯誤:在C:\ xampp \ htdocs \ User_managemet \ search.php中調用成員函數fetch_assoc()布爾值:335 Stack trace:#0 {main}拋出C: \ xampp \ htdocs \ User_managemet \ search.php on line 335 – shashini

+0

我得到了上面的反饋 – shashini