2014-01-20 243 views
0

我已經把一個腳本一起用於打印一切從「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. 
?> 
+0

'mysql'已棄用。使用'mysqli' – hammus

+0

它似乎沒有工作 – AdamMc

+0

你想要使用JOIN – serakfalcon

回答

1

您的查詢需要更多這樣的:

SELECT sl.sales_model, sl.sales_customer_firstname, sl.sales_customer_surname, ao.accessories_orders_total, COALESCE(ao.shipped, 'Add') status 
FROM sales_list sl 
LEFT JOIN accessories_orders ao ON(ao.sales_id = sl.sales_id) 
WHERE sl.users_sales_guild_id = 1234; 

LEFT JOIN是這裏的關鍵。即使accessories_orders中沒有對應的條目,它也允許從sales_list的數據返回一行。

看到這個搗鼓工作示例:http://sqlfiddle.com/#!2/f7d3d/4

正如其他人已經說過,你應該使用mysqli功能相對於mysql功能設置設置。

+0

這工作,它檢索正確的行中提交的正確的accessories_orders_total,但添加/掛起按鈕並不完全正常,因爲它只是diplaying「添加」,我會玩弄它並提交代碼完成時。感謝您的幫助@Starson Hochschild,並感謝您的鏈接,這是一個很棒的網站! – AdamMc

+0

我更新了可用的代碼。謝謝@Starson Hochschild – AdamMc

0

首先,你不應該包括表名。例如:

//wrong 
echo '<td align="center">' . $row['sl.sales_model'] . '</td>'; 

//correct 
echo '<td align="center">' . $row['sales_model'] . '</td>'; 

其次,你不應該,除非你使用的是PHP的一個過時的版本使用mysql。切換到mysqliPDO

第三,你沒有錯誤處理來告訴你你的查詢是否成功。事實上,你有相反的錯誤處理,因爲你正試圖使用​​@運算符來抑制你的錯誤。相反,你應該這樣做:

//assuming you switch to mysqli 
mysqli_query("SELECT acc.accessories_orders_id, acc.users_id, acc.sales_id, 
     acc.accessories_orders_total, acc.accessories_orders_date, acc.shipped, 
     acc.timestamp, sl.sales_id 
     FROM accessories_orders AS acc, sales_list AS sl 
     WHERE acc.sales_id = sl.sales_id"); 
if(mysqli_error()) { 
    throw new Exception(mysqli_error(), mysqli_errno()); 
} 

如果你這樣做,你的mysql錯誤將被記錄在你的php錯誤相同的地方。然後,您將能夠判斷您的問題是否來自查詢格式不正確。

+0

感謝您的反饋@spsc_tech – AdamMc

+0

我已經更新了代碼,它工作正常,即使使用mysql函數。我會盡快更新,謝謝你的幫助 – AdamMc

+0

Mysql擴展工作正常,直到它停止釋放數據庫連接(儘管多次關閉()調用並禁用持久連接)並導致服務器崩潰。說起真的很煩人的經歷。 – miyasudokoro