0

how to update multiple update in database using checkbox with php mysql

 <form  action="enableprofile1.php" method="post">

    //user id 
    <input type="hidden" name="id1[]" id="id1[]" value="<?php echo $sn;  ?>"  />

    //user id 

    //enable checkbox
    <input type="checkbox" name="enable[]" id="enable[]"  value="1"  <?php if($en=='1'){ echo 'checked';}  ?> />
    //enable checkbox
     <input type="submit" name="save" value="Save" class="fontBold">
    </form>
2
Contributors
8
Replies
28
Views
2 Years
Discussion Span
Last Post by Madiri Salman
0

HTML:

<form  action="enableprofile1.php" method="post">
    <input type="hidden" name="id[]" id="id[]" value="<?php echo $sn; ?>">
    <input type="checkbox" name="enable[]" id="enable[]"  value="1"  <?php if($en=='1'){ echo 'checked'; } ?>>
    <input type="submit" name="save" value="Save" class="fontBold">
</form>

PHP:

<?php
foreach ($_POST['id'] as $i => $id) {
    $enable = $_POST['enable'][$i];

    if (!$enable)
        continue;

    // Do whatever you want here.
}

If you use input names like "id[]", the POST values will be read as an array by PHP, enabling us to use (for example) a foreach() loop to loop through them.

0

What's not working? What's happening? What do you want to do?

Obviously you still need to add your own code for updating your database to my example.

0

You should probably name your checkbox manually, something like enabled[name][] for each checkbox group.

0

I'm afraid I cannot! You are ought to put some effort in this of your own :). We can offer help with your problems, not full solutions to your problems.

So what do you have so far, and where is it going wrong? If you have an example of what you've tried so far (and if it includes PHP and MySQL), then post it below and then maybe we can help you.

0

ok when i unchecked in am updating only one here's my backend script for form

<?php

/* ESTABLISH CONNECTION */

$con = new mysqli("localhost", "root", "12345", "test"); /* REPLACE NECESSARY DATA */

/* CHECK CONNECTION */
if (mysqli_connect_errno()) {
  printf("Connect failed: %s\n", mysqli_connect_error());
  exit();
}

if(isset($_POST["save"])){ /* IF SAVE BUTTON IS CLICKED */

  /* STORE THE PASSED DATA FROM YOUR FORM VIA POST */
  $enable = $_POST["enable"];
  $hiddenid = $_POST["id1"];
  $total = count($enable); /* COUNT TOTAL ARRAY */

  for($x=0;$x<=$total;$x++){ /* RUN A LOOP THAT CHECKS ALL DATA */

    if(!empty($enable[$x]))

    { 
    $todo=1; 
    }
    else 
    { 
    $todo=0; 
    }

     $stmt = $con->prepare("UPDATE ` talent_uploads`  SET enable = ? WHERE sno = ?"); /* REPLACE NECESSARY CONNECTION VARIABLE, TABLE NAME AND COLUMN NAME */
     $stmt->bind_param('ss', $todo, $hiddenid[$x]); /* BIND PASSED DATA TO THE QUERY */
     $stmt->execute(); /* EXECUTE QUERY */


echo '<script type="text/javascript">

window.location = "talent-profile.php";
</script>';



  } /* END OF FOR LOOP */

} /* END OF ISSET SAVE */

?>
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.