0

I am having a list of items I got from a product table in the db.

Now when a client choses a particular category, all the items in that group are displayed and he just enters the quantity for each in a table and submits. Now my issue is that each each time he submits just the last item on the list, it submits to the order table in the database.

What could I be doing wrong.


    <form method="post" action="function.php"  >
        <table> 
            <thead>
                <tr>
                    <th>Products</th>
                    <th>Crate Price</th>
                    <th>Quantity</th>
                    <th>Total Amount</th>                                        
                </tr>
            </thead>
            <tbody>
                <?php    
                    $query = "SELECT * FROM products "; 
                    $select_products = mysqli_query($conn,$query);  
                    while($row = mysqli_fetch_assoc($select_products)){
                        $order_product_id = $row['productID'];
                        $order_product_name = $row['productName'];
                        $order_product_crateprice = 
                            $row['productCratePrice'];
                        echo "<tr>";
                        echo " <td><input type='hidden' name='order_product_id' value='$order_product_id'>$order_product_name </td>";
                        echo " <td><input type='hidden' name='order_product_crateprice'value='$order_product_crateprice'>$order_product_crateprice </td>";
                        echo " <td> <input type='number' name='qty'> </td>";
                        echo " <td> </td>";
                        echo "</tr>";               
                    }
                ?>
            </tbody>
        </table>
        <input class="form-control" name="ordername" id="ordername" 
            type="text" placeholder="Set Order Name" value="" />
        <input class="form-control" name="empties" id="empties" 
            type="number" placeholder="Number of Empties" value="" />
        <input class="form-control" id="cash" name="cash" type="number" 
            placeholder="Number of Cash" value="" />
        <input class="form-control" id="credit" name="credit" type="number" 
            placeholder="Enter your Contact" value="" />
        <input class="form-control" name="orderdate" id="orderdate" 
            type="Date">
        <input type="hidden" name="retailer" id="retailer" 
            value="Undefined" >
        <button type="submit" name="submit_order" 
                class="btn btn-primary btn-SM">
            SUBMIT
        </button> 
        </div>                                
    </form>
    

///////////Add into orders function.php

    function Add_order() {
        global $conn;
        
        if (isset($_POST['submit_order'])) {
            $order_name  = $_POST["ordername"];
            $order_product  = $_POST["order_product_id"];
            $order_crate_price   = $_POST["order_product_crateprice"];
            $order_qty = $_POST["qty"];
            $order_delievry_date  = $_POST["orderdate"];
            $order_empties   = $_POST["empties"];
            $order_cash  = $_POST["cash"];
            $order_credit  = $_POST["credit"];
            $order_retailer  = $_POST["retailer"];
    
            $query = "INSERT INTO orders(orderCode, orderProductID, 
                orderProductQty, orderCratePrice) 
            VALUES ( '$order_name', '$order_product', '$order_qty', 
               '$order_crate_price')";
            $addproduct= mysqli_query($conn,$query);
            if (!$addproduct) {
                die("Insert Failed" . mysqli_error($conn));
            }
                    
            $query = "INSERT INTO payments(paymentOrdercode, 
                paymentOrderRetailerID, paymentOrderCash, 
                paymentOrderEmpties, paymentOrderCredit, paymentOrderDate) 
            VALUES ( '$order_name', '$order_retailer', '$order_cash', 
                '$order_empties','$order_credit','$order_delievry_date')";
            $addpayment= mysqli_query($conn,$query);
            if (!$addpayment) {
                die("Insert Failed" . mysqli_error($conn));
            }
        }    
    }
    ?>
Andrew Halil
  • 1,107
  • 15
  • 14
  • 19
Ken
  • 19
  • 5
  • 1
    It is really hard to tell what your question is. This is tagged with a bunch of tangentially related tags. For instance, bootstrap, datatable and jquery are client-side whereas PHP and MySQL are server-side. Can you simplify your question? – Chris Haas Nov 29 '21 at 17:35
  • Clearly state your problem and show only the code that pertains to that problem. Make it easy for those who would help you – Kinglish Nov 29 '21 at 17:44
  • @ChrisHaas think i have reduced the code – Ken Nov 29 '21 at 17:52
  • @ChrisHaas just edited the whole question. Now the issue is, there are two tables in the bd order and products. there is a list of item that get displayed in a table format coming from the products table. the client just enters the quantity in the various rows by the items in the table and some few others and submit, but upon submission just the last item in the gets submitted to the order table. meanwhile he entered quantity for many items. – Ken Nov 29 '21 at 17:58
  • @Kinglish have done that – Ken Nov 29 '21 at 17:58
  • I think your problem is that you are reusing form field names such as `order_product_id`. In that case, only the last wins. If you change those to `order_product_id[]`, PHP will interpret that to mean an array. For instance, `` – Chris Haas Nov 29 '21 at 17:59
  • @ChrisHaas doing this `` gives a syntax error – Ken Nov 29 '21 at 18:31
  • What is the error? – Chris Haas Nov 29 '21 at 18:49
  • @ChrisHaas `Parse error : syntax error, unexpected ']', expecting '-' or identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in C:\xampp\htdocs\orderfirst\functions\crud\order_function.php on line 59` – Ken Nov 29 '21 at 18:55
  • See [this](https://3v4l.org/v8EM5) for how it should generally look, as well as [this](https://stackoverflow.com/a/45233604/231316) for general form/array syntax. – Chris Haas Nov 29 '21 at 19:48
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/239678/discussion-between-ken-and-chris-haas). – Ken Nov 29 '21 at 20:28
  • `Notice: Array to string conversion in C:\xampp\htdocs\orderfirst\functions\controllers\order_form_handler.php on line 18 Notice: Array to string conversion in C:\xampp\htdocs\orderfirst\functions\controllers\order_form_handler.php on line 18 Notice: Array to string conversion in C:\xampp\htdocs\orderfirst\functions\controllers\order_form_handler.php on line 18 Insert FailedCannot add or update a child row: a foreign key constraint fails (`orderfirst`.`orders`, CONSTRAINT `orders_ibfk_1` FOREIGN KEY (`orderProductID`) REFERENCES `products` (`productID`))` – Ken Nov 29 '21 at 20:43

1 Answers1

0
<form method="post" action="function.php"  >
        <table> 
            <thead>
                <tr>
                    <th>Products</th>
                    <th>Crate Price</th>
                    <th>Quantity</th>
                    <th>Total Amount</th>                                        
                </tr>
            </thead>
            <tbody>
                <?php    
                    $query = "SELECT * FROM products "; 
                    $select_products = mysqli_query($conn,$query);  
                    while($row = mysqli_fetch_assoc($select_products)){
                        $order_product_id = $row['productID'];
                        $order_product_name = $row['productName'];
                        $order_product_crateprice = 
                            $row['productCratePrice'];
                        echo "<tr>";
                        echo " <td><input type='hidden' name='order_product_id[]' value='$order_product_id'>$order_product_name </td>";
                        echo " <td><input type='hidden' name='order_product_crateprice[]'value='$order_product_crateprice'>$order_product_crateprice </td>";
                        echo " <td> <input type='number' name='qty[]'> </td>";
                        echo " <td> </td>";
                        echo "</tr>";               
                    }
                ?>
            </tbody>
        </table>
        <input class="form-control" name="ordername" id="ordername" 
            type="text" placeholder="Set Order Name" value="" />
        <input class="form-control" name="empties" id="empties" 
            type="number" placeholder="Number of Empties" value="" />
        <input class="form-control" id="cash" name="cash" type="number" 
            placeholder="Number of Cash" value="" />
        <input class="form-control" id="credit" name="credit" type="number" 
            placeholder="Enter your Contact" value="" />
        <input class="form-control" name="orderdate" id="orderdate" 
            type="Date">
        <input type="hidden" name="retailer" id="retailer" 
            value="Undefined" >
        <button type="submit" name="submit_order" 
                class="btn btn-primary btn-SM">
            SUBMIT
        </button> 
        </div>                                
    </form>

 function Add_order() {
        global $conn;
        
        if (isset($_POST['submit_order'])) {
            $order_name  = $_POST["ordername"];
            $order_product  = $_POST["order_product_id"];
            $order_crate_price   = $_POST["order_product_crateprice"];
            $order_qty = $_POST["qty"];
            $order_delievry_date  = $_POST["orderdate"];
            $order_empties   = $_POST["empties"];
            $order_cash  = $_POST["cash"];
            $order_credit  = $_POST["credit"];
            $order_retailer  = $_POST["retailer"];
    
            $count = count($order_product);

    for($i=0; $i<$count; $i++){

     $query = "INSERT INTO orders(orderCode, orderProductID, 
                orderProductQty, orderCratePrice) 
                 VALUES ( '$order_name', '$order_product[$i]', '$order_qty[$i]', 
                      '$order_crate_price')";
                $addproduct= mysqli_query($conn,$query);
             if (!$addproduct) {
                  die("Insert Failed" . mysqli_error($conn));
             }
            }
           
                    
            $query = "INSERT INTO payments(paymentOrdercode, 
                paymentOrderRetailerID, paymentOrderCash, 
                paymentOrderEmpties, paymentOrderCredit, paymentOrderDate) 
            VALUES ( '$order_name', '$order_retailer', '$order_cash', 
                '$order_empties','$order_credit','$order_delievry_date')";
            $addpayment= mysqli_query($conn,$query);
            if (!$addpayment) {
                die("Insert Failed" . mysqli_error($conn));
            }
        }    
    }
    ?>