How to sum values from a database based on user input on a form using PHP?

Viewed 27

So, I created a bunch of drop down options like this:

enter image description here

And I have a table in my database called 'recipes' which will contain meal names and the number of the various ingredients required to make them.

enter image description here

When a user chooses inputs using the drop down boxes and submits it via the POST function, I want to be able to sum the total ingredients required for all the meals they have chosen and echo those values. How can I do this?

My code for the drop down boxes is here:

<label for="dinner7">S</label>
<select name="dinner7" id="mealSelect">
<option name=default id=defaultSelect>None</option>
   <?php 
    $sqli = "SELECT * FROM recipes;";
    $result = mysqli_query($connect, $sqli);
    while ($row = mysqli_fetch_assoc($result)) {
        $mealname = $row['mealname'];
        echo "<option>$mealname</option>\n";
   }
   ?>
</select>
0 Answers
Related