Picking checkbox from database in table and update it back using PHP

Sandhu

I picked data from database and arranged in table it also contains a checkbox as true if 1 and false if 0. The program should update the database according the checkbox functioning.

        <?php
            $query1="SELECT * FROM notices;";
            $result1=mysql_query($query1,$conn);
            $num_rows=mysql_num_rows($result1);
            $pick=array();
            if($num_rows>0)
            {
                while ( ($row=mysql_fetch_array($result1)) != null)
                { 
                $pick=$row['sl'];
                ?>
                <tr class="trc">
                    <td align="left"><label class="lab1"><?php echo($row['notice']); ?></label></td>
                    <td align="center"><label class="lab1"><?php echo($row['date']); ?></label></td>
                    <td align="center"><input type="checkbox" name="notice_id[]" value="YES" <?php 
                                            if($row['active']==1)
                                            {
                                                echo("checked");
                                            }
                                            ?> /></td>
                </tr>
            <?php 
                }
            }
        ?>

Below is the code to update data back to the dabase.

if(isset($_POST['up_note'])==true)
{
    $n_ids = $_POST['notice_id'];
    $sl_no = 1;

    foreach($n_ids as $key => $id)
    {
        echo($n_ids." | ".$id." | ".$key."<br />");

        $cb = ($POST['notice_id[$id]'] == 'YES')?'1':'0';

        if($cb==1)
        {
            $query3 = "UPDATE notices SET active = 1 WHERE sl='$sl_no';";
            $result3 = mysql_query($query3, $conn);
            echo("entered");
        }
        else
        {
            $query3 = "UPDATE notices SET active = 0 WHERE sl='$sl_no';";
            $result3 = mysql_query($query3, $conn);
            echo("not entered");
        }
        $sl_no = $sl_no + 1;    
    }
}
Vipin Kumar Soni
<?php
            $query1="SELECT * FROM notices;";
            $result1=mysql_query($query1,$conn);
            $num_rows=mysql_num_rows($result1);
            $pick=array();
            if($num_rows>0)
            {
                while ( ($row=mysql_fetch_array($result1)) != null)
                { 
                $pick=$row['sl'];
?>
                <tr class="trc">
                    <td align="left"><label class="lab1"><?php echo($row['notice']); ?></label></td>
                    <td align="center"><label class="lab1"><?php echo($row['date']); ?></label></td>
                    <td align="center"><input type="checkbox" name="notice_id[]" value="<?php echo $pick; ?>" <?php if($row['active']==1){ echo("checked"); } ?> /></td>
                </tr>
<?php 
                }
            }
?>


/////////////////////////////////////
if(isset($_POST['up_note'])==true)
{
    if(!empty($_POST['notice_id'])){
        $n_ids = $_POST['notice_id'];
            $query_all = "UPDATE notices SET active = 0";
            $result_all = mysql_query($query_all, $conn);
        foreach($n_ids as $key => $id){
            $sl_no = $n_ids[$key];
            $query3 = "UPDATE notices SET active = 1 WHERE sl=$sl_no;";
            $result3 = mysql_query($query3, $conn);
            echo("entered");    
        }
    } else {
           $query_all = "UPDATE notices SET active = 0";
           $result_all = mysql_query($query_all, $conn);
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Correct syntax to update a table in a database using PHP

From Dev

update table with multi checkbox in php

From Dev

update table with multi checkbox in php

From Dev

Update data from form to database using php

From Dev

get and display multiple checkbox values from database using php

From Dev

get and display multiple checkbox values from database using php

From Dev

UPDATE MySQL table from a CSV using PHP

From Dev

Format date from database to table using php

From Dev

Checkbox with Result from Database in PHP

From Dev

Update value in mysql database column with checkbox in php

From Dev

Update value in mysql database column with checkbox in php

From Dev

Unable to update MYSQL database table fields using php in XAMPP

From Dev

Update Database From Grid View Checkbox

From Dev

update database table from scrapy using Request Meta

From Dev

Using variables to update database table

From Dev

update and select from a database table

From Dev

Update database from ADF Table

From Dev

Update Image from database using ajax, jquery,and PHP not working

From Dev

Update Image from database using ajax, jquery,and PHP not working

From Dev

Not picking the right ID from the database

From Dev

checkbox is checked if value is in database in php using jquery

From Dev

How to retrieve checkbox value from database in PHP

From Dev

how to fill textbox with data on a click on a checkbox inside a table using jquery from database in mvc

From Dev

Update information in database using php

From Dev

Update information in database using php

From Dev

Update database using MySQL and PHP

From Dev

Select and Update form from table using php and mysqli

From Dev

Select from jlist and update jtable using checkbox

From Dev

how to access mysql table from wamp database using this php code?

Related Related

  1. 1

    Correct syntax to update a table in a database using PHP

  2. 2

    update table with multi checkbox in php

  3. 3

    update table with multi checkbox in php

  4. 4

    Update data from form to database using php

  5. 5

    get and display multiple checkbox values from database using php

  6. 6

    get and display multiple checkbox values from database using php

  7. 7

    UPDATE MySQL table from a CSV using PHP

  8. 8

    Format date from database to table using php

  9. 9

    Checkbox with Result from Database in PHP

  10. 10

    Update value in mysql database column with checkbox in php

  11. 11

    Update value in mysql database column with checkbox in php

  12. 12

    Unable to update MYSQL database table fields using php in XAMPP

  13. 13

    Update Database From Grid View Checkbox

  14. 14

    update database table from scrapy using Request Meta

  15. 15

    Using variables to update database table

  16. 16

    update and select from a database table

  17. 17

    Update database from ADF Table

  18. 18

    Update Image from database using ajax, jquery,and PHP not working

  19. 19

    Update Image from database using ajax, jquery,and PHP not working

  20. 20

    Not picking the right ID from the database

  21. 21

    checkbox is checked if value is in database in php using jquery

  22. 22

    How to retrieve checkbox value from database in PHP

  23. 23

    how to fill textbox with data on a click on a checkbox inside a table using jquery from database in mvc

  24. 24

    Update information in database using php

  25. 25

    Update information in database using php

  26. 26

    Update database using MySQL and PHP

  27. 27

    Select and Update form from table using php and mysqli

  28. 28

    Select from jlist and update jtable using checkbox

  29. 29

    how to access mysql table from wamp database using this php code?

HotTag

Archive