html - PHP drop down menu script doesn't update database -


this question has answer here:

the following script supposed update database, environment column selected value drop down menu. right now, last host getting value of 0. doing wrong? seems not able track hosts , value selected.

while($row = mysql_fetch_array($result)){  $host = $row['host']; $environment = $row['environment'];  echo "<tr><td>" . $host . "</td>       <td><select name='id[".$host."]'><option value='null'>select any</option>                               <option value='dev/qa/test'>dev/qa/test</option>                               <option value='prod/stage'>prod/stage</option>                               </select></td>                               <td>" . $environment . "</td></tr>"; }   echo "</tbody></table><input type='submit' value='submit'></form>";  if (gettype($_post['id'])=="array") {      foreach($_post['id'] $host => $val){              $id_c = $val; if ($val != 'null') {             $query1 = "update hosts set environment = '$val' host='$host'";              $result1 = mysql_query($query1);              if($result1 === false) {              die(mysql_error());          }       echo "environment host " .$host. " updated. <br>";  }}} 

updated working script.

your code wrong. i'll try explain doing , how should have code it.

first mistake:

while($row = mysql_fetch_array($result)) {     echo '<select name="id[]">';     [...] } 

you creating 1 select host (that's ok). once it's created, can't select goes host. way can tell order appears, that's not reliable.
i'd suggest identify select host id (or identifier have).
this:

while($row = mysql_fetch_array($result)) {     echo '<select name="id['.$row['host_id'].']">';     [...] } 

second mistake:

while($row = mysql_fetch_array($result)) {     $host = $row['host']; } // here, $host contains last row  foreach ($_post['id'] $val) {     // update $host $val } 

what's happening here: updating last host every select value passed via form.

here should do:

while($row = mysql_fetch_array($result)) {     echo '<select name="id['.$row['host_id'].']">';     [...] }  foreach ($_post['id'] $host_id => $val) {      // update hosts set environment = $val host_id = $host_id } 

that way updating each host each matching value in $_post['id'].
can print_r($_post) understand structure.


Comments

Popular posts from this blog

html - Firefox flex bug applied to buttons? -

html - Missing border-right in select on Firefox -

html - Why is a table with width of 75% wider than three tables which total 99%? -