Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Juan, this i getting really long and i'm not sure whether the Stack Overflow site is intended for this kind of dialog, but i'll try to explain once again. I will simplify your tables so the code i post here is simple.</p> <pre><code>CREATE TABLE orders ( orderID int unsigned not null auto_increment, user int unsigned not null, PRIMARY KEY (orderID) ); CREATE TABLE orderitems ( oiID int unsigned not null auto_increment, order int unsigned not null, item int unsigned not null, PRIMARY KEY (oiID) ); </code></pre> <p>The above is assuming that there is a table of users (Where is the user's name and email etc) and table of items (where the item names and item prices are).</p> <p>I'm also assuming (wrongly, but for the sake of simplicity), that in my scenario noone will ever have the desire to buy more than 1 piece of any item i'm selling and noone will ever want to buy more than 3 items at the same time.</p> <p>Then my HTML entry form will be simple enough, because the user must be logged in when ordering (Thus i already know their userID - and it is held in my php $_SESSION['userID'] global variable), the order is always dynamically assigned at the time when user clicks CHECKOUT button (submits the form) and so the only thing we need the user to ENTER is the item ID (or rather pick it from a drop down list for example).</p> <p>so here is VERY SIMPLE entry form:</p> <pre><code>&lt;form action='orderit.php' method='post'&gt; &lt;select name='item[]'&gt; &lt;option value='' selected='selected'&gt;Pick an item from the list!&lt;/option&gt; &lt;option value='1'&gt;Knife&lt;/option&gt; &lt;option value='2'&gt;Spoon&lt;/option&gt; &lt;option value='3'&gt;Lava lamp&lt;/option&gt; &lt;option value='4'&gt;HB pencil&lt;/option&gt; &lt;/select&gt;&lt;br/&gt; &lt;select name='item[]'&gt; &lt;option value='' selected='selected'&gt;Pick an item from the list!&lt;/option&gt; &lt;option value='1'&gt;Knife&lt;/option&gt; &lt;option value='2'&gt;Spoon&lt;/option&gt; &lt;option value='3'&gt;Lava lamp&lt;/option&gt; &lt;option value='4'&gt;HB pencil&lt;/option&gt; &lt;/select&gt;&lt;br/&gt; &lt;select name='item[]'&gt; &lt;option value='' selected='selected'&gt;Pick an item from the list!&lt;/option&gt; &lt;option value='1'&gt;Knife&lt;/option&gt; &lt;option value='2'&gt;Spoon&lt;/option&gt; &lt;option value='3'&gt;Lava lamp&lt;/option&gt; &lt;option value='4'&gt;HB pencil&lt;/option&gt; &lt;/select&gt;&lt;br/&gt;&lt;br/&gt; &lt;input type='submit' value=' Checkout ' name='do'/&gt; &lt;/form&gt; </code></pre> <p>From the above you see than when when user clicks checkout all the data (namely the choices the customer made are sent to the <code>action</code> which is set to <code>orderit.php</code>. Please note that i am not bothered about making sure the user is submiting valid information and i'm not using the latest technology (that being msqli) instead i'm using the deprecated mysql routines.</p> <p>So here it follows:</p> <pre><code>&lt;?php //here you will have your database opened, session initiated and so on // $db contains the database connection handler // $_POST['userID'] contains valid user's id if (isset($_POST['item']) &amp;&amp; count($_POST['item'])&gt;0) { //they ordered something we can create the order $q=mysql_query("INSERT INTO orders SET user='".mysql_real_escape_string($_SESSION['userID'])."'",$db); if (!$q) die("An error while creating new order"); $orderID=mysql_insert_id($db); //fetch the just-now created order's ID foreach($_POST['item'] as $item) { //go through all the ordered items $q=mysql_query("INSERT INTO orderitems SET order='$orderID', item='".abs((int) $item)."'"); if (!$q) die("Couldn't save item $item of order #$orderID"); } print "Your order was saved successfuly!"; } else die("This is an empty order"); ?&gt; </code></pre> <p>And that's all.</p> <p>Please note that i am writing this code from top of my head now, so there may be typos or other types of error. take it more as an approximate guide rather than as a working piece of code.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload