I need to display a table that corresponds to the value chosen after I select a value in a dropdown list. If I run this query in my SQLPro Studio, and obviously without the $mr_id
variable, it says that MR_ID
is ambiguous. Is there a way to fix this? This seems like a small problem to me but I can't seem to figure it out. I just need to make sure there is a WHERE
clause somewhere so that it will only display the values that correlate to the value selected.
The table is only 2 columns, MR_ID
(which is what is displayed in the dropdown list and also concatenated with another column not in the table) and Supp_ID
.
<?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 = "
SELECT 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
WHERE Stage_Rebate_Index.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>
EDIT: Updated query with harcoded value...
$sql_one = "
SELECT
CONCAT(CAST(t1.MR_ID AS INT),' - ', COALESCE(t2.MR_Name, '')) AS MR_ID,
t1.MR_ID AS sort_column,
CAST(Supp_ID as INT) AS Supp_ID
FROM Stage_Rebate_Index t1
LEFT JOIN Stage_Rebate_Master t2
ON t2.MR_ID = t1.MR_ID
WHERE
CONCAT(CAST(t1.MR_ID AS INT),' - ', t2.MR_Name) = LTRIM(RTRIM('1 - Company A'))
ORDER BY sort_column";
In your where clause
Specify the table you want it from as such:
You should probably also specify the tables in your select clause too.