我有三张桌子:
表1称为“卡”。重要的字段是:
"state_id" - This table has 3 values (1,2,3)
"associated_driver" - called "referred_as" on driver table
"referred_as" - called "associated_card" on driver table
表2称为“驱动程序”。重要的字段是:
"ID" - The auto incremented value of the table
"associated_card" - Has a value , normally some number e.g 123555
"referred_as" - The name of the driver () called "associated_driver" on card table
忘记添加此表:
表3称为“card_driver”。重要的字段是:
"driver_id" - The id from the driver table that links to the card
"card_id" - The id from the card table that links to the driver
我想要发生的事情:
当用户从驱动程序表中输入其id时,它将比较两个表所具有的字段,即“关联卡”字段。从那里(bellow)的原始查询将接管,虽然这也需要修改。
我真的不知道如何进行SQL查询。
我需要改变这一部分:
$sql ="SELECT * FROM card WHERE id = '$id'" ;
谢谢
原始代码:
$id = $_POST['id'];
$sql ="SELECT * FROM card WHERE id = '$id'" ;
mysql_select_db('damp');
$result = mysql_query( $sql, $conn );
$row = mysql_fetch_assoc($result);
switch($row['state_id'])
{
case "1":
echo "<strong><font color=\"green\">Authorisation Granted!</font></strong>";
break;
case "2":
echo "<strong><font color=\"red\">Your card has expired and authorisation is denied</font></strong>";
break;
case "3":
echo "<strong><font color=\"red\">Your card has been cancelled and authorisation is denied</font></strong>";
break;
default:
echo "<strong><font color=\"red\">The Card ID does not exist</font></strong>";
}
//echo $_GET['here']; // Prints the name of the page content
//echo $class_obj_id; // Prints the id number of the selected row
?>
<?php } ?>
<div id="auth">
<form method="post" action="<?php $_PHP_SELF ?>">
<table id="auth" width="150" >
<tr>
<td colspan="2"><h2>Check Card Authorisation</h2></td>
</tr>
<tr>
<td width="50" align="center">ID</td>
<td><input name="id" type="text" id="id" value="" /></td>
</tr>
<tr>
<td width="100"> </td>
<td>
<input name="check" type="submit" id="check" value="Check">
</td>
</tr>
</table>
</form>
</div>
答案 0 :(得分:0)
$sql ="SELECT * FROM card c JOIN driver d ON c.referred_as=d.referred_as WHERE d.ID='$id'";
答案 1 :(得分:-1)
$sql = "SELECT * FROM driver INNER JOIN card ON driver.associated_card = card.associated_card WHERE id = '$id'";
加入相关卡片ID匹配的驱动程序和卡片表,然后您可以从此处拨打$row['state_id']