Jump to content

[SOLVED] multi table query


bhavin_85

Recommended Posts

hi guys

 

 

im trying to create the tables so that my users can see their invoices online. Each item is generic hence the item table, the price is entered manually by the user

 

i have created the following tables:

Invoices: invoice_id(*), cust_id and invoice_no

Item: item_id(*), item_name

Invoice_items: invoice_item_id(*),invoices_invoice_no,item.item_no, description, weight, price

 

I need to create a query that will lift the following information:

cust_id, invoice_no, then for each item purchased show the item_name, description, weight and price

 

how do i get a query to get information from each table? is it possible in just 1 query? the main identifier will be the cust_id which is already set as a session variable and is avaialble on the page.

 

you help is much appreciated :)

 

Bhav

Link to comment
Share on other sites

[pre]

Invoices:                Invoice_items:              Item:

 

invoice_id(*)            invoice_item_id(*)    +---- item_id(*)

cust_id        +------  invoices_invoice_no    |    item_name

invoice_no  ----+        item_item_no      ----+

                        description

                        weight

                        price

[/pre]

 

SELECT a.cust_id, a.invoice_no, c.item_name, b.description, b.weight, b.price
FROM invoices a
    INNER JOIN invoice_items b ON a.invoice_no = b.invoices_invoice_no
    INNER JOIN item c ON b.item_item_no = c.item_id
WHERE a.cust_id = '$custId'

Link to comment
Share on other sites

i just realised i messed the tables up a bit...oppps

 

Invoices: invoice_id(*), cust_id

Item: item_id(*), item_name

Invoice_items: invoice_item_id(*),invoices.invoice_id,item.item_no, description, weight, price

 

so that translates into

 

 

Invoices:                Invoice_items:              Item:

 

invoice_id(*)--+        invoice_item_id(*)    +---- item_id(*)

cust_id          +----  invoices.invoice_id    |    item_name

                              item.item_id      ----+

                            description

                            weight

                            price

 

so would that translate into this code?

$sql="SELECT a.cust_id, a.invoice_id, c.item_name, b.description, b.weight, b.price
FROM invoices a
    INNER JOIN invoice_items b ON a.invoice_id = b.invoices.invoice_id
    INNER JOIN item c ON b.item_item_no = c.item_id
WHERE a.cust_id = '$custId'";

 

now im getting error:

Unknown column 'b.invoices.invoice_id' in 'on clause'

Link to comment
Share on other sites

ok so i figured out that echo should do print the data...

 

still getting an error though


<?
session_start();
if ( empty($_SESSION['username'])){
header("location:default.php");
exit;
}
$id=$_SESSION['cust_id'];

include ('config.php');

$sql=$sql="SELECT a.cust_id, a.invoice_id, c.item_name, b.description, b.weight, b.price
FROM invoices a
    INNER JOIN invoice_items b ON a.invoice_id = b.invoices_invoice_id
    INNER JOIN item c ON b.item_item_id = c.item_id
WHERE a.cust_id = '$id'";
$query=mysql_query($sql) or die(mysql_error());
while (mysql_num_rows($query) != 0) { 
$row = mysql_fetch_assoc($query);
echo $row["cust_id"];
echo $row["invoice_id"];
}

 

error: 4141

Fatal error: Maximum execution time of 60 seconds exceeded in C:\Program Files\xampp\htdocs\PBL\invoice.php on line 21

 

line 21 is the last line of code pasted - the }

 

its 3 in the morning and i cant figure it out  ???

Link to comment
Share on other sites

while (mysql_num_rows($query) != 0) {  is the entirely wrong way of doing it (no offense intended).

 

Unless reset, the number of rows will stay the same....

 

Change that line to while($row = mysql_fetch_assoc($query)) {

 

If you don't know what that does, look it up on Google or the PHP manual.

Link to comment
Share on other sites

i tried that before and i didntget it 2 work  ??? but all sorted now :)

 

heres the code if any1 wants it

 

<?
session_start();
if ( empty($_SESSION['username'])){
header("location:default.php");
exit;
}
$id=$_SESSION['cust_id'];

include ('config.php');

$sql=$sql="SELECT a.cust_id, a.invoice_id, c.item_name, b.description, b.weight, b.price, b.date
FROM invoices a
    INNER JOIN invoice_items b ON a.invoice_id = b.invoices_invoice_id
    INNER JOIN item c ON b.item_item_id = c.item_id
WHERE a.cust_id = '$id'";
$query=mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_assoc($query)) {
$_SESSION['cust_id'] = $row["cust_id"];
$_SESSION['invoice_id'] = $row["invoice_id"];
$_SESSION['item_name'] = $row["item_name"];
$_SESSION['description'] = $row["description"];
$_SESSION['weight'] = $row["weight"];
$_SESSION['price'] = $row["price"];
$_SESSION['date'] = $row["date"];
}
?>

Link to comment
Share on other sites

Out of curiosity, wouldn't it be better to put the description, weight, and price under the Item table, otherwise you're not normalized ... description and weight have nothing to do with the primary key of invoice_items ...

 

It would change your query slightly, but you'd eliminate a lot of redundant data in the database ... listing the description for an item only once instead of X number of times in the invoices.

 

Just another thought to throw at you ...

Link to comment
Share on other sites

good suggestion nloding....but the price, weight and desc cant be put in the item table because the item table only hold the main categories ie ring, watch, necklace, they are a gerneralised category....where as when the invoice is created that category is used and the user manually enters the details...ie price, weight and desc

 

cheers

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.