|
|
|
Next: Oracle query needed
|
| Author |
Message |
External

Since: Dec 08, 2008 Posts: 5
|
(Msg. 1) Posted: Mon Dec 08, 2008 10:32 am
Post subject: how to populate multiple records in a mysql DB Archived from groups: alt>php>sql (more info?)
|
|
|
I am new at this and looking for guidance......
I have a website using PHP and I connect to a mysql db.
I have currently created a form and I can populate a record in that db
I now wish to fill in a form containing multiple rows (the number of
rows being different each time) and then upload that to the database
tables.
Can you help me.
thanks |
|
| Back to top |
|
 |  |
External

Since: May 31, 2006 Posts: 2034
|
(Msg. 2) Posted: Mon Dec 08, 2008 6:00 pm
Post subject: Re: how to populate multiple records in a mysql DB Archived from groups: per prev. post (more info?)
|
|
|
BURL ives wrote:
> I now wish to fill in a form containing multiple rows (the number of
> rows being different each time) and then upload that to the database
> tables.
Use arrays in the form
<input type="text" name="something[]">
then you get the results with
$_REQUEST['something']
you can then loop the array with foreach().
Of course you may want to have more than one "column", in those cases you can
use a 2D array and of course you can index the cells in the array already in
the form.
There are other ways to do it too, but this is IMHO the simplest way.
--
//Aho |
|
| Back to top |
|
 |  |
External

Since: Dec 08, 2008 Posts: 5
|
(Msg. 3) Posted: Mon Dec 08, 2008 6:00 pm
Post subject: Re: how to populate multiple records in a mysql DB Archived from groups: per prev. post (more info?)
|
|
|
THANKS JO for a prompt response. I'm going to work on that today.
On Mon, 08 Dec 2008 18:00:53 +0100, "J.O. Aho" <user.RemoveThis@example.net>
wrote:
>BURL ives wrote:
>
>> I now wish to fill in a form containing multiple rows (the number of
>> rows being different each time) and then upload that to the database
>> tables.
>
>Use arrays in the form
>
><input type="text" name="something[]">
>
>then you get the results with
>
>$_REQUEST['something']
>
>you can then loop the array with foreach().
>
>Of course you may want to have more than one "column", in those cases you can
>use a 2D array and of course you can index the cells in the array already in
>the form.
>
>
>There are other ways to do it too, but this is IMHO the simplest way. |
|
| Back to top |
|
 |  |
External

Since: Dec 08, 2008 Posts: 5
|
(Msg. 4) Posted: Tue Dec 09, 2008 10:40 pm
Post subject: Re: how to populate multiple records in a mysql DB Archived from groups: per prev. post (more info?)
|
|
|
OK....after working on this for some time, I have to break down and
ask some questions since I'm not getting anywhere.........
I have found that if I enter the number of rows and then using a FOR
loop to run through the cycles, the web page is refreshed and it no
longer remembers the values for the variables.
Here is an example of the rows that I would like to be able to fill :
-------------------------------
method | column | cost |
-------------------------------
hplc | RP18 | 600 |
hplc | phyl | 300 |
hplc | rp8 | 300 |
gc | carbx | 900 |
tlc | na | 200 |
-------------------------------
as you can see, there could be several entries for the primary field-
METHOD and each primary field can have one or multiple related fields
(such as column and cost). My thought was to enter the number of
primary fields (in this case there are 3 - hplc, gc, tlc). then with
the for loop, I would cycle through the first pass. after entering
the first value, there would be a nested loop asking how many related
fields to the current primary field ( in the above case there would be
3 related fields to the first primary field). Once I completed that
loop I would cycle through the second of 3 passes and in this case
there would be only 1 related field etc etc etc. The trouble is the
system doesn't retain any info once I refresh the page.
Of course I could make the entries one row at a time but I wanted a
more elegant approach. I could also have a datatable in mysql to
retain the number of primary and related fields and try to remember
what stage of the cycle has been completed but I didn't think that was
a great idea either.
So are there any suggestions on how to do this with PHP so that I can
enter these values into a mysql db?
On Mon, 08 Dec 2008 18:00:53 +0100, "J.O. Aho" <user.DeleteThis@example.net>
wrote:
>BURL ives wrote:
>
>> I now wish to fill in a form containing multiple rows (the number of
>> rows being different each time) and then upload that to the database
>> tables.
>
>Use arrays in the form
>
><input type="text" name="something[]">
>
>then you get the results with
>
>$_REQUEST['something']
>
>you can then loop the array with foreach().
>
>Of course you may want to have more than one "column", in those cases you can
>use a 2D array and of course you can index the cells in the array already in
>the form.
>
>
>There are other ways to do it too, but this is IMHO the simplest way. |
|
| Back to top |
|
 |  |
External

Since: May 31, 2006 Posts: 2034
|
(Msg. 5) Posted: Wed Dec 10, 2008 3:04 am
Post subject: Re: how to populate multiple records in a mysql DB Archived from groups: per prev. post (more info?)
|
|
|
BURL ives wrote:
Soon time for me to leave for work, so will be short and all code untested.
> Here is an example of the rows that I would like to be able to fill :
> -------------------------------
> method | column | cost |
> -------------------------------
> hplc | RP18 | 600 |
> hplc | phyl | 300 |
> hplc | rp8 | 300 |
> gc | carbx | 900 |
> tlc | na | 200 |
> -------------------------------
You can create a 2D array in the form
<input type=text name="array[1][m]"><input type=text
name="array[1][cn]"><input type=text name="array[1][ct]">
<input type=text name="array[2][m]"><input type=text
name="array[2][cn]"><input type=text name="array[2][ct]">
<input type=text name="array[3][m]"><input type=text
name="array[3][cn]"><input type=text name="array[3][ct]">
In this example you have three input rows to fill in all the three columns for
the database, adding automatically new rows with javascript is possible, just
keep track of the index value in the last "lines" cell.
$array = $_POST['array'];
$insertvalues = array();
foreach($array => $line) {
//You should validate the input values here and drop a line if one of the
values wrong
$insertvalues[] = "('{$line['c']}','{$line['cn']}',{$line['ct']})";
}
if(count($insertvalues))
$query = "INSERT INTO table(method, column, cost)
VALUES".implode(',',$insertvalues);
then check that the $query is set befor you start to connect to the database
and run the query. Keep in mind that you should check all the incoming values
in the foreach loop, or else there is a risk that the quiery failes due
missing data or someone tris to make a sql injection.
--
//Aho |
|
| Back to top |
|
 |  |
External

Since: Dec 08, 2008 Posts: 5
|
(Msg. 6) Posted: Mon Dec 15, 2008 8:12 am
Post subject: Re: how to populate multiple records in a mysql DB Archived from groups: per prev. post (more info?)
|
|
|
I recogize that this is not a javascript group but since I needed
javascript to create the inputs for PHP to send to the DB, please
allow me the following question:
I managed to create a javascript that with each hit of a button, a new
row is added to the form. However once I added 3 rows and did a VIEW
SOURCE, I noticed that the variables had not changed from the initial
page. Is that suppose to happen? How will PHP know how many records
to send to the DB?
I was playing around with a 4 column table and the function I used is:
function addRow(id){
var tbody = document.getElementById
(id).getElementsByTagName("TBODY")[0];
var row = document.createElement("TR")
var td1 = document.createElement("TD")
td1.appendChild(document.createTextNode("column 1"))
var td2 = document.createElement("TD")
td2.appendChild (document.createTextNode("column 2"))
var td3 = document.createElement("TD")
td3.appendChild(document.createTextNode("column 3"))
var td4 = document.createElement("TD")
td4.appendChild (document.createTextNode("column 4"))
row.appendChild(td1);
row.appendChild(td2);
row.appendChild(td3);
row.appendChild(td4);
tbody.appendChild(row);
}
and the form is:
<a href="javascript:addRow('myTable')">PRESS ONCE FOR EACH ROW
NEEDED</a>
<table id="myTable" cellspacing="0" border="1">
<th>MILESTONE</th><th>ACTIVITY</th><th>COST</th><th>STATUS</th>
<tr>
<td></td><td></td><td></td><td></td>
</tr>
</table>
ANY SUGGESTIONS?
On Wed, 10 Dec 2008 07:29:26 +0100, "J.O. Aho" <user.RemoveThis@example.net>
wrote:
>BURL ives wrote:
>
>Soon time for me to leave for work, so will be short and all code untested.
>
>> Here is an example of the rows that I would like to be able to fill :
>> -------------------------------
>> method | column | cost |
>> -------------------------------
>> hplc | RP18 | 600 |
>> hplc | phyl | 300 |
>> hplc | rp8 | 300 |
>> gc | carbx | 900 |
>> tlc | na | 200 |
>> -------------------------------
>
>You can create a 2D array in the form
>
><input type=text name="array[1][m]"><input type=text
>name="array[1][cn]"><input type=text name="array[1][ct]">
>
><input type=text name="array[2][m]"><input type=text
>name="array[2][cn]"><input type=text name="array[2][ct]">
>
><input type=text name="array[3][m]"><input type=text
>name="array[3][cn]"><input type=text name="array[3][ct]">
>
>
>In this example you have three input rows to fill in all the three columns for
>the database, adding automatically new rows with javascript is possible, just
>keep track of the index value in the last "lines" cell.
>
>$array = $_POST['array'];
>$insertvalues = array();
>
>foreach($array => $line) {
> //You should validate the input values here and drop a line if one of the
>values wrong
> $insertvalues[] = "('{$line['c']}','{$line['cn']}',{$line['ct']})";
>}
>
>if(count($insertvalues))
>$query = "INSERT INTO table(method, column, cost)
>VALUES".implode(',',$insertvalues);
>
>then check that the $query is set befor you start to connect to the database
>and run the query. Keep in mind that you should check all the incoming values
>in the foreach loop, or else there is a risk that the quiery failes due
>missing data or someone tris to make a sql injection. |
|
| Back to top |
|
 |  |
External

Since: Apr 24, 2007 Posts: 49
|
(Msg. 7) Posted: Mon Dec 15, 2008 8:17 am
Post subject: Re: how to populate multiple records in a mysql DB Archived from groups: per prev. post (more info?)
|
|
|
On 15 Dec, 13:12, BURL ives <e_ro... DeleteThis @hotmail.com> wrote:
Please do not top post.
> I managed to create a javascript that with each hit of a button, a new
> row is added to the form. However once I added 3 rows and did a VIEW
> SOURCE, I noticed that the variables had not changed from the initial
> page. Is that suppose to happen?
The answer is in your question! You did a "View Source"! Here is the
defintion of "source":
1. any thing or place from which something comes, arises, or is
obtained; origin: Which foods are sources of calcium?
So what you see when you view source is what you started with.
> How will PHP know how many records
> to send to the DB?
You say: "and the form is:..." but you don't show a form, only a
table. Since there is no form, nothing will get sent to php. |
|
| Back to top |
|
 |  |
External

Since: Apr 24, 2007 Posts: 49
|
(Msg. 8) Posted: Mon Dec 15, 2008 11:13 am
Post subject: Re: how to populate multiple records in a mysql DB Archived from groups: per prev. post (more info?)
|
|
|
On 15 Dec, 18:23, BURL ives <e_ro... RemoveThis @hotmail.com> wrote:
> On Mon, 15 Dec 2008 08:17:37 -0800 (PST), Captain Paralytic
>
>
>
> <paul_laut... RemoveThis @yahoo.com> wrote:
> >On 15 Dec, 13:12, BURL ives <e_ro... RemoveThis @hotmail.com> wrote:
>
> >Please do not top post.
>
> >> I managed to create a javascript that with each hit of a button, a new
> >> row is added to the form. However once I added 3 rows and did a VIEW
> >> SOURCE, I noticed that the variables had not changed from the initial
> >> page. Is that suppose to happen?
> >The answer is in your question! You did a "View Source"! Here is the
> >defintion of "source":
>
> >1. any thing or place from which something comes, arises, or is
> >obtained; origin: Which foods are sources of calcium?
>
> >So what you see when you view source is what you started with.
>
> >> How will PHP know how many records
> >> to send to the DB?
> >You say: "and the form is:..." but you don't show a form, only a
> >table. Since there is no form, nothing will get sent to php.
>
> I guess I gotta take the tongue-lashing or else!....
>
> help me understand somethings.......
>
> 1) is there actually an ettiquete that says you don't reply on TOP of
> the prior post???? My newsgroup reader automatically puts me there
> and I didn't think it was a faux pas to keep the current text on top.
> So again, if there is some ettiquete that says one should post on the
> bottom, I would be happy to oblige...but I would also like to know
> where the rules are....there may be others that I am unintentially
> violating
http://home.in.tum.de/~jain/software/oe-quotefix/
OE-Quotefix
you can install
Outlook Express
If you are using
top!.
bottom to
is written from
conversation which
to follow a
It is very difficult
>
> 2) thanks for pointing out that view source does not show all the
> added rows. The related question was how can I capture the number of
> added rows so that PHP can send the right rows to the DB?
Aho already told you this, but I think it is easier to just code
name="inputname[]" on each input and process from there. There are
lots of tutorials around showing how this works. A google search for
php post input array
gives this amongst others http://jetlogs.org/2007/07/19/passing-input-arrays-in-php/
>
> 3) I am sorry about the form....my bad....should I have said the CODE
> FOR THE FORM is below instead of saying the FORM IS BELOW?
There is no form code at all, just markup for a HTML table.
>
> thanks for the reply
You're welcome |
|
| Back to top |
|
 |  |
External

Since: Apr 24, 2007 Posts: 49
|
(Msg. 9) Posted: Mon Dec 15, 2008 11:15 am
Post subject: Re: how to populate multiple records in a mysql DB Archived from groups: per prev. post (more info?)
|
|
|
On 15 Dec, 18:23, BURL ives <e_ro... DeleteThis @hotmail.com> wrote:
> On Mon, 15 Dec 2008 08:17:37 -0800 (PST), Captain Paralytic
>
>
>
> <paul_laut... DeleteThis @yahoo.com> wrote:
> >On 15 Dec, 13:12, BURL ives <e_ro... DeleteThis @hotmail.com> wrote:
>
> >Please do not top post.
>
> I guess I gotta take the tongue-lashing or else!....
I very nicely (I said please) asked you not to top post. Is that
really a tongue lashing? |
|
| Back to top |
|
 |  |
External

Since: Dec 08, 2008 Posts: 5
|
(Msg. 10) Posted: Mon Dec 15, 2008 1:23 pm
Post subject: Re: how to populate multiple records in a mysql DB Archived from groups: per prev. post (more info?)
|
|
|
On Mon, 15 Dec 2008 08:17:37 -0800 (PST), Captain Paralytic
<paul_lautman RemoveThis @yahoo.com> wrote:
>On 15 Dec, 13:12, BURL ives <e_ro... RemoveThis @hotmail.com> wrote:
>
>Please do not top post.
>
>> I managed to create a javascript that with each hit of a button, a new
>> row is added to the form. However once I added 3 rows and did a VIEW
>> SOURCE, I noticed that the variables had not changed from the initial
>> page. Is that suppose to happen?
>The answer is in your question! You did a "View Source"! Here is the
>defintion of "source":
>
>1. any thing or place from which something comes, arises, or is
>obtained; origin: Which foods are sources of calcium?
>
>So what you see when you view source is what you started with.
>
>> How will PHP know how many records
>> to send to the DB?
>You say: "and the form is:..." but you don't show a form, only a
>table. Since there is no form, nothing will get sent to php.
I guess I gotta take the tongue-lashing or else!....
help me understand somethings.......
1) is there actually an ettiquete that says you don't reply on TOP of
the prior post???? My newsgroup reader automatically puts me there
and I didn't think it was a faux pas to keep the current text on top.
So again, if there is some ettiquete that says one should post on the
bottom, I would be happy to oblige...but I would also like to know
where the rules are....there may be others that I am unintentially
violating.
2) thanks for pointing out that view source does not show all the
added rows. The related question was how can I capture the number of
added rows so that PHP can send the right rows to the DB?
3) I am sorry about the form....my bad....should I have said the CODE
FOR THE FORM is below instead of saying the FORM IS BELOW?
thanks for the reply |
|
| Back to top |
|
 |  |
External

Since: May 31, 2006 Posts: 2034
|
(Msg. 11) Posted: Mon Dec 15, 2008 9:40 pm
Post subject: Re: how to populate multiple records in a mysql DB Archived from groups: per prev. post (more info?)
|
|
|
BURL ives wrote:
> 1) is there actually an ettiquete that says you don't reply on TOP of
> the prior post???? My newsgroup reader automatically puts me there
> and I didn't think it was a faux pas to keep the current text on top.
> So again, if there is some ettiquete that says one should post on the
> bottom, I would be happy to oblige...but I would also like to know
> where the rules are..
Yes, there is one see RFC 1855 and RFC 2822 for more information or visit
a site like http://www.albion.com/netiquette/
There is a company that makes software who don't like to follow rules and
standards set up by others and hardly manage to follow the rules and
none-standards that they themselves create.
> 2) thanks for pointing out that view source does not show all the
> added rows. The related question was how can I capture the number of
> added rows so that PHP can send the right rows to the DB?
Use Firefox together with FireBug and you can see things more clearly, for
example you can even look at code that are generated by javascript.
--
//Aho |
|
| Back to top |
|
 |  |
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|
|
|
|