我有兩個表在我的分貝。結構如下:比較兩個表的數據,並選中相同的數據複選框
Comparitive_st_sup
================
id | item_name
================
486 | Item1
487 | Item2
488 | Item3
489 | Item4
=================
quotation_items
=====================
item_id | item_name
=====================
487 | Item2
489 | Item4
=====================
我想要實現的是首先我需要顯示的是「comparitive_st_sup」的表1中的所有記錄。其次,所顯示記錄的下方有一個複選框。然後我需要比較表2中的數據'quotation_items'。這是如果id = item_id,那麼應該檢查複選框。下面是代碼亞姆試圖檢查記錄,但並不是所有的記錄都被檢查。我犯了什麼錯誤嗎?
<?php
include('config.php');
$sql = "SELECT item_id FROM quotation_items WHERE tender_id=150002";
$query_resource = mysql_query($sql);
$quotation = mysql_fetch_assoc($query_resource);
$quotation_rec = explode(',', $quotation['item_id']);
$sql = "SELECT id from comparitive_st_sup where tender_id=150002";
$query_resource = mysql_query($sql);
$comparitive = Array();
while($name = mysql_fetch_assoc($query_resource))
{
?>
<span><?php echo $name['id']; ?></span>
<input type="checkbox" name="comparitive[]" value="<?php echo $name['id']; ?>" <?php if(in_array($name['id'], $quotation_rec)): ?> checked='checked' <?php endif; ?>/><br />
<?php } ?>
@斯蒂芬,下面的腳本我試圖嘗試代碼:
<?php
include('config.php');
$sql = "SELECT c.id FROM comparitive_st_sup c LEFT JOIN quotation_items q ON c.id = q.item_id WHERE c.tender_id=150002";
//$sql = "SELECT id from comparitive_st_sup where tender_id=150002";
$query_resource = mysql_query($sql);
$comparitive = Array();
while($name = mysql_fetch_assoc($query_resource))
{
?>
<span><?php echo $name['id']; ?></span>
<input type="checkbox" name="comparitive[]" value="<?php echo $name['id']; ?>" <?php if(null !== $name['item_id']): ?> checked='checked' <?php endif; ?>/><br /> <?php } ?>
'mysql_ *'已棄用。請閱讀http://php.net/manual/en/faq.databases.php#faq.databases.mysql.deprecated – Tigger
是的,我知道..只是編輯一些已經由某人開發的代碼...... Iam試圖糾正一些錯誤.. –