我有一个下拉列表,该列表使用数据库中的表中的数据导入。下拉值由值MR_ID组成,该值与位于不同表中的MR_Name连接。根据所选内容,我想显示一个表格,该表格显示与下拉列表中所选内容相关的必要信息。因此,例如,如果我在下拉列表中选择“1 - Company A”,我希望Supp_ID值(可以超过1个值)与要显示在表中的“1 - Company A”值相关联。我怎么能这样做?
该表只有2列,MR_ID(显示在下拉列表中)和Supp_ID。
之前我有这个工作,但我不得不改变我的查询而不是使用CTE而且它让我失望。
HTML下拉列表:
<!-- Form -->
<form name="myForm" action="">
<!-- Dropdown List -->
<select name="master_dropdown" id="mr_id">
<option selected value="select">Choose a MR_ID</option>
<?php foreach($users->fetchAll() as $user) { ?>
<option data-name="<?php echo $user['MR_ID'];?>">
<?php echo $user ['MR_ID'];?>
</option>
<?php } ?>
</select>
</form>
以下是我的主页上的新旧查询(index.php)......
// Old query on main page (index.php)
$sql = "SELECT DISTINCT CAST(MR_ID AS INT) AS MR_ID FROM Stage_Rebate_Index";
// New query on main page (index.php)
$sql = "WITH cte AS (
SELECT DISTINCT CONCAT(CAST(Stage_Rebate_Index.MR_ID AS INT),' - ', Stage_Rebate_Master.MR_Name) AS MR_ID
, Stage_Rebate_Index.MR_ID AS sort_column
FROM Stage_Rebate_Index
LEFT JOIN Stage_Rebate_Master
ON Stage_Rebate_Master.MR_ID=Stage_Rebate_Index.MR_ID
)
SELECT MR_ID
FROM
cte
ORDER BY
sort_column;";
以下是我在test-table.php页面上的旧查询和新查询
// Query that is used in ajax call (test-table.php)
$sql_one = "SELECT CAST(Supp_ID AS INT) AS Supp_ID, CAST(MR_ID AS INT) AS MR_ID FROM Stage_Rebate_Index WHERE MR_ID = '$mr_id'";
// Query that is used in ajax call (test-table.php)
$sql_one = "WITH cte AS (
SELECT DISTINCT CONCAT(CAST(Stage_Rebate_Index.MR_ID AS INT),' - ', Stage_Rebate_Master.MR_Name) AS MR_ID
, Stage_Rebate_Index.MR_ID AS sort_column, CAST(Stage_Rebate_Index.Supp_ID as INT) AS Supp_ID
FROM Stage_Rebate_Index
LEFT JOIN Stage_Rebate_Master
ON Stage_Rebate_Master.MR_ID=Stage_Rebate_Index.MR_ID
)
SELECT MR_ID, Supp_ID
FROM
cte
WHERE
MR_ID = '$mr_id'
ORDER BY
sort_column;";
这是我的ajax(index.js)以及一行带来$ _POST值的代码......
// Reads what the user selects from the drop down list and displays table when a selection is made
function updatetable(myForm) {
function show() { document.getElementById('index-table').style.display = 'block'; }
var selIndex = myForm.selectedIndex;
console.log();
var selName = $( "#mr_id option:selected" ).text();
// Ajax sends POST method to Stage_Rebate_Index table and pulls information based on drop down selection
$.ajax ({
url: "test-table.php",
method: "POST", //can be post or get, up to you
data: {
mr_id : selName
},
beforeSend: function () {
//Might want to delete table and put a loading screen, otherwise ignore this
},
success: function(data){
$("#table_div").html(data); // table_div is the div you're going to put the table into, and 'data' is the table itself.
}
});
}
$ _ POST方法引入test-table.php ...
$mr_id = $_POST['mr_id'];
test-table.php脚本......
<?php
$host="xxxxxxxxxxxx";
$dbName="xxxxx";
$dbUser="xxxxxxxx";
$dbPass="xxxxxxxxxxxxxx";
$mr_id = $_POST['mr_id'];
$dbh = new PDO( "sqlsrv:server=".$host."; Database=".$dbName, $dbUser, $dbPass);
$dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$sql_one = "WITH cte AS (
SELECT DISTINCT CONCAT(CAST(Stage_Rebate_Index.MR_ID AS INT),' - ', Stage_Rebate_Master.MR_Name) AS MR_ID
, Stage_Rebate_Index.MR_ID AS sort_column, CAST(Supp_ID as INT) AS Supp_ID
FROM Stage_Rebate_Index
LEFT JOIN Stage_Rebate_Master
ON Stage_Rebate_Master.MR_ID=Stage_Rebate_Index.MR_ID
)
SELECT MR_ID, Supp_ID
FROM
cte
WHERE
MR_ID = '$mr_id'
ORDER BY
sort_column;";
//$users = $dbh->query($sql);
$users_one = $dbh->query($sql_one);
?>
<html>
<body>
<!-- Table -->
<p>
<div id="table_div">
<table border="1" id="index_table" class="ui-widget ui-widget-content">
<thead>
<tr class="ui-widget-header">
<td>MR ID</td>
<td>Supplier ID</td>
</tr>
</thead>
<?php foreach($users_one->fetchAll() as $supp) { ?>
<tr>
<td class="mr_id"><?php echo $supp['MR_ID'];?></td>
<td class="supp_id"><?php echo $supp['Supp_ID'];?></td>
</tr>
<?php } ?>
</table>
</div>
</body
</html
答案 0 :(得分:0)
有几种方法可以对此进行调试,并提供有关正在发生的事情的更多信息(通过查看从先前查询和当前查询获得的结果集的差异,只是孤立的查询)。如果您更新的查询只返回一组空结果或实际错误等,那将是一件好事。
然而,在顶部,我认为您的SQL中可能存在一个问题,即您尝试将int与字符串连接起来,这里:
CONCAT(CAST(Stage_Rebate_Index.MR_ID AS INT),' - ', Stage_Rebate_Master.MR_Name)
我假设MR_Name是一个varchar,所以我确定你需要做的一件事就是将MR_ID作为varchar而不是int转换。
查询可能还有其他问题,但最好还是了解实际结果是什么。