简体   繁体   English

Server Side Ajax JQuery CRUD DataTable - PHP PDO, MySql

[英]Server Side Ajax JQuery CRUD DataTable - PHP PDO, MySql

I am trying to fetch data from child table using Id stored in the child table to populate DataTable and display records related to Parent table and child table.我正在尝试使用存储在子表中的 Id 从子表中获取数据,以填充 DataTable 并显示与父表和子表相关的记录。 Below is the code:下面是代码:

Problem is when I add WHERE statement to fetch records based on HolderID shows number of entries returned but not displayed.问题是当我添加 WHERE 语句以根据 HolderID 获取记录时显示返回但未显示的条目数。 And filtered also shows 0 Code:并且过滤后还显示 0 代码:

function get_beneficaries_records($rowId)
{
    include('db.php');
    $statement = $connection->prepare("SELECT * FROM beneficiaries WHERE HolderID = $rowId");
    $statement->execute();
    $result = $statement->fetchAll();
    return $statement->rowCount();
}

Fetching records on fectchrecords.php.

$query .= "SELECT * FROM pro_beneficiaries";
if(isset($_POST["search"]["value"]))
{
    $query .= ' WHERE BeneficiaryIDNo LIKE "%'.$_POST["search"]["value"].'%" ';

    $query .= 'OR LastName LIKE "%'.$_POST["search"]["value"].'%" ';

    $query .= 'OR Initials LIKE "%'.$_POST["search"]["value"].'%" ';
}
if(isset($_POST["search"]["value"]))
{
    $query .= 'OR BeneficiaryIDNo LIKE "%'.$_POST["search"]["value"].'%" ';
}

if(isset($_POST["order"]))
{
    $query .= 'WHERE FK_HolderID = $rowIds';
    $query .= 'ORDER BY '.$_POST['order']['0']['column'].' '.$_POST['order']['0']['dir'].' ';
} 
else
{
    $query .= 'ORDER BY BID DESC ';
}
if($_POST["length"] != -1)
{
    $query .= ' WHERE BeneficiaryID LIKE "%'.$_POST["search"]["value"].'%" ';
}

$statement = $connection->prepare($query);
$statement->execute();
$result = $statement->fetchAll();
$data = array();
$filtered_rows = $statement->rowCount();


foreach($result as $row)
{

    $sub_array = array();
    $sub_array[] = $row["Initials"];
    $sub_array[] = $row["LastName"];
    $sub_array[] = $row["BeneficiaryIDNo"];
    $sub_array[] = $row["Relationship"];
    $sub_array[] = $row["MemberType"];
    
    $sub_array[] = '<button type="button" name="update" id="'.$row["BID"].'" class="btn btn-outline-warning btn-xs updatebeneficiary"><i class="fa fa-pencil-alt"></i></button>';
    $sub_array[] = '<button type="button" name="delete" id="'.$row["BID"].'" class="btn btn-outline-danger btn-xs deletebeneficiary"><i class="fa fa-minus"></i></button>';
    $data[] = $sub_array;
}
$output = array(
    "draw"              =>  intval($_POST["draw"]),
    "recordsTotal"      =>  $filtered_rows,
    "recordsFiltered"   =>  get_beneficaries_records($rowId),
    "data"              =>  $data
);
echo json_encode($output);

I would print raw content of the query result, I would print the query itself so I see what the code produced, then I could run it in a separate client (I love HeidiSQL for this).我会打印查询结果的原始内容,我会打印查询本身,以便查看生成的代码,然后我可以在单独的客户端中运行它(为此我喜欢 HeidiSQL)。

And I think there might a condition where it will put duplicate WHERE而且我认为可能会出现重复的 WHERE

if(isset($_POST["order"]))
{
    $query .= 'WHERE FK_HolderID = $rowIds';
} 

if($_POST["length"] != -1)
{
    $query .= ' WHERE BeneficiaryID LIKE "%'.$_POST["search"]["value"].'%" ';
}

I would form it into one WHERE and use boolean AND OR to form the conditions I want我会将它形成一个 WHERE 并使用 boolean AND OR 来形成我想要的条件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM