I have a grid view on my page. The grid gets filled from my database. I have build a search in my script, so when I type the number
, name
or price
I get the correct data.
My SQL query to get information is the following:
$sql = "SELECT id, name, price FROM employees";
Now I want to extend my script and the relevant information for a specific user. But when I add a WHERE statement to my SQL query, the search function stops working
This is the SQL query I need:
$sql = "SELECT id, name, price FROM employees WHERE user_id='1'";
I dont know what is wrong with this. This query shows me the right data, but the search function stops working. I also tried this SQL query in phpMyAdmin and nothing is wrong with it.
Can someone help me with this problem. I really dont know what is wrong:
Here is the script I am using to get information from my database. The SQL query is on line 24: -- response.php --
<?php
include_once("connection.php");
$params = $columns = $totalRecords = $data = array();
$params = $_REQUEST;
$columns = array(
0 =>'id',
1 =>'name',
2 => 'price',
3 => 'user_id'
);
$where = $sqlTot = $sqlRec = "";
if( !empty($params['search']['value']) ) {
$where .=" WHERE ";
$where .=" ( id LIKE '".$params['search']['value']."%' ";
$where .=" OR name LIKE '".$params['search']['value']."%' ";
$where .=" OR price LIKE '".$params['search']['value']."%')";
}
$sql = "SELECT id, name, price FROM employees WHERE user_id='1'";
$sqlTot .= $sql;
$sqlRec .= $sql;
if(isset($where) && $where != '') {
$sqlTot .= $where;
$sqlRec .= $where;
}
$sqlRec .= " ORDER BY ". $columns[$params['order'][0]['column']]." ".$params['order'][0]['dir']." LIMIT ".$params['start']." ,".$params['length']." ";
$queryTot = mysqli_query($conn, $sqlTot) or die("database error:". mysqli_error($conn));
$totalRecords = mysqli_num_rows($queryTot);
$queryRecords = mysqli_query($conn, $sqlRec) or die("error to fetch employees data");
while( $row = mysqli_fetch_row($queryRecords) ) {
$data[] = $row;
}
$json_data = array(
"draw" => intval( $params['draw'] ),
"recordsTotal" => intval( $totalRecords ),
"recordsFiltered" => intval($totalRecords),
"data" => $data
);
echo json_encode($json_data);
?>
Edit 1:
Now I have:
if( !empty($params['search']['value']) ) {
$where .=" WHERE ";
$where .=" ( id LIKE '".$params['search']['value']."%' ";
$where .=" OR name LIKE '".$params['search']['value']."%' ";
$where .=" OR price LIKE '".$params['search']['value']."%') AND user_id='1'
$sql = "SELECT id,name,price FROM customers";
When I run this, I get a grid-view without user_id='1'
and a search function with user_id='1'
. I need user_id='1'
in both of them.
if $params is not empty you have two where in sql this is wrong
if( !empty($params['search']['value']) ) {
$where .=" WHERE ";
$where .=" ( id LIKE '".$params['search']['value']."%' ";
$where .=" OR name LIKE '".$params['search']['value']."%' ";
$where .=" OR price LIKE '".$params['search']['value']."%')";
}
$sql = "SELECT id, name, price FROM employees WHERE user_id='1'";
......
// here you assign the firts where by $sql
$sqlTot .= $sql;
// here you assign the second where by eval $param
if(isset($where) && $where != '') {
$sqlTot .= $where;
$sqlRec .= $where;
}
I suggestio syou should use this setting
if( !empty($params['search']['value']) ) {
$where .=" WHERE ";
$where .=" ( id LIKE '".$params['search']['value']."%' ";
$where .=" OR name LIKE '".$params['search']['value']."%' ";
$where .=" OR price LIKE '".$params['search']['value']."%') ";
}
if (!empty($where) ) {
$where .= "AND user_id='1' ";
} else {
$where .= "WHERE user_id='1' ";
}
$sql = "SELECT id,name,price FROM customers";