我已經把一個腳本一起用於打印一切從「sales_list」表中的行,但只有那些具有「users_sales_guild_id」相匹配登錄的用戶。這工作正常。選擇查詢連接兩個表PHP
我所試圖做的是打印所有行,但檢索匹配的從「accessories_orders」表sales_id,並把「accessories_orders_total」,並隨查詢狀態,所以查詢如果登錄的用戶的「user_sales_guild_id」的值爲「1234」,則在瀏覽器中應該如下所示。
+--------+---------------+-------------------+----------+
| Model | Customer Name | Accessories Total | Status |
+--------+---------------+-------------------+----------+
| Nissan | Malcom Smith | | Add |
| Ford | Jane Smith | 200.00 | Pending |
+------------------------+-------------------+----------+
所以,如果有在「accessories_orders」表匹配的行,然後將打印「已裝船」和「accessories_orders_total」數據。如果沒有匹配的行,那麼它會顯示一個「Add」鏈接,它會導致add_accessories_sales.php。
我收到一條錯誤消息「Undefined index:sales_model」以及第一個查詢中的其他所有內容,任何人都可以指出我要出錯的地方嗎?
「sales_list」 表
+--------------------------------------------------------------------------------------------------------------------------+
| sales_list |
+------+--------------------------+--------------------------+------------------------+-------------+----------------------+
| sales_id | users_sales_guild_id | sales_customer_firstname | sales_customer_surname | sales_model | sales_entry_date |
+----------+----------------------+--------------------------+------------------------+-------------+----------------------+
| 1 | 1234 | Jane | Smith | Ford | 2013-12-02 12:00:00 |
| 2 | 5678 | John | Chan | Mazda | 2013-12-03 12:00:00 |
| 3 | 5678 | Kevin | Chan | Fiat | 2013-12-04 12:00:00 |
| 4 | 1234 | Malcom | Smith | Nissan | 2013-12-05 12:00:00 |
+----------+----------------------+--------------------------+------------------------+-------------+----------------------+
「accessories_orders」 表
+-------------------------------------------------------------------------------------------------------------------------+
| accessories_orders |
+-----------------------+----------------------+----------+--------------------------+-------------------------+----------+
| accessories_orders_id | users_sales_guild_id | sales_id | accessories_orders_total | accessories_orders_date | shipped |
+-----------------------+----------------------+----------+--------------------------+-------------------------+----------+
| 1 | 1234 | 1 | 200.00 | 2013-12-02 12:00:00 | Pending |
| 2 | 5678 | 2 | 350.00 | 2013-12-03 12:00:00 | Pending |
| 3 | 5678 | 3 | 100.00 | 2013-12-03 12:00:00 | Pending |
+-----------------------+----------------------+----------+--------------------------+-------------------------+----------+
編輯和更新代碼
<?php
require_once ('database.php'); // Connect to the database.
$query = " SELECT sl.sales_model, sl.sales_customer_firstname, sl.sales_customer_surname, ao.accessories_orders_total, ao.shipped,
COALESCE(ao.shipped)
FROM sales_list sl
LEFT JOIN accessories_orders ao ON(ao.sales_id = sl.sales_id)
WHERE sl.users_sales_guild_id ='".$_SESSION['users_sales_guild_id']."'
ORDER BY
".$order_by." LIMIT ".$start.", ".$display;
$result = @mysql_query ($query); // Run the query.
echo '<table width="610" cellspacing="1" cellpadding="5" style="font-size:11px;">
<tr>
<td align="center">Model </td>
<td align="center">Customer Name</td>
<td align="center">Accessories Total</td>
<td align="center">Status</td></tr>';
$bg = '#ffffff'; // Set the background color.
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$status = $row['shipped'];
$bg = ($bg=='#e1e3e6' ? '#cdcdcf' : '#e1e3e6'); // Switch the background color.
echo '<tr bgcolor="' . $bg . '">';
echo '<td align="center">' . $row['sl.sales_model'] . '</td>';
echo '<td align="center">' . $row['sl.sales_customer_firstname'] . ' ' . $row['sl.sales_customer_surname'] . '</td>';
echo '<td align="center">$' . $row['acc.accessories_orders_total'] . '</td>';
$str = '<td align="center">';
if($status == 'Pending') {
$str .=' Pending</td></tr>';
}
else {
$str .='<strong><a href="add_accessories_sales.php?sid=' . $row['sl.sales_id'] . '">Add</a></strong></td></tr>';
}
echo $str;
}
echo '</table>';
mysql_free_result ($result); // Free up the resources.
mysql_close(); //Close the database connection.
?>
'mysql'已棄用。使用'mysqli' – hammus
它似乎沒有工作 – AdamMc
你想要使用JOIN – serakfalcon