1 回答
TA贡献1780条经验 获得超4个赞
为了同时解决 sql 漏洞和缺少返回数据(数据未echoed返回到 ajax 函数),以下内容可能会有所帮助
<?php
require '../db_connection.php';
function getclientRecords( $con=false ){
$id = isset( $_POST['client_id'] ) ? $_POST['client_id'] : false;
if( $con && $id ){
$sql='select `item_date`
from `invoice_data`
where `client_id`=?';
$stmt=$con->prepare( $sql );
$stmt->bind_param( 's', $id );
$stmt->execute();
$result=$stmt->get_result();
$data=[];
while( $rs=$result->fetch_object() ){
$data[]=$rs->item_date;
}
$stmt->free_result();
$stmt->close();
return json_encode( $data );
}
return false;
}
function getclientRecordByDate( $con=false ){
$date = isset( $_POST['date'] ) ? $_POST['date'] : false;
$id = isset( $_POST['client_id'] ) ? $_POST['client_id'] : false;
if( $con && $id && $date ){
$sql = 'select `client_name`, `item_date`, `item_refe`, `item_parti`, `balance_amount`, `item_amnd`, `item_amnf`, `item_tax`, `item_amniw`, `item_amnif`
from `invoice_data`
where `item_date` = ? and client_id = ?';
$stmt=$con->prepare( $sql );
$stmt->bind_param( 'ss', $date, $id );
$stmt->execute();
$result=$stmt->get_result();
$data=[];
while( $rs=$result->fetch_object() ){
$data[]=array(
'client_name' => $rs->client_name,
'item_date' => $rs->item_date,
'item_refe' => $rs->item_refe,
'item_parti' => $rs->item_parti,
'balance_amount' => $rs->balance_amount,
'item_amnd' => $rs->item_amnd,
'item_amnf' => $rs->item_amnf,
'item_tax' => $rs->item_tax,
'item_amniw' => $rs->item_amniw,
'item_amnif' => $rs->item_amnif
);
}
$stmt->free_result();
$stmt->close();
return json_encode( $data );
}
return false;
}
$data=[];
$action = isset( $_GET['action'] ) ? $_GET['action'] : false;
switch( $action ){
case 'getclientRecords':
$data=getclientRecords($con);
break;
case 'getclientRecordByDate':
$data=getclientRecordByDate($con);
break;
default:
$data=['error'=>'no defined action'];
break;
}
header('Access-Control-Allow-Origin: *');
http_response_code( $action ? 200 : 400 );
exit( $data );
?>
- 1 回答
- 0 关注
- 271 浏览
添加回答
举报