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...

9
  • You should use a class. This would drastically reduce the number of lines of code Commented Sep 17, 2017 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. Commented Sep 17, 2017 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. Commented Sep 17, 2017 at 18:39
  • I am trying but still didn't get any website that explains this topic in easy way so I can understand. Commented Sep 17, 2017 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 Numbers are wrong, two of the HSN Numbers 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. Commented Sep 17, 2017 at 20:14

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.