-1

Background

I have a database which has a table called menuitems. The menuitems store attributes of a menu of a resteraunt. The columns include "itemno, name, price, discount, category"

The menu items are displayed via sql in the menu.php page. You have two options. You can either click on the Food Category or Beverage category.

Upon clicking the Food category, for example. It will display the "name" and "price" under the columns "Items" and "Unit Price"

Now this webpage is done by someone else. My job, is to try to figure out a way to get the Quantities of this order and insert the order into the "orderdb" table

The "orderdb" table has orderid (autoincrement), itemno, qty, idtable, subtotal. The subtotal is calculated by multiplying the itemno and quantity.


This is the code.

What I have done uptil now, is created a textfield on the menu.php page where the user can enter the quantity and I have created a button that says "+" representing to add the order into the orderdb table.

But I am not sure what else to do. I know how to execute this in simply SQL language and its terminology but my PHP is very weak.

This is my code uptil now.

    <!DOCTYPE html>
    <html>
    <title>Menu List</title>
    <head>
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <style>
        #foodDIV {
          width: 100%;
          padding: 50px 0;
          text-align: center;
          background-color: lightblue;
          height:400px;
          width:500px;
          display: none;
        }
        #bevDIV2 {
          width: 100%;
          padding: 50px 0;
          text-align: center;
          background-color: pink;
          height:400px;
          width:500px;
          display:none;
        }

        #menuNav {

          border: 1px solid #ccc;
          background-color: #f1f1f1;
        }

        /* Style the buttons inside the tab */
        .menuNav button {
          background-color: inherit;
          float: left;
          border: none;
          outline: none;

          padding: 14px 16px;
          font-weight:bold;
          font-size: 18px;
        }

        /* Change background color of buttons on hover */
        .menuNav button:hover {
          background-color: #ddd;
        }
        th {
          padding-top: 12px;
          padding-bottom: 12px;
          width:150px;

          color: white;
        }



        </style>
    </head>

    <body>

        <div class="menuNav">
            <button onclick="categorize('food')">Food</button>
            <button onclick="categorize('beverage')">Beverage</button>
        </div>

        <div id="foodDIV">

            <table>
                <tr>
                    <th>Items</th>
                    <th>Unit Price</th>
                    <th>Qty</th>                    
                    <th>Add item</th>
                    <th> Remove Item</th>
                </tr>
            <?php
            $conn=mysqli_connect("localhost","root","","tabletable");
            if($conn->connect_error){
                die("Connection failed:".$conn->connect_error);
            }
            $sql= "SELECT name,price from menuitems where category='Food'";
            $result =$conn ->query($sql);

            if($result-> num_rows>0){
                while($row=$result->fetch_assoc()){
                    echo "<tr><td>".$row["name"]."</td><td>".$row["price"]."</td><td><input type='number' name='fquantity'></td><td><input type='button' value='+'></td><td><input type='button' value='-'></td></tr>";
                }
                echo"</table>";
            }
            else{
                echo "0 result ";
            }
            $conn->close();
            ?>
            </table>
        </div>
        <div id="bevDIV2">

            <table>
                <tr>
                    <th>Items</th>
                    <th>Unit Price</th>
                    <th>Qty</th>
                    <th>Add item</th>                                       
                    <th>Remove item</th>

                </tr>
            <?php
            $conn=mysqli_connect("localhost","root","","tabletable");
            if($conn->connect_error){
                die("Connection failed:".$conn->connect_error);
            }
            $sql= "SELECT name,price from menuitems where category='Beverage'";
            $result =$conn ->query($sql);

            if($result-> num_rows>0){
                while($row=$result->fetch_assoc()){
                    echo "<tr><td>".$row["name"]."</td><td>".$row["price"]."</td><td><input type='number' name='bquantity'></td><td><input type='button' value='+'></td><td><input type='button' value='-'></td></tr>";
                }
                echo"</table>";
            }
            else{
                echo "0 result ";
            }
            $conn->close();
            ?>
            </table>
        </div>

    <script>
    function categorize(ctg) {
        if(ctg=='food'){
          var x = document.getElementById("foodDIV");
          var y=document.getElementById("bevDIV2");
          if (x.style.display !== "block"){
              x.style.display = "block";
          }
          if (y.style.display !== "none"){
              y.style.display = "none";
          }
        }
    else if(ctg=='beverage'){
        var x = document.getElementById("bevDIV2");
        var y=document.getElementById("foodDIV");
        if (x.style.display !== "block"){
              x.style.display = "block";
        }
          if (y.style.display !== "none"){
              y.style.display = "none";
          }
    }
    }


    </script>

    </body>
</html>

Where do I go from here?

Image1

Menu.php page

This is where the menu items are stored

This is the table where it needs to be stored in. The quantity, the item, the subtotal is calculated by price * quantity

halfer
  • 19,471
  • 17
  • 87
  • 173
  • 1
    Helpful advice: if readers need database data from you in order to answer the question, it should go into the question itself. However, I wonder if the question is rather too broad for Stack Overflow anyway, and readers would be best giving advice just in comments. – halfer Oct 16 '19 at 17:56
  • Yes I am not too familiar with this platform overall. I know people can help out so I post here. – Muhammad Shaeel Abbas Oct 16 '19 at 17:58
  • 1
    My guess is that you need an AJAX operation, which is a piece of JavaScript that will send the data transparently to a PHP script, which in turn will write the data to the database and return some results (e.g. the success of the write). If that sounds like a good fit for your use case, I recommend searching for "PHP AJAX" on the web and doing some research - there are plenty of examples around, and we don't need to type it out again here `;-)`. – halfer Oct 16 '19 at 17:59
  • Incidentally, you may not have read [ask] - the posting guidelines are rather different at Stack Overflow, which is why everyone wants to come here. The main point to note is that questions that do not meet the posting guidelines can be closed, by a process of democratic voting. I think this question meets the criteria for Too Broad. – halfer Oct 16 '19 at 18:02
  • Okay brother, so I'll check for PHP AJAX on google. But anything in specific? Are you understanding what I have to do here @halfer – Muhammad Shaeel Abbas Oct 16 '19 at 18:02
  • Yes, I think I understand what you need. When you click on the +/- buttons it increments or decrements the quantity field, and changes the database contents. If that is correct, then AJAX is a good way to do it. You can do it with raw JavaScript, but people sometimes use jQuery, a JavaScript library that makes it a bit easier. – halfer Oct 16 '19 at 18:04
  • It is a very bad idea to use `die($conn->connect_error);` in your code, because it could potentially leak sensitive information. See this post for more explanation: [mysqli or die, does it have to die?](https://stackoverflow.com/a/15320411/1839439) – Dharman Oct 16 '19 at 18:04
  • @halfer what if you simply do SQL INSERT Statement INSERT INTO ORDERDB (itemno,qty) VALUES ( **Whatever is the ID of the ITEM displayed on the page** , **Whatever is in the input field**) Something like that I am trying to achieve through PHP And SQL – Muhammad Shaeel Abbas Oct 16 '19 at 18:08
  • @Muhammad: it's a bit more complex than that. You can certainly run `INSERT` queries in PHP, but in this use case, you probably want to do them on a JS event rather than in a traditional form. – halfer Oct 16 '19 at 20:41

1 Answers1

1

You've shown us a php program to read a database table, construct an HTML page, and sent it to a browser. The HTML generated by your php program contains some Javascript code to handle some user interface operations.

The HTML page and the Javascript it contains run in your users' browsers, not on your php server. There's nothing at all in your program allowing the browser to get a php program to do anything on your server.

And your MySQL database is only available from your server. It makes no sense at all for a browser-resident Javascript program to, itself, try to INSERT or UPDATE some database rows.

Generally the beginners' way to run a php program when a user clicks something in a browser is to use an HTML form, and make the click submit the form. The form gets submitted to your php program, which does the INSERT or whatever, and sends a new copy of the HTML page back to the browser.

Once you're thoroughly familiar with that kind of form-post workflow, you can consider adding AJAX or Fetch requests to the Javascript in your web page. AJAX and Fetch can send data from your web page's Javascript to your php server without requiring the HTML page to be refreshed.

With respect, may I suggest you work your way through a php / forms tutorial or two before you move forward with this project? There's much knowledge you need and don't have.

O. Jones
  • 92,698
  • 17
  • 108
  • 152
  • Oh it's supposed to be running locally. Using localhost. Like, it's part of the requirement you can say. Project based. You have to import the database. If refreshing the page is not an issue then do I still need AJAX? Can you guide me further with knowing the information you have now about my case? – Muhammad Shaeel Abbas Oct 17 '19 at 01:46
  • "locally" probably does not mean what you think it means. Your Javascript runs in your browser, so that's one kind of local. Your php runs in your server, which happens to be on localhost, your local machine. They are not the same thing. Even if the database is running on the same machine as your browser, **the Javascript in your browser must go through php to access the database.**. Do a tutorial or two: you are missing a basic concept. – O. Jones Oct 17 '19 at 11:11