Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    Jan 2006
    Posts
    377
    Thanks
    8
    Thanked 1 Time in 1 Post

    Update database with dynamic checkbox info

    Hi,

    I have a table with following structure. This table is to keep records of which titles the member has on his list.

    tablea:
    ID title checked


    I pull records from the table and display them. He can check or uncheck the results and submit the form for update.

    PHP Code:

    <form method="post" action="<?php $_SERVER['PHP_SELF']; ?>">

    <?php

    $result 
    mysql_query("SELECT ID,title,checked FROM tablea") or die(mysql_error());
    while (
    $row mysql_fetch_array($result)) {
        
    extract($row);
        echo 
    "$title";
        echo 
    "&nbsp;";
        echo 
    "<input type=\"checkbox\" name=\"checkbox[]\"";
        if (
    $checked==1) {
            echo 
    " checked";
        }
        echo 
    ">";
        echo 
    "<br />";
    }
    ?>

    <input type="submit" value="Update Database">
    </form>
    When the form is submitted, I need a foreach loop that checks what is checked or unchecked, then updates the database tablea.checked, which is type ENUM '0','1'.

    I am stuck because if a checkbox is not checked, it is not submitted at all. So somehow I need to check existing database value as well...
    Last edited by guvenck; 02-17-2006 at 09:22 AM.

  • #2
    Senior Coder
    Join Date
    Nov 2002
    Location
    North-East, UK
    Posts
    1,265
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You could do a global update statement to reset all fields to 0 then use your submitted data to update the checked values to 1

  • #3
    Regular Coder
    Join Date
    Jan 2006
    Posts
    377
    Thanks
    8
    Thanked 1 Time in 1 Post
    Degsy,
    Nice tip, will do it. Can you also give an example for the foreach loop?

  • #4
    Senior Coder
    Join Date
    Nov 2002
    Location
    North-East, UK
    Posts
    1,265
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You don't need to use a foreach loop.

    You will be recieving an array so you can use IN.

    PHP Code:
    echo "<input type=\"checkbox\" name=\"checkbox[]\" value=\"$id\"";
        if (
    $checked==1) {
            echo 
    " checked";
        }
        echo 
    ">";
        echo 
    "<br />"
    PHP Code:
    "UPDATE tablea SET checked = 1 WHERE id IN (" $_POST['checkbox'] . ")"
    http://www.w3schools.com/sql/sql_in.asp

  • #5
    Regular Coder
    Join Date
    Jan 2006
    Posts
    377
    Thanks
    8
    Thanked 1 Time in 1 Post
    I used this loop, is working:

    PHP Code:
    if(isset($_POST['submit'])) {
        include (
    "db.php");
        
    $reset "DELETE FROM table WHERE usrid='$myid'";
        
    $doreset mysql_query($reset) or die(mysql_error());
        foreach (
    $_POST['check_id'] as $value){
            
    $update "INSERT table (usrid,rdid) VALUES ($myid,$value)";
            
    $doupdate mysql_query($update) or die(mysql_error());
        }


  • #6
    Senior Coder
    Join Date
    Nov 2002
    Location
    North-East, UK
    Posts
    1,265
    Thanks
    0
    Thanked 0 Times in 0 Posts
    ok, just if you did have thousands of records it would be quicker to perform one query instead of thousands of queries.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •