0

I have the following to code to update the database based on checkbox selection.

while($row = mysql_fetch_array($result)){
$rebootpol = $row['reboot policy'];
$host = $row['host'];
$environment = $row['environment'];
$patchpol = $row['patch policy'];


echo "<tr><td>" . $host . "</td><td>" . $environment . "</td><td><input type='checkbox' name='envdev[]' value='" . $host . "'/></td><td><input type='checkbox' name='envprd[]' value='" . $host . "'/></td><td>" . $reboot . "</td><td><input type='checkbox' name='id[]' value='" . $host . "'/></td></tr>";
}

echo "</tbody></table><input type='submit' value='submit'></form>";

if(gettype($_POST['id'])=="array"){
    foreach($_POST['id'] as $val){
     $id_c = $val;
     $query2 = "UPDATE hosts SET reboot = 'Yes' where host='".$id_c."'";
     $result2 = mysql_query($query2);
     if($result2 === false) {
        die(mysql_error());
     }
     echo "Reboot updated for Host " .$id_c. " is updated. <br>";

How can I port the same for dropdown code like below.

while($row = mysql_fetch_array($result)){
$rebootpol = $row['reboot policy'];
$host = $row['host'];
$environment = $row['environment'];
$patchpol = $row['patch policy'];


echo "<tr><td>" . $host . "</td>

<td>
            <input type='hidden' name='envdev[]' value='" . $host . "'>
            <select name='select'><option value='0' selected='selected'>Select any</option>
            <option value='DEV/QA/TEST'>DEV/QA/TEST</option>
            <option value='PROD/STAGE'>PROD/STAGE</option>
            </select>
            </td><td>" . $environment . "</td>

How can I keep track of what was selected and update the database? For the checkbox I was using the Value field but that is used in dropdowns.

Rob G
  • 592
  • 4
  • 18
Kevin M
  • 45
  • 1
  • 8
  • [Your script is at risk for SQL Injection Attacks.](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) – Jay Blanchard Aug 18 '15 at 20:58
  • If you can, you should [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) [statements](http://php.net/manual/en/pdo.prepared-statements.php) instead, and consider using PDO, [it's really not hard](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Aug 18 '15 at 20:58
  • Okay. Will do. Thank you. – Kevin M Aug 18 '15 at 20:59
  • You have to add `selected` parameter to right `` – Skamielina Aug 18 '15 at 21:00
  • If I add selected, then that option comes up as selected in the browser. Right now, I have "Select Any" as the default selected option. – Kevin M Aug 18 '15 at 21:03

0 Answers0