i have a form, with text fields storing data in an array. each will be a row of data in the db.
<input name="value[]" type="text">
<input name="id[]" type="hidden" value="<?php print $id; ?>">
i have two questions.
how do i get these value into the db? i've been accessing each element by it's number in a while loop. ex: insert $value[1], id[1], insert $value[2]...
i'm sure this isn't the best way to do this.
also, i'm not requiring that all the fields in the form have data and am getting zero values. how do i make it so these values are never stored in the array? my idea was to only insert, into the db, the values that were not zero. is there a more efficient way?
form values into an array
Moderator: General Moderators
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
To avoid storing empty values use
Code: Select all
if (!empty($var)) {
//Do whatever
}This makes no sense, where's the value attribute? As far as storing data from multiple fields goes then yes you can use a loop, that's efficient enough. Everything should be in $_POST or $_GET depending upon how you send the data however, so you should be accessing it via this method.<input name="value[]" type="text">
- bluesman333
- Forum Commoner
- Posts: 52
- Joined: Wed Dec 31, 2003 9:47 am
but is there a better way? here's what i've been doing.As far as storing data from multiple fields goes then yes you can use a loop, that's efficient enough.
Code: Select all
$x = 0;
$num = count(the number of input fields in the form);
while ($num > $x) {
INSERT INTO TABLE VALUES ($id[$x], $value[$x])
$x++;
}
but, this will insert the values from every field in the form, even the ones with no value input.
are you suggesting this?
while ($num > $x) {
if (!empty($value[$x]) {
INSERT INTO TABLE VALUES ($id[$x], $value[$x])
$x++;
}
}feyd | Please use
Code: Select all
andCode: Select all
tags where approriate when posting code. Read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact: