Hello,

I need advice about making a PHP form and MYSQL base.
First, I'll give an example of what I trying to make:
I'm trying to make a MYSQL base of products, then to make new base of ingredients and then to make php form for Add new products and it's ingredients but what is a question here? If I make table products with Name and total weight, and another table ingredients with just Name, how is it possible to add new rows in table Products with name from ingredients table (that will be structure) + weight of that ingredients (that will be value of that structure).

Only solution in my head is to make MSQL table products with a structure that will contain the names of all the ingredients but it is not a good solution for me, that will be a very long structure.

It would be best if I could through the php form add new structure/rows in the database Products.

And one more thing that I need advice is:
How to make a php form that will not show all ingredients in form, just show entries name (from ingredients table) and weight then + add new ingredients, then will open again (name and weight then + add new....) that will add new structure/rows in the database Products with name=(name from ingredients list) and weight as value?

I've made a small localhost cms in which I trying to add this feature.

I'm not good with English, I'm bad in writing but good in understanding :)
I hope I managed to explain what I'm trying to do.
Thank you all for your time to review this topic!

Recommended Answers

All 4 Replies

are you going to do an post-processing using php,because if thats the case you should make different columns in your table . You can initialize all ingredients(weight) to 0 and in your form make a small addon list so that the user only inputs those ingredients he needs and he need not worry about other ingredients.What i mean is like you could have a add ingredient button and then ask the user to choose the kind of ingredient using a dropdown menu

Member Avatar for diafol

I assume that you need 3 tables:

Products
prod_id | prod_name | prod_weight??

Ingredients
ingred_id | ingred_name

Product_ingredients
id | prod_id | ingred_id | ingred_weight

You use JOIN syntax to get info from the products.
You can use just the ingredients table for SELECT into dropdown and for INSERT from ADD field.

@diafol

Products
prod_id | prod_name | prod_weight??

Ingredients
ingred_id | ingred_name

Product_ingredients
id | prod_id | ingred_id | ingred_weight

This looks like good suggestion.
So it should look something like this:

Table Product_ingredients
id    prod_id    ingred_id    ingred_weight
-------------------------------------------
1     8          1            0,445
-------------------------------------------
2     8          2            0,845
-------------------------------------------
3     8          3            6          
-------------------------------------------
4     8          4            0,457 
-------------------------------------------
5     8          5            4        
-------------------------------------------
6     9          3            0,4
-------------------------------------------
7     9          5            1        
-------------------------------------------
etc.

But I do not understand how to add multiple rows with one form, I might have known it, but not all products have the same number of rows.

Or maybe I did not understand concept?

Member Avatar for diafol

No, you understood. You need to have multiple rows of fields in your form. You can have javascript add additional rows of form fields. Soemthing like this:

<form ...>
    <label for="title">Product Title:</label>
    <input name="title" />
    <fieldset class="iRows">
        <div class="ingredientRow">
            <legend>Ingredients</legend>
            <label>Ingredient:</label>
            <select name="ingredient[]">
                <option value="1">Ingredient 1</option>
                <!-- etc from php -->
            </select>
            <label>Weight:</label>
            <input name="weight[]" value="0" />
            <button class="deleteRow">Delete</button>
        </div>
    </fieldset>
    <div class="buttonControls">
        <label>Add More Rows</label>
        <button class="addRow">+ 1</button>
    </div>
    <input type="submit" name="addProduct" value="Add Product" />
</form>

If you use jQuery or similar, you can attach a new row of select ingredients/weight box with something like this:

$('.addRow').click(function(){
    row = '<div class="ingredientRow">
                <legend>Ingredients</legend>
                <label>Ingredient:</label>
                <select name="ingredient[]">
                    <option value="1">Ingredient 1</option>
                    <!-- etc from php -->
                </select>
                <label>Weight:</label>
                <input name="weight[]" value="0" />
                <button class="deleteRow">Delete</button>
            </div>';
    $('.iRows').append(row);
});

That's just a basic example. Not entirely recommeded as there's some duplication of the html in the js, but you get the idea.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.