Orders shopping cart


Hello everyone, I'm a newbie. Thanks for who will help me
I 'm on a local network, a pc runs easyphp, and it hosts a web gallery with a client side shopping cart and a series of pc run the browser with the gallery and they give the opportunity to buy photos.
Shopping carts send the data to the server through a form with an array currently composed by
- Client's name,
- customer email,
- the sold photo (the values are the file path of the photo on the server, the file name and the title of the photo) is sent with item_name_1 for the first photo, item_name_2 for the second photo etc.
- The cost of each item is sent as amount_1 for the first, amount_2 for the second and so on.
- The quantity is sent as quantity_1 for the first item, quantity_2 for the second and so on. However, it is entered only if the quantity is greater than 1. Otherwise, it is assumed that the quantity is 1.
I can make some changes in sending data, but I'd rather avoid having to touch the structure.
What I need to do is:
- Save the data for each transaction
- Run on a page a display of the various transactions
- For each transaction, create a folder on the server with the client name and copy the original photos inside
- Prepare, waiting for the machine is connected to wifi, the upload of the customer folders (password-protected) and send a mail to the customer with the link and password to download the photos.
I do not ask for the code of all this. I kindly ask you a comparison on how to structure and design everything, given the starting structure and what I need. Thanks in advance.


Welcome to the forums, @diegosaggiorato. Why don't you start by telling us what you have so far in terms of the structure and design. In other words, what is your attempt at doing what you are asking members here to.provide you with?


At the moment I have the web galleries and the shopping cart ( which is already able to send the form as I described ).
That's all I have ready
I need to project the "backoffice", with the goals I listed before.
I know I need php and mysql. I would like a suggestion on how to think the whole work, how to handle it, a kind of steps guide. I don't need coding examples, there are dozens of tutorial.
For example I 'm not sure how to design the tables of the database.


first you think about unique parts of information - a.k.a. relations - you need, like

users, photos, purchases

then you think about what properties they may have, like

id, name, email, price, dates

then you think how they correlate with each other

a user has many photos
user buys many photos

so you end up with a table structure like that

create table users (id int primary key, name varchar(250), email varchar(250));
create table photos (id int primary key, owner int, name varchar(250), price decimal(8,2));
create table purchases (id int primary key, item int, name varchar(250), sold decimal(8,2));

now you can go on with foreign key constrains or other in-database checks.


Thank you chorn!
I decided to create a table

ORDERS with : id, clientname, clientemail, date

And one ITEMS with: id, id_id (orders),itemname, quantity, amount

in order to have multiple items on the same order.

Now I receive from the form an array with: item_name_1, amount_1, quantity_1, item_name_2, amount_2, quantity_2, etc.

I made some tries and I searched some forums, but I wasn't able to figure out how to loop over the array on order to save it in the database.
Could anyone give me a hint?


your form structure is messy, use arrays

<input type="text" name="amount[1]" /> Item 1 // use ID as the array key
<input type="text" name="amount[2]" /> Item 2


I know it's a messy, but I'm not sure I can modify the form. It's implemented in a big web app , I 'd prefer to keep it as it is.

Any other option avialable?


If the form cannot be changed, then you'll just have to loop through the form submission. It's not all that different to bringing it in as an array.

$total_lines = 99; // or some arbitrary value if the form doesn't send it
while ($line < $total_lines) {
  $itemname = $_POST['item_name_' . $line];
  $amount = $_POST['amount_' . $line];
  $quantity = $_POST['quantity_' . $line];
  // now do what you need with those variables
  } // end of loop

So you'll be working with the variables created each time that loop runs. It'd be a bit easier with an array because you could deal easily with how many items have been sent from the form. As I've done it, you'll also need to check whether those $_POST variables exist before you start using them (i.e. before you assign them to the other variable names), otherwise you'll get errors. It might be the case (depends on your form layout) that as soon as you hit a value of $line where the $_POST variables don't exist, that's because it's the end of the form, and you can just break out of the loop.

I'd suggest you consider a better name than id_id for the reference into the ORDERS table. To me, the logical name would be orders_id as it gives an idea of what it's used for.


Thank you droopsnoot, good calls. I tried yesterday something similar:

for ($i=1; $i<=99; $i++) { // 
  $item_name = 'item_name'.$i;
  $var = $_POST[$item_name];
  // processing of $var

As you said, I got errors when $_POST variables didn't exist. How can I check it, or how can I exit the loop when variables are finished?


A common method is to use isset() - that will tell you whether the $_POST variable is there:

if (isset($_POST[$item_name])) { 
  // it exists, so you can use it

To exit the loop you could use break (the proper way), or just set $i=100 in your specific example. The break option is better because you don't have to change it if you change the values in your loop.


Hello again, I wasn't able to use the method you said.

I decided to change the form, and now I have a series of array like item_name[], amount[], quantity [].

Now I'm tryng to save the arrays into mysql , in order to have multiple rows , one row for every product.
I tried:

$item_path = $_POST['item_path'];
foreach ( $item_name as $key => $value){
$sql = "INSERT INTO products ( name) VALUES ("
     . "'" . $value . "')"
  if ($cn->query($sql) === TRUE) {
    echo "New record created successfully";
        } else {
        echo "Error: " . $sql . "<br>" . $cn->error;


$item_path = $_POST['item_path'];
foreach ( $item_name as  $row){
$sql = "INSERT INTO products ( name) VALUES ("
     . "'" . $row . "')"   ;    
  if ($cn->query($sql) === TRUE) {
    echo "New record created successfully";
        } else {
        echo "Error: " . $sql . "<br>" . $cn->error;

but it saves only one row, with the last product.
If I display the arrays, i can see all the data, but it seems I'm not able to save all of them , possibly on multiple rows.
Could you help me?


Do you have a typo here?

$item_path = $_POST['item_path'];
foreach ( $item_name as $key => $value){

I haven't done much with form arrays, but wouldn't you need to foreach() through $item_path, not $item_name, as I can't see where you create the latter variable at all?

Aside from that, though, the problem is that you run the foreach() loop, but don't execute the query until after it has closed. Hence you only get the final row inserted.

foreach ( $item_name as $key => $value){
  $sql = "INSERT INTO products ( name) VALUES ("
     . "'" . $value . "')"
  // the call to $cn->query needs to go here, inside the loop
// you have it here, after the loop has finished.


Ty droopsnoot!
I'm feeling a little stupid....


No need to, often it's just a different pair of eyes that spots something you've been staring at for hours.


This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.