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>

Recommended Answers

All 8 Replies

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.

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.

it's working CAN I KNOW WHEN I UNCHECKED CHECKBOX CAN I STORE VALUE LIKE 0

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

can you write full code i am new to php

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.

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 */

?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.