Jump to content


This topic is now archived and is closed to further replies.


Inserting multiple entries into single ROW & FIELD for sales record

Recommended Posts

Hey All!

I am working on a purchasing system.  All seems to work thusfar...except a customer may select more than one item for purchase.  Currently the MSQL DB only lists the one item in the sales table.  I would like each additional item (the book# ISBN) to be listed in the same row (the ISBN field).  How would I go about doing this?

Here is the code I have:

[code]<?php $database=mysql_pconnect('localhost','jandj_root');
$total = '0';

if (count($selectedArray) > 0) { // If the shopping cart is not empty...
foreach ($selectedArray as $value) {      // For each book id in the shopping cart...
// Get the book information
$query="select * from jandj_jandj.books where id=".$value." and display='JJ' limit 1";

$align = $left;


// Display the information of the selected book
echo '<div id="book"> <p><font color="#7b8049" face="Geneva, Arial, Helvetica, sans-serif"><b>';
echo '<img src="../images_books/'.$image.'" width="47" height="69" align="'.$align.'">';
echo $title;
echo '<br></b></font>
<b><font color="#000000" size="1" face="Geneva, Arial, Helvetica, sans-serif">
by '.$author.'
<p><b><font color="#000000" size="2" face="Geneva, Arial, Helvetica, sans-serif">
J &amp; J price: '.$priceAct.' &#8364;


As you see above I have each book added to the shopping cart set to list for the customer's confirmation.  Addtionally the prices are added to create a 'Grand Total'.

Here is the INSERT for the purchase into the DB:

[code]<?php $status= '1'; // 1 - placing the order
$_SESSION['sessionOrderNum'] = $orderNum;  // Track the order number for when we return from Caixa Cataluyna

$submit="insert into jandj_jandj.sales values ('','".$customerID."',NOW(),'".$grandtotal."','".$paymentType."','".$status."','".$orderNum."','".$isbn."')";
// echo 'insert $result = '.$result.'<br>$submit='.$submit;
if ($result)
// New sales record was created. Move along
else {

As you see above I have the 'Insert' for the Order information.  However, it only inserts ONE 'ISBN' number.  Additionally, I want to maintain just one 'orderNumber' (1 orderNumber per row in DB), but I would like the ISBN column to list each 'ISBN' in the order.  The client will need to know what books were ordered!

Any and all help would be GREATLY appreciated!!!


Share this post

Link to post
Share on other sites
i'm not sure i follow. are you wanting all ISBN's to be listed in the [b]one field[/b], or are you willing to consider setting up an associative table? if it's the former, you'll need to do something like:
$isbn = array('#1', '#2', '#3';
$sql = mysql_query("INSERT INTO table (isbn) VALUES ('" . implode(',', $isbn) . "')");

that way, you're creating one string to be inserted with several ISBN's in it.

if you're willing to consider a table change, i would recommend having an order table where each row contains one order number, but have another table for [b]order detail[/b] where you can store as many records as you want (ISBN's), and each row would then reference an order number. so, you could have 15 entries in the order detail table that all reference the same order. make sense?

hope this helps some.

Share this post

Link to post
Share on other sites

Unfortunately, perhaps because I wasn't clear...I'm not so sure I understand what your suggestion is  :-\

Here's what I would like to happen:

1. Customer places 3 books in the shopping cart.
2. Customer checks out
3. PHP inserts a new row and creates an 'orderNum' 1 higher than the highest currently in the database
4. The 'isbn' field receives 'isbn' numbers for each book in the order


Does this help?  I'm not sure how else I might explain it..except my client will need to query the DB based on 'orderNum' and view each book purchased under that 'orderNum'.

Please let me know your thoughts.



Share this post

Link to post
Share on other sites
Any Ideas?  Any and All help will be greatly appreciated!


Share this post

Link to post
Share on other sites

I'm not sure how I would use the code snippet you gave me.

I have an 'if/foreach' loop that pulls the DB information for the customer to view while checking out.  Then the price and shipping, etc. are put together for the transaction with the bank. 

This same information is used to create the sales record.  HOWEVER, the sales table as it currently stands on receives ONE new row and that row only contains 'I believe' the last 'ISBN' if it was the last in the loop.

How can I put together a list of variables that are retreved and then enter them in to one row in the DB (please see my above grafic)??

Please, help...I'm getting a bit crazy....



Share this post

Link to post
Share on other sites


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.