I've 2 input box, 'person.tag_name' and 'person.created_by', which I post 'person' from .js to .php.
The problem now is that it requires both input box to be fielded only the data will show up, however on my sql statement I only match against createdBy and tag_name wasn't use at all.
In summary:
- If only 1 of 2 input box is fielded I will not received data.
- I will only receive data if 2/2 input box is fielded.
- Tried removing
$tagName = $request->tag_name;
and it will return data
php code:
$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
$createdBy = $request->created_by;
$tagName = $request->tag_name;
if (!empty($createdBy) && !empty($tagName)){
$Where = "created_by LIKE '%$createdBy%' AND name LIKE '%$tagName%'";
}
else if (!empty($createdBy)){
$Where = "created_by LIKE '%$createdBy%'";
}
else if (!empty($createdBy)){
$Where = "name LIKE '%$tagName%'";
}
// Create connection
$con = new mysqli($servername, $username, $password, $db);
// Check connection
if ($con->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM test.test_tag WHERE deleted_by is null AND $Where;" ;
$qry = $con ->query($sql);
$data = array();
if ($qry->num_rows > 0) {
// output data of each row
while($row = $qry->fetch_object()) {
$data[] = $row;
}
} else {
$data[] = null;
}
$con->close();
Please assist.