I have a table with an unknown number of rows. I’m trying to save edits to a checkbox field, select field and text field.
Here’s a sample of what a table row looks like (generalized):
<tr onMouseover="this.bgColor='#EEEEEE'"onMouseout="this.bgColor='#FFFFFF'">
<input type="hidden" name="batchupdate[][item1]" value="118">
<td><a target="blank" href="www.link.com">Link</a></td>
<td>Name</td>
<td><input value="" type="text" name="batchupdate[][item2]" size=35></td>
<td>
<select name="batchupdate[][item3]">
<optgroup label="Group 1">
<option >1</option>
<option >2</option>
<option >3</option>
<option >4</option>
<option >5</option>
</optgroup>
<optgroup label="Group 2">
<option >6</option>
<option >7</option>
<option >8</option>
<option >9</option>
</optgroup>
</select>
</td>
<td><input type="checkbox" value="FALSE" name="batchupdate[][item4]"></td>
</tr>
The “id” (item1) is the auto incrementing field in my postgresql database.
Here’s the php:
if(isset($_POST['savebutton'])) {
if(isset($_POST['bachtupdate']) || is_array($_POST['batchupdate']))
{
foreach($_POST['batchupdate'] as $i)
{
$item1 = $i['item1'];
$item2 = $i['item2'];
$item2 = pg_escape_string($item2);
$item3 = $i['item3'];
if(isset($i['item4'])){
$item4 = "TRUE";
} else {
$item4 = "FALSE";
}
$query = "UPDATE schema.table SET field2='$item2', field3='$item3', field4='$item4' WHERE id='$item1'";
$result = pg_query($database, $query);
if ($result) {
// success
} else {
die("Error: " .pg_last_error());
}
There’s some other logic after this, but the query part is where it fails. I get an error that basically item3 and the id (item1) are empty, so it cannot find the row to update. Items2 and 4 work fine and I can see what they contain in the error message provided by postgres.
I found this solution online and adapted it to my own use. I’m just confused as to why some of the fields work and others don’t. Ideas?
EDIT:
This is what the output looks like with echoes (inside the foreach loop). I commented out the postgres query. Notice that it does it twice. I also noticed that the checkbox isn’t returning true. However, all the information is there…,
118 (id),
Text box 1,
Option 2,
FALSE (checkbox)
119 (id),
Text box 2,
Option 3
118 (id),
Text box 1,
Option 2,
FALSE (checkbox)
119 (id),
Text box 2,
Option 3
EDIT:
Next I’m going to try one of the following:
-
Use php to create name attributes that are unique (
item11,item12,item13, etc), and increment aforloop that counts how many rows in the table there are, change the variables and perform the query. This seems way too messy, but I can’t think of anything else…, -
Instead of doing a
foreach, do aforloop as seen in other examples I’ve found. Not sure what it will change, but it’s worth a shot I guess.
I’ll report back what I found.
How generalized is the HTML you show?
I mean, when you write
You do place something in the first key of batchupdate, do you not?
Because otherwise the POST will send out, say,
which will create TWO DIFFERENT ROWS in batchupdate[], one with item1, the other with only item3, since [] is autoincrement syntax.
Each “group” of rows should have a different ID as the first index:
This will result in one entry of batchupdate with item1,2 and 3 defined and equal to 118, 119, and 120, which seems what you are looking for.