PDA

View Full Version : Interting multiple rows and columns via PHP and arrays


keith1995
12-03-2005, 08:33 PM
I have a form that has 12 different text fields in it. To insert those text fields would be a breeze, the problem is that there need to be 10 rows of those same 12 fields.

The rows of the table in my form with these text fields looks like this (only showing 2 rows here as there is no need to show all 10):
<tr>
<td>&nbsp;</td>
<td><input name='board[][number]' type='text' size='12'></td>
<td>&nbsp;</td>
<td><input name='board[][read_rl]' type='text' size='5'></td>
<td>&nbsp;</td>
<td><input name='board[][copy_description]' type='text' size='15'></td>
<td>&nbsp;</td>
<td><input name='board[][billboard_size_ft_a]' type='text' size='3'></td>
<td>&nbsp;</td>
<td><input name='board[][billboard_size_in_a]' type='text' size='3'></td>
<td>&nbsp;</td>
<td><input name='board[][billboard_size_ft_b]' type='text' size='3'></td>
<td>&nbsp;</td>
<td><input name='board[][billboard_size_in_b]' type='text' size='3'></td>
<td>&nbsp;</td>
<td><input name='board[][vinyl_type]' type='text' size='10'></td>
<td>&nbsp;</td>
<td><input name='board[][extensions_ft_a]' type='text' size='3'></td>
<td>&nbsp;</td>
<td><input name='board[][extensions_in_a]' type='text' size='3'></td>
<td>&nbsp;</td>
<td><input name='board[][extensions_ft_b]' type='text' size='3'></td>
<td>&nbsp;</td>
<td><input name='board[][extensions_in_b]' type='text' size='3'></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><input name='board[][number]' type='text' size='12'></td>
<td>&nbsp;</td>
<td><input name='board[][read_rl]' type='text' size='5'></td>
<td>&nbsp;</td>
<td><input name='board[][copy_description]' type='text' size='15'></td>
<td>&nbsp;</td>
<td><input name='board[][billboard_size_ft_a]' type='text' size='3'></td>
<td>&nbsp;</td>
<td><input name='board[][billboard_size_in_a]' type='text' size='3'></td>
<td>&nbsp;</td>
<td><input name='board[][billboard_size_ft_b]' type='text' size='3'></td>
<td>&nbsp;</td>
<td><input name='board[][billboard_size_in_b]' type='text' size='3'></td>
<td>&nbsp;</td>
<td><input name='board[][vinyl_type]' type='text' size='10'></td>
<td>&nbsp;</td>
<td><input name='board[][extensions_ft_a]' type='text' size='3'></td>
<td>&nbsp;</td>
<td><input name='board[][extensions_in_a]' type='text' size='3'></td>
<td>&nbsp;</td>
<td><input name='board[][extensions_ft_b]' type='text' size='3'></td>
<td>&nbsp;</td>
<td><input name='board[][extensions_in_b]' type='text' size='3'></td>
</tr>

The form submits to another page where the form results are echo'd to the page and the values are inserted into a MySQL database table. The insert statement looks like this:
foreach ($_POST['board'] as $key => $value) {

$sql = "INSERT INTO client_boards VALUES (
'".$value['number']."',
'".$value['read_rl']."',
'".$value['copy_description']."',
'".$value['billboard_size_ft_a']."'
'".$value['billboard_size_in_a']."'
'".$value['billboard_size_ft_b']."'
'".$value['billboard_size_in_b']."'
'".$value['vinyl_type']."'
'".$value['extensions_ft_a']."'
'".$value['extensions_in_a']."'
'".$value['extensions_ft_b']."'
'".$value['extensions_in_b'].")";

$result = mysql_query($sql);
}

The problem is that nothing is being inserted in the database, whether I type text into one row of the text fields or multiple rows of the fields.

Can anyone see where my problem is?

Velox Letum
12-03-2005, 08:48 PM
Try defining the fields to insert into: INSERT INTO table (field,field,field) VALUES (value,value,value)

Also, change the mysql_query to: $result = mysql_query($sql) or die(mysql_error()); which should help you pinpoint the problem.

keith1995
12-03-2005, 09:09 PM
Velox,

I just realized that I was missing comas to seperate the values in the insert statement. I've added them and have made the updates that you detailed in your post, so the code for the insert statement now looks like this:

foreach ($_POST['board'] as $key => $value) {

$sql = "INSERT INTO client_boards (board_number,read_rl,copy_description,billboard_size_ft_a,billboard_size_in_a,billboard_size_ft_b,b illboard_size_in_b,vinyl_type,extensions_ft_a,extensions_in_a,extensions_ft_b,extensions_in_b) VALUES (
'".$value['number']."',
'".$value['read_rl']."',
'".$value['copy_description']."',
'".$value['billboard_size_ft_a']."',
'".$value['billboard_size_in_a']."',
'".$value['billboard_size_ft_b']."',
'".$value['billboard_size_in_b']."',
'".$value['vinyl_type']."',
'".$value['extensions_ft_a']."',
'".$value['extensions_in_a']."',
'".$value['extensions_ft_b']."',
'".$value['extensions_in_b']."')";
$result = mysql_query($sql) or die(mysql_error());
}

I tested this and tried it first without any text in any of the textfields and it inserted 120 rows (I'm guessing that is from the 12 textfields x 10 rows). If nothing is input into the fields, nothing should be inserted. Same thing goes if only 1 row has content, only 1 row should be inserted and if 9 rows have input, 9 rows should be inserted.

Any ideas where the problem lies?

Velox Letum
12-04-2005, 04:07 AM
I think the problem is in the naming of your form elements...they're making an array for each one. Try making them like board_number[] and board_readrl[].

keith1995
12-04-2005, 05:56 AM
Velox,

If I make each 10-some of the text fields into their own array, how would the insert statement look? I can handle it when it comes to just one array but I'll have 10 arrays here:

The inputs would look like this:
<tr>
<td>&nbsp;</td>
<td><input name='board_number[]' type='text' size='12'></td>
<td>&nbsp;</td>
<td><input name='board_read_rl[]' type='text' size='5'></td>
<td>&nbsp;</td>
<td><input name='board_copy_description[]' type='text' size='15'></td>
<td>&nbsp;</td>
<td><input name='board_billboard_size_ft_a[]' type='text' size='3'></td>
<td>&nbsp;</td>
<td><input name='board_billboard_size_in_a[]' type='text' size='3'></td>
<td>&nbsp;</td>
<td><input name='board_billboard_size_ft_b[]' type='text' size='3'></td>
<td>&nbsp;</td>
<td><input name='board_billboard_size_in_b[]' type='text' size='3'></td>
<td>&nbsp;</td>
<td><input name='board_vinyl_type[]' type='text' size='10'></td>
<td>&nbsp;</td>
<td><input name='board_extensions_ft_a[]' type='text' size='3'></td>
<td>&nbsp;</td>
<td><input name='board_extensions_in_a[]' type='text' size='3'></td>
<td>&nbsp;</td>
<td><input name='board_extensions_ft_b[]' type='text' size='3'></td>
<td>&nbsp;</td>
<td><input name='board_extensions_in_b[]' type='text' size='3'></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><input name='board_number[]' type='text' size='12'></td>
<td>&nbsp;</td>
<td><input name='board_read_rl[]' type='text' size='5'></td>
<td>&nbsp;</td>
<td><input name='board_copy_description[]' type='text' size='15'></td>
<td>&nbsp;</td>
<td><input name='board_billboard_size_ft_a[]' type='text' size='3'></td>
<td>&nbsp;</td>
<td><input name='board_billboard_size_in_a[]' type='text' size='3'></td>
<td>&nbsp;</td>
<td><input name='board_billboard_size_ft_b[]' type='text' size='3'></td>
<td>&nbsp;</td>
<td><input name='board_billboard_size_in_b[]' type='text' size='3'></td>
<td>&nbsp;</td>
<td><input name='board_vinyl_type[]' type='text' size='10'></td>
<td>&nbsp;</td>
<td><input name='board_extensions_ft_a[]' type='text' size='3'></td>
<td>&nbsp;</td>
<td><input name='board_extensions_in_a[]' type='text' size='3'></td>
<td>&nbsp;</td>
<td><input name='board_extensions_ft_b[]' type='text' size='3'></td>
<td>&nbsp;</td>
<td><input name='board_extensions_in_b[]' type='text' size='3'></td>
</tr>

The insert statement used to look like this:
foreach ($_POST['board'] as $key => $value) {

$sql = "INSERT INTO client_boards VALUES (
'".$value['number']."',
'".$value['read_rl']."',
'".$value['copy_description']."',
'".$value['billboard_size_ft_a']."'
'".$value['billboard_size_in_a']."'
'".$value['billboard_size_ft_b']."'
'".$value['billboard_size_in_b']."'
'".$value['vinyl_type']."'
'".$value['extensions_ft_a']."'
'".$value['extensions_in_a']."'
'".$value['extensions_ft_b']."'
'".$value['extensions_in_b'].")";

$result = mysql_query($sql);
}


Any ideas what it should look like now? I tried it as above and got the following error:

Warning: Invalid argument supplied for foreach() in index.php on line 265

GJay
12-04-2005, 04:20 PM
<td>&nbsp;</td>
<td><input name='board[][number]' type='text' size='12'></td>
<td>&nbsp;</td>
<td><input name='board[][read_rl]' type='text' size='5'></td>
<td>&nbsp;</td>
<td><input name='board[][copy_description]' type='text' size='15'></td>
<td>&nbsp;</td>


will put each field in an array of it's own.
You need to have numbers between the first set of []s. so


<td>&nbsp;</td>
<td><input name='board[0][number]' type='text' size='12'></td>
<td>&nbsp;</td>
<td><input name='board[0][read_rl]' type='text' size='5'></td>
<td>&nbsp;</td>
<td><input name='board[0][copy_description]' type='text' size='15'></td>
<td>&nbsp;</td>
...
...
<td>&nbsp;</td>
<td><input name='board[1][number]' type='text' size='12'></td>
<td>&nbsp;</td>
<td><input name='board[1][read_rl]' type='text' size='5'></td>
<td>&nbsp;</td>
<td><input name='board[1][copy_description]' type='text' size='15'></td>
<td>&nbsp;</td>
etc.


Something similar to the above should work

keith1995
12-04-2005, 05:24 PM
GJay,

Okay, now I'm atleast getting something inserted into the DB, however is is inserting 10 rows into the table, even if only 1 row of text fields has text input into them (the other rows are being insert with only the primary id key).

How do I edit the code so that it only inserts a row if one of the textfields in that row have text?

Any help on this is much appreciated. My new code is below:

Table structure to create the rows:
for ($i=0; $i<10; $i++) {
echo "
<tr>
<td>&nbsp;</td>
<td><input name='board[$i][number]' type='text' size='12'></td>
<td>&nbsp;</td>
<td><input name='board[$i][read_rl]' type='text' size='5'></td>
<td>&nbsp;</td>
<td><input name='board[$i][copy_description]' type='text' size='15'></td>
<td>&nbsp;</td>
<td><input name='board[$i][billboard_size_ft_a]' type='text' size='3'></td>
<td>&nbsp;</td>
<td><input name='board[$i][billboard_size_in_a]' type='text' size='3'></td>
<td>&nbsp;</td>
<td><input name='board[$i][billboard_size_ft_b]' type='text' size='3'></td>
<td>&nbsp;</td>
<td><input name='board[$i][billboard_size_in_b]' type='text' size='3'></td>
<td>&nbsp;</td>
<td><input name='board[$i][vinyl_type]' type='text' size='10'></td>
<td>&nbsp;</td>
<td><input name='board[$i][extensions_ft_a]' type='text' size='3'></td>
<td>&nbsp;</td>
<td><input name='board[$i][extensions_in_a]' type='text' size='3'></td>
<td>&nbsp;</td>
<td><input name='board[$i][extensions_ft_b]' type='text' size='3'></td>
<td>&nbsp;</td>
<td><input name='board[$i][extensions_in_b]' type='text' size='3'></td>
</tr>"; }


Insert statement code:
foreach ($_POST['board'] as $key => $value) {

$sql = "INSERT INTO client_boards (board_number, read_rl, copy_description, billboard_size_ft_a, billboard_size_in_a, billboard_size_ft_b, billboard_size_in_b, vinyl_type, extensions_ft_a, extensions_in_a, extensions_ft_b, extensions_in_b)
VALUES ('".$value['number']."', '".$value['read_rl']."', '".$value['copy_description']."', '".$value['billboard_size_ft_a']."', '".$value['billboard_size_in_a']."', '".$value['billboard_size_ft_b']."', '".$value['billboard_size_in_b']."', '".$value['vinyl_type']."', '".$value['extensions_ft_a']."', '".$value['extensions_in_a']."', '".$value['extensions_ft_b']."', '".$value['extensions_in_b']."')";
$result = mysql_query($sql) or die(mysql_error());
}

GJay
12-04-2005, 08:23 PM
Put an if statement in the lopp that contains the insert to check for the presence of the fields:

foreach ($_POST['board'] as $key => $value) {
if(isset($value['number']&&$value['number']!='') {
//do query
}
}


Will only insert rows if the 'number' field has a value. If other fields are compulsory, add conditions...