0

I want to update my database by using checkbox. But it seems like the code doesn't loop in a foreach. Where did I go wrong ? Is there have anything wrong with my foreach ?

<?php
  foreach($_POST['listMenu'] as $checkBox1){
    $strUpdateData = "
      update tblMenuAkses
      set MenuAkses = 'N'
      where MenuAksesID = $checkBox1
    ";

    $rsUpdateData = odbc_exec($dbconnVOT,$strUpdateData);

    if($rsUpdateData){
      echo "Success";
    } else {    
      echo "ERROR <br>";
      echo odbc_errormsg($dbconnVOT);
    }
  }
  //It loops here
  var_dump($_POST['listMenu']);
?>

<form method="post" action="skrinMenu.php" enctype="multipart/form-data">
  <table align= "center" border="1" cellspacing="0" cellpadding="2">
  <?php
    $strgettable="select * from tblMenuAkses";

    $varRecCount=0;
    $rs=odbc_exec($dbconnVOT, $strgettable);
    if($rs){
      while (odbc_fetch_row($rs)) {// loop a table from database and checkbox too
        $varRecCount++; 
        echo '<tr><td>';
        echo '<center> &nbsp;'.$varRecCount.'</center>';
        echo '</td>';
        echo '<td>';
        echo '<center> &nbsp;'.odbc_result($rs,"UsrGrpNama").'</center>';
        echo '</td>';
        echo '<td>';
        echo '<center> &nbsp;'.odbc_result($rs,"MenuNama").'</center>';
        echo '</td>';
        echo '<td>';
        echo '<center> &nbsp;'.odbc_result($rs,"MenuAkses").'</center>';
        echo '</td>';
        echo '<td>';
        echo '<center> &nbsp; <input type="checkbox" name="listMenu[]" id="listMenu" value='.odbc_result($rs,"MenuAksesID").'> </center>';
        echo '</td>';
        echo '</td></tr>';          
      }
    }
  ?>            
   </table>
   <input type="submit" name="btnMenu" id="btnMenu" value = "Kemaskini">
</form>

The result of the var_dump($_POST['listMenu']) : NULL

Diyana
  • 9
  • 3
  • 1
    If `` is meant to execute your query, it's outside of your form and should be a submit type, not a button type. – Funk Forty Niner Jan 12 '17 at 02:05
  • While formatting your code, I found a syntax error in your query, the extra `)`?? – Xorifelse Jan 12 '17 at 02:06
  • @Fred-ii- I try to change it into ''submit', but it didn't do anything. – Diyana Jan 12 '17 at 02:39
  • @NurDiyana That's simply because `$_POST['listMenu']` is null, and `foreach(null as $value)` must give an error and thus your query never ran but would have caused an error when you fixed the current error, which is`` is **outside** the form so no value is ever posted. – Xorifelse Jan 12 '17 at 02:49
  • @Xorifelse I've already changed the `` inside the form in question and my code. But it doesn't do a thing. Yes, the `var_dump` output is now gone, but it doesn't update my database either. – Diyana Jan 12 '17 at 03:09
  • The only reason I can find for $_POST['listMenu'] to be null is if you're not clicking any checkboxes when you're submitting your request.... please tell me you're clicking the checkboxes during the test? – fie Jan 12 '17 at 03:49
  • @fie of course I did clicking it.... after all, what is the purpose of testing when I'm not clicking it right ? – Diyana Jan 12 '17 at 04:00
  • Would you mind pasting the full HTML source of the output after submitting the form with boxes checked. (pastebin or something) and instead of doing `var_dump($_POST['listMenu'])` do `print_r($_POST);` at the top right after ` – fie Jan 12 '17 at 04:21
  • Also, you have an extra `echo ''; echo ''; ` – fie Jan 12 '17 at 04:22
  • Also you're not quoting values on the checkboxes `value='.odbc_result($rs,"MenuAksesID").'` -- it shouldn't matter as long as the IDs are just numbers but just thought I'd point it out. – fie Jan 12 '17 at 04:24
  • @fie thanks for your concern sir. I've changed and edit my code as you said. And I've already figure it out the cause of the error. Thank you so much – Diyana Jan 13 '17 at 03:13
  • @Diyana What was the issue? – fie Jan 14 '17 at 04:13
  • @fie each of the checkbox doesn't have an ID. Thats why it doesn't loop in the foreach. So I've edited my SQL to fetch an ID for each of the checkbox and it went well . – Diyana Jan 17 '17 at 03:52

0 Answers0