Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12
  1. #1
    New Coder
    Join Date
    Dec 2002
    Posts
    31
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Writing away $_POST data to a mysql table

    I hope that someone can help and I will try to explain myself.

    I have a form in which I am entering recipe information.

    I have 2 fields which in the form are called qty1 & ing1 and there are 10 sets of them with the last set named qty10 & ing10.

    I am returning the data back into the php page and capturing it with

    if (isset($_POST['item']))

    {

    }

    I have a table called recipe_ingredients which could hold 1, 2, 3 or maybe 10 sets of ingredients depending on how many the user has entered in the form where I have the following fields

    id
    recipe_id - this is the id of the main recipe details in the recipe table
    qty
    ing

    What I am trying to do is to create so that if for instance 1 set of ingredient fields has been used it adds 1 record into this table but if say 5 set of fields have been populated in the form it adds in 5 records.

    I have the $_POST variables captured but I am struggling with the script that works out how many records to add into the ingredients table using the php INSERT function.

    I would really appreciate any help here.

    Kind Regards

    Rowan

  • #2
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    If I understand you correctly, I would personally suggest just imploding the ingredients entries from the POST array and inserting them into the (single)? ingredients field in the DB. The other possible option would be a seperate table specifically for ingredients and link each ingredient entry to its parent record in the master table.

  • #3
    New Coder
    Join Date
    Dec 2002
    Posts
    31
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Hiya,

    Yes that is what I was wanting to do I have set up a seperate table for the ingredients.

    id
    recipe_id - id of the recipe in the recipe table
    qty
    ingredient

    I have the $_POST vars in sets qty1, ing1 through to qty10, ing10 but I am struggling with the a script that would add in x records to this ingredients table depending on how many pairs of fields have been populated?

    Does that help?

    I hope you could point me in the right direction for the PHP mysql INSERT script to handle this.

    Thanks again.

    Rowan

  • #4
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Assuming that the ingredient entries in post are in their own array, just use a foreach and insert. e.g:

    Code:
    foreach ($_POST['ingredients'] as $key => $value) //This would equate to name="ingredients['.$count++.']" or similar in your form.
    {
        mysql_query('INSERT INTO ingredients (parent_id, ingredient) VALUES ('.$parent_id.', '.$value.')') or exit([error message here]);
    }

  • #5
    New Coder
    Join Date
    Dec 2002
    Posts
    31
    Thanks
    1
    Thanked 0 Times in 0 Posts
    hello,

    thanks for that although I am very new to php and I am struggling here.

    I have

    <input type =text name =qty1> & <input type =text name =ing1>

    all the way to qty10 & ing10

    I assume that they will be from $_POST['qty1'] & $_POST['ing1'] all the way upto $_POST['qty10'] & $_POST['ing10']

    but I am not too sure what I need to do to the script you kindly did for me.

    Just struggling.....

    Regards


    Rowan

  • #6
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    First, change your form so that these lines, (and the other 9 following them):

    Code:
    <input type =text name =qty1> & <input type =text name =ing1>
    so that they use this format, (note the braces around the number):

    Code:
    <input type="text" name="qty[1]"> & <input type="text" name="ing[1]">
    Then, when you're working on the $_POST vars use this code:

    Code:
    if (isset($_POST['item']))
    {
        [the rest of your code here for inserting the parent/master table entry]
    
        foreach ($_POST['ing'] as $key => $value)
        {
            mysql_query('INSERT INTO ingredients (recipe_id, ingredient, quantity) VALUES ('.$recipe_id.', \''.mysql_real_escape_string($value).'\', '.intval($_POST['qty'][$key]).')') or exit([error message here]);
        }
    }
    $recipe_id is the id of the entry in the master recipe table.
    Last edited by MattF; 02-10-2010 at 04:39 PM.

  • #7
    New Coder
    Join Date
    Dec 2002
    Posts
    31
    Thanks
    1
    Thanked 0 Times in 0 Posts
    thanks again,

    I just don't understand what the $key and $value do?

    I assume I need to get the main recipe id generated first but I have that set to auto in the main table so I am not too sure how I will get that value for use in the recipe ingredients table?

    Rowan

  • #8
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Quote Originally Posted by rowantrimmer View Post
    I just don't understand what the $key and $value do?
    $key is the numeric index of the array, (in your case, 1 through to 10), and $value is what the user entered in the form field.


    I assume I need to get the main recipe id generated first but I have that set to auto in the main table so I am not too sure how I will get that value for use in the recipe ingredients table?
    If you're not using a dblayer which has an insert_id or similar function, just select the last entry from the recipe table when you have updated it and get your id that way. Unless the recipe table is being updated multiple times per second, it should be a usable method.

  • #9
    New Coder
    Join Date
    Dec 2002
    Posts
    31
    Thanks
    1
    Thanked 0 Times in 0 Posts
    This is the code below that I have written so that it displays the next set of ingredients fields on the screen if needed...

    so I need to build the qty[1] and ing[1] into that or in my case

    qty[i] and ing[i] ???

    I will give it a go.

    Thanks again.

    Rowan


    <table id="ingredients" border="0" cellpadding=2 cellspacing=0 width=500 align=left style=border:1px;border-style:solid;border-color:maroon;>

    <tr style="display: block;">
    <td class=greytext width=100>quantity</td>
    <td class=greytext>ingredient </td>
    </tr>

    <tr style="display: block;">

    <td><input name="QTY1" onchange="showNextLine(this);" /></td>
    <td><input name="ING1" onchange="showNextLine(this);" /></td>
    </tr>
    <script type="text/javascript">
    for ( var i = 2; i <= 10; ++i )
    {
    document.write( '<tr id="L' + i + '">'
    + '<td><input name="QTY' + i + '" onchange="showNextLine(this);" /></td>'
    + '<td><input name="ING' + i + '" onchange="showNextLine(this);" /></td>'
    + '</tr>');
    }
    </script>
    </tr>
    </table>

  • #10
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Code:
    [rest of code here]
    	<td><input name="qty[1]" onchange="showNextLine(this);" /></td>
    	<td><input name="ing[1]" onchange="showNextLine(this);" /></td>
    	</tr>
    	<script type="text/javascript">
    	for ( var i = 2; i <= 10; ++i )
    	{
    	    document.write( '<tr id="L' + i + '">'
    	    +  '<td><input name="qty[' + i + ']" onchange="showNextLine(this);" /></td>'
    	    +  '<td><input name="ing[' + i + ']" onchange="showNextLine(this);" /></td>'
    [rest of code here]

  • #11
    New Coder
    Join Date
    Dec 2002
    Posts
    31
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Hi MatF

    The other bit of code that I have had to change is... this controls the hiding and displaying of additional ingredient fields.........

    <script type="text/javascript">
    var lineCount = 1;

    function showNextLine(fld)
    {
    var form = fld.form;
    var fldNum = parseInt( fld.name.substring(4) );
    if ( form.elements["QTY["+fldNum+"]"].value != ""
    && form.elements["ING[" + fldNum+"]"].value != ""
    && fldNum == lineCount )
    {
    ++lineCount;
    document.getElementById("L"+lineCount).style.display = "block";
    }
    }

    </script>

    I thought that I was all set now to see if I could at least display the ingredient / quantity fields before trying to save them into my table...

    In my PHP script I have

    print $_POST['QTY[1]']; print "<br>";
    print $_POST['QTY[2]']; print "<br>";
    print $_POST['QTY[3]'];

    but nothing gets displayed......

    If I print one of the other fields like the recipe title this works so my other print posts must be in the correct place but not displaying anything?

    Do you have any ideas?

    Thanks again.

    Rowan
    ------

    print $_POST[title];

    <script type="text/javascript">
    var lineCount = 1;

    function showNextLine(fld)
    {
    var form = fld.form;
    var fldNum = parseInt( fld.name.substring(4) );
    if ( form.elements["QTY["+fldNum+"]"].value != ""
    && form.elements["ING[" + fldNum+"]"].value != ""
    && fldNum == lineCount )
    {
    ++lineCount;
    document.getElementById("L"+lineCount).style.display = "block";
    }
    }

    </script>

  • #12
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Take note of the syntax in the previous examples.

    Code:
    print $_POST['QTY'][1]; print "<br>";


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •