...

View Full Version : Writing away $_POST data to a mysql table



rowantrimmer
02-10-2010, 03:01 PM
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

MattF
02-10-2010, 03:08 PM
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.

rowantrimmer
02-10-2010, 03:48 PM
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

MattF
02-10-2010, 04:00 PM
Assuming that the ingredient entries in post are in their own array, just use a foreach and insert. e.g:



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]);
}

rowantrimmer
02-10-2010, 04:22 PM
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

MattF
02-10-2010, 04:34 PM
First, change your form so that these lines, (and the other 9 following them):



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


so that they use this format, (note the braces around the number):



<input type="text" name="qty[1]"> & <input type="text" name="ing[1]">


Then, when you're working on the $_POST vars use this 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.

rowantrimmer
02-10-2010, 04:58 PM
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

MattF
02-10-2010, 05:04 PM
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.

rowantrimmer
02-10-2010, 06:34 PM
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>

MattF
02-10-2010, 06:44 PM
[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]

rowantrimmer
02-11-2010, 11:01 AM
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>

MattF
02-11-2010, 08:03 PM
Take note of the syntax in the previous examples.



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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum