我在html中有一个表,用户可以单击一行并将该行的数据属性传递给加载在屏幕上的PHP文件。我希望这个PHP文件包含一些与传递的变量相关的数据。
例如,下面是定义变量的简单jQuery:$(document).on('click', 'tr', function() {
var id = $(this).attr("data-recordId");
$('#section2').load('data_entry_form.php?id='+id);
});
这里是PHP打算使用id变量并获取相关元素(根据SQL查询),其中id = fee_source_id。 PHP怎么了?这行不通。我想简单地回显/显示SQL查询的结果,其中条件fee_source_id = :id
为真。
<html>
<form>
<?php
require_once 'config.php';
$dbh = new PDO($dsn, $dbuser, $dbpass);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$id = $_GET['id'];
$sth = $dbh->prepare('
SELECT a_aif.aif_id,
a_aif.fee_source_id,
a_aif.company_name_per_sedar,
a_aif.document_filing_date
FROM a_aif
WHERE a_aif.fee_source_id = :id');
$sth->bindParam(':id', $id, PDO::PARAM_STR);
$sth->execute();
?>
</form>
</html>
您需要获取结果集的行。然后回显您需要的字段。
如果使用$可能会有所帮助。getJSON而不是$('#section2')。如果返回数组
,则加载$sth = $dbh->prepare('
SELECT a_aif.aif_id,
a_aif.fee_source_id,
a_aif.company_name_per_sedar,
a_aif.document_filing_date
FROM a_aif
WHERE a_aif.fee_source_id = :id');
$sth->bindParam(':id', $id, PDO::PARAM_STR);
$sth->setFetchMode(PDO::FETCH_ASSOC);
$sth->execute();
$row = $sth-> fetch(); // returns 1 row
//$data[] = $row; Returns all of array
//$id = $row['id'];// Returns indivadual fiels in array
echo $data;