1

Thanks in advance. I am new to php and want to create a simple software for a shop. Now I have created a form that allows to add items one after another (one at a time). But I want to make it flexible so when I press "Add Row" button then one more row is shown and after filling multiple lines, I need to press "Submit" only once and all rows are stored in database.

So far I have written this code (I haven't added CSS yet) :

This is file : index.php

<?php
    function companyName() {        
        $conn = mysqli_connect('localhost','root','root','DataShop');
        if (!$conn) {
            echo "Couldn't retrieve company names...!";
        }
        $sql = "SELECT Name FROM `Companies`";
        $sql2 = "SELECT CompanyName FROM `DataShop` WHERE ID = ( SELECT max(ID) FROM `DataShop` )";
        $companies_result = mysqli_query($conn,$sql);
        $companies_result_2 = mysqli_query($conn,$sql2);
        if ((!$companies_result)||(!$companies_result_2)) {
            echo "Query Failed for company names";
        }
        while($company = mysqli_fetch_array($companies_result_2, MYSQLI_ASSOC)) {
            echo '<option value="';
            echo $company['CompanyName'];
            echo '">';
            echo $company['CompanyName'];
            echo '</option>';
        }
        while($companies = mysqli_fetch_array($companies_result, MYSQLI_ASSOC)) {
            echo '<option value="';
            echo $companies['Name'];
            echo '">';
            echo $companies['Name'];
            echo '</option>';
        }
        mysqli_close($conn);
    }

    function lastInvoice() {
        $conn = mysqli_connect('localhost','root','root','DataShop');
        if (!$conn) {
            echo "Couldn't retrieve company names...!";
        }
        $sql = "SELECT InvoiceNumber FROM `DataShop` WHERE ID = ( SELECT max(ID) FROM `DataShop` )";
        $invoice_result = mysqli_query($conn,$sql);
        if (!$invoice_result) {
            echo "Query Failed for company names";
        }
        while($invoice = mysqli_fetch_array($invoice_result, MYSQLI_ASSOC)) {
            echo $invoice['InvoiceNumber'];
        }
        mysqli_close($conn);
    }

    function lastDate() {
        $conn = mysqli_connect('localhost','root','root','DataShop');
        if (!$conn) {
            echo "Couldn't retrieve company names...!";
        }
        $sql = "SELECT Date FROM `DataShop` WHERE ID = ( SELECT max(ID) FROM `DataShop` )";
        $invoice_result = mysqli_query($conn,$sql);
        if (!$invoice_result) {
            echo "Query Failed for company names";
        }
        while($invoice = mysqli_fetch_array($invoice_result, MYSQLI_ASSOC)) {
            echo $invoice['Date'];
        }
        mysqli_close($conn);
    }

?>

<!DOCTYPE html>
<html>
<head>
    <title>Submission Form</title>
    <link rel="stylesheet" type="text/css" href="login.css">
</head>
<body>
    <div id="division1">
    <div id="division2">
        <form method="post" action="action.php">
            <table>
                <tr>
                    <td>Company Name :</td>
                    <td>
                        <select name="company_name">
                            <?php companyName(); ?>
                        </select><br>
                    </td>
                </tr>
                <tr>
                    <td>Date :</td>
                    <td><input type="date" name="date1" placeholder="" value="<?php echo date('Y-m-d');?>" size=""><br></td>
                </tr>
                <tr>
                    <td>Invoice Number :</td>
                    <td><input type="text" name="invoice_number" placeholder="" value="<?php lastInvoice(); ?>" size=""><br></td>
                </tr>
                <tr>
                    <td>Item Name :</td>
                    <td><input type="text" name="item_name" placeholder="" size=""><br></td>
                </tr>
                <tr>
                    <td>Description :</td>
                    <td><input type="text" name="description" placeholder="" size=""><br></td>
                </tr>
                <tr>
                    <td>HSN Number :</td>
                    <td><input type="text" name="hsn_number" placeholder="" size=""><br></td>
                </tr>
                <tr>
                    <td>Quantity :</td>
                    <td><input type="number" name="quantity" placeholder="" size="" min="1" max="1000"><br></td>
                </tr>
                <tr>
                    <td>MRP :</td>
                    <td><input type="number" name="mrp" placeholder="" size="" min="1"><br></td>
                </tr>
                <tr>
                    <td>Cost :</td>
                    <td><input type="number" name="cost" placeholder="" size=""><br></td>
                </tr>
                <tr>
                    <td>GST % :</td>
                    <td>
                        <select name="gst">
                            <option value="0.0">0.00</option>
                            <option value="5.5">5.50</option>
                            <option value="15.0">12.00</option>
                            <option value="15.0">18.00</option>
                            <option value="28.0">28.00</option>
                        </select><br>
                    </td>
                </tr>
                <tr>
                    <td><input type="submit" name="" placeholder=""></td>
                    <td><input type="reset" name="" placeholder=""></td>
                </tr>
            </table>      
        </form><br><br><br>
        <a href="Goods.php">Show Records</a><br><br>
        <a href="Search.html">Search</a>
    </div>   
   </div>
</body>
</html> 

And the action file : action.php

<?php

    $db = mysqli_connect('localhost','root','root','DataShop');

   if (!$db) {
    echo 'Failed to connect to database...';
   }

    $companyName = $date = $invoiceNumber = $itemName = $hsnNumber = $description = $quantity = $mrp = $cost = $gst = '';

    $companyName = $_POST['company_name'];
    $date = $_POST['date1'];
    $invoiceNumber = $_POST['invoice_number'];
    $itemName = $_POST['item_name'];
    $hsnNumber = $_POST['description'];
    $description = $_POST['hsn_number'];
    $quantity = $_POST['quantity'];
    $mrp = $_POST['mrp'];
    $cost = $_POST['cost'];
    $gst = $_POST['gst'];

    $sql = "INSERT INTO `DataShop` (CompanyName, Dates, InvoiceNumber, ItemName, Description, HSNNumber, Quantity, MRP, Cost, GST) VALUES ('$companyName','$date','$invoiceNumber','$itemName','$description','$hsnNumber','$quantity','$mrp','$cost','$gst')";

    $result = mysqli_query($db,$sql);
    if(!$result) {
        echo 'No result';
    }
    else {
        echo 'One record inserted';
    }

?>

I have got idea of using array to store the data but yet it is not clear how to do that.

I want something like when user puts data like "Product Name", then it goes into the variable/array "ProductName[]" and then stored in database one by one automatically.

Thanks again...

Mahesh Suthar
  • 146
  • 1
  • 12
  • You should use a class. This would drastically reduce the number of lines of code – Rotimi Sep 17 '17 at 18:36
  • yes, you are right. But as I said I am still naive in php, so I will learn gradually. Thanks for help. I will keep in mind next time or modify it later. – Mahesh Suthar Sep 17 '17 at 18:38
  • You know what you need to do, so whats stopping you doing it? You just need to research how to add rows with javascript and how to handle arrays in a `foreach`, plus how to stop SQL injections as it's going to break if you add any `'`s in your data. You also don't need to connect/disconnect to mysql each time. – Lawrence Cherone Sep 17 '17 at 18:39
  • I am trying but still didn't get any website that explains this topic in easy way so I can understand. – Mahesh Suthar Sep 17 '17 at 18:42
  • Mahesh, this is my advice, for now: don't do, what you have proposed yourself to do! Giving the user the option of completing multiple "rows", e.g records, at once is user unfriendly. Why? Just imagine yourself, that the user displays five records, completes them and clicks on Submit. Now, let's say, a client-side validator shows that all `Invoice Number`s are wrong, two of the `HSN Number`s are not even completed, three dates are not correctly written. The user would need then to scroll up and down, find the red error inputs, correct them overall, etc. –  Sep 17 '17 at 20:14
  • @aendeerei I understood your point and it is valid for sure. But I am trying to make it for a shop so when shopkeeper gets the delivery of goods, he stores them into database by using the bill. Now if he will need to fill the form again and again for each item and then submit it, then it would not be user friendly either. Suppose he got 74 items and now he needs to enter all those items individually. – Mahesh Suthar Sep 17 '17 at 20:20
  • Good. Let's go on the server-side validation. You must implement such a thing too. So, the user submits and there arise some other errors regarding the inputs. What happens? The user is again forced to check all the five records for errors and correct them. Then,... he submits again. All good. But now, not all records were successfully added to the database, because of... multile causes. You have then to track the records, and code a mechanism to show this to the user. But in the form: "Record 3, error `HSN number` too long", "Record 3+6, error `Invoice Number` not the right format", etc etc. –  Sep 17 '17 at 20:21
  • Of course it is user-friendly. One record means one item, e.g. means one form with 10 (!) inputs. You can make a beautiful code by allowing him to complete the `Company Name`, the `Date` and the `Invoice Number` only once. Then, when he submits the first item of the same invoice, he already has these 3 fields completed, when the second item fields are displayed. –  Sep 17 '17 at 20:30
  • My last argument. Try to use phpMyAdmin and to insert 10 records at once. You can decide yourself, if is user-friendly ;-) Now, about inserting multiple records, you could look into my answer [Inserting multiple rows at once with prepared statements](https://stackoverflow.com/questions/46198614/inserting-multiple-rows-at-once-with-prepared-statements/46203296#46203296) to see how I built the sql statement (only one) for it. Note that the rest is PDO. Good luck. –  Sep 17 '17 at 20:41

0 Answers0