php - Saving multiple rows to database -


i have table unknown number of rows. i'm trying save edits checkbox field, select field , text field.

here's sample of table row looks (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) auto incrementing field in postgresql database.

here's php:

if(isset($_post['savebutton'])) {     if(isset($_post['bachtupdate']) || is_array($_post['batchupdate']))     {         foreach($_post['batchupdate'] $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' id='$item1'";          $result = pg_query($database, $query);          if ($result) {             // success         } else {             die("error: " .pg_last_error());         } 

there's other logic after this, query part fails. error item3 , id (item1) empty, cannot find row update. items2 , 4 work fine , can see contain in error message provided postgres.

i found solution online , adapted own use. i'm confused why of fields work , others don't. ideas?

edit:

this output looks echoes (inside foreach loop). commented out postgres query. notice twice. noticed checkbox isn't returning true. however, information 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 try 1 of following:

  1. use php create name attributes unique (item11, item12, item13, etc), , increment for loop counts how many rows in table there are, change variables , perform query. seems way messy, can't think of else...,

  2. instead of doing foreach, for loop seen in other examples i've found. not sure change, it's worth shot guess.

i'll report found.

how generalized html show?

i mean, when write

 <input type="hidden" name="batchupdate[][item1]" value="118"> 

you place in first key of batchupdate, not?

because otherwise post send out, say,

batchupdate[][item1]=118&batchupdate[][item3]=124... 

which create 2 different rows in batchupdate[], 1 item1, other item3, since [] autoincrement syntax.

each "group" of rows should have different id first index:

 <input type="hidden" name="batchupdate[row1][item1]" value="118">  <input type="hidden" name="batchupdate[row1][item2]" value="119">  <input type="hidden" name="batchupdate[row1][item3]" value="120">  ...  <input type="hidden" name="batchupdate[row2][item1]" value="123"> 

this result in 1 entry of batchupdate item1,2 , 3 defined , equal 118, 119, , 120, seems looking for.


Comments

Popular posts from this blog

c# - SVN Error : "svnadmin: E205000: Too many arguments" -

c# - Copy ObservableCollection to another ObservableCollection -

All overlapping substrings matching a java regex -