is it possible to make INSERT all checkbox values into database when there is not even one checkbox being checked since im using array_diff as shown below?

my problem currently facing is,
when there is a checkbox being checked, it will insert into checked_table and the remainder options which did not selected will insert into unchecked_table.
howewer, when none of the checkbox is being selected, click on submit button, the value didnt send to my database unchecked_table.

I want all the unchecked checkbox insert into database as well. kindly assist please :(

<?php
    include('connect.php');
    $checked =  ($_POST['date']);
    $unchecked = ($_POST['nondate']);
    $diff = array_diff($unchecked,$checked);

            if(isset($_POST['date']))
            {
                foreach ($_POST['date'] as $dateValue)
                {
                $insert="INSERT INTO checked_table ($colm_ladate) VALUES ('$dateValue')";
                mysql_query($insert);
                }
                echo header("location:home.php");
        }
        if (isset($_POST['nondate']))
        {
        foreach ($diff as $dateValue2)
        {
            $insert2="INSERT INTO unchecked_table($colm_lrdate) VALUES ('$dateValue2')";
            mysql_query($insert2);
        }
            echo header("location:home.php");
            }

?>

Recommended Answers

All 10 Replies

As when you do not check check box, it is not posted.

So you must set master array while processing and find diff of master array and checked array.

$master = array(1,2,3,4,5,6,7,8,9,10,11);

$diff = array_diff($master,$checked);

hi, urtrivedi, thanks alot for your reply.

i get your idea which mentioned above.
but, i got no idea how to create my master array with the code below:

can u kindly guide me on this?
your help is very much appreciated :)

<?php
    $startDate = strtotime($datefrom);  
    $endDate = strtotime($dateto);  
    $date = $startDate;  

        while( $date <= $endDate) {  
        echo  "<input type='hidden' name='nondate[]' value ='".date('Y-m-d', $date)."'>";
        echo  "<input type='checkbox' name='date[]' value ='".date('Y-m-d', $date)."'>";
        echo date('Y-m-d', $date) .'&nbsp;';  
        $date = strtotime('+1 day', $date); 
    }

    ?>

i have tried with this, does it make sense?

<?php

    include('connect.php');

    $master = array();
    $master[] = $date;
    $checked =  ($_POST['date']);
    $diff = array_diff ($master, $checked);

            if(isset($_POST['date']))
            {
                foreach ($_POST['date'] as $dateValue)
                {
                            $insert="INSERT INTO checked_table($colm_ladate) VALUES ('$dateValue')";
                            mysql_query($insert);
                }
                    echo header("location:home.php");
            }
            if (isset($_POST['nondate']))
            {
            foreach ($diff as $dateValue2)
                {

                    $insert2="INSERT INTO unchecked_table($colm_lrdate) VALUES ('$dateValue2')";
                    mysql_query($insert2);

                }
                echo header("location:home.php");
            }

?>

You doing it correct. your unchecked array itself is master arrary
So you code must work

PUt two lines in begining of process page (where you write insert code)

error_reporting(E_ALL); 
ini_set("display_errors", 1); 

Check can you see any insert query error

hi, thanks for your reply. i get the errors as shown below:-

Notice: Undefined variable: date in C:\xampp\htdocs\date.php on line 6

Notice: Undefined index: date in C:\xampp\htdocs\date_process.php on line 7

Warning: array_diff(): Argument #2 is not an array in C:\xampp\htdocs\date_process.php on line 8

Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\date_process.php on line 21

Try with our original code and two more lines to find error

<?php
    error_reporting(E_ALL); 
    ini_set("display_errors", 1); 

    include('connect.php');
    $checked =  ($_POST['date']);
    $unchecked = ($_POST['nondate']);
    $diff = array_diff($unchecked,$checked);

            if(isset($_POST['date']))
            {
                foreach ($_POST['date'] as $dateValue)
                {
                $insert="INSERT INTO checked_table ($colm_ladate) VALUES ('$dateValue')";
                mysql_query($insert);
                }
                echo header("location:home.php");
        }
        if (isset($_POST['nondate']))
        {
        foreach ($diff as $dateValue2)
        {
            $insert2="INSERT INTO unchecked_table($colm_lrdate) VALUES ('$dateValue2')";
            mysql_query($insert2);
        }
            echo header("location:home.php");
            }

?>

Hi, sadly that, i still getting the error messages as shown:-

Notice: Undefined index: date in C:\xampp\htdocs\date_process.php on line 6

Warning: array_diff(): Argument #2 is not an array in C:\xampp\htdocs\date_process.php on line 8

Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\date_process.php on line 21

how should i defined the date here if the code for it will be as shown:-

<form id="status" name="status" method="post" action="date_process.php">
    <label>Date:-&nbsp;<b>FROM</b></label><input type="date" name="ldatefrom" value="<?php echo $datefrom;?>" disabled>
    <label><b>TO</b></label> <input type="date" name="ldateto" value="<?php echo $dateto;?>" disabled><br>
    <?php
    $startDate = strtotime($datefrom);  
    $endDate = strtotime($dateto);  
    $date = $startDate;  
        while( $date <= $endDate) {  
        echo  "<input type='hidden' name='nondate[]' value ='".date('Y-m-d', $date)."'>";
        echo  "<input type='checkbox' name='date[]' value ='".date('Y-m-d', $date)."'>";
        echo date('Y-m-d', $date) .'&nbsp;';  
        $date = strtotime('+1 day', $date); 
    }
    ?>
    <br/><input id="submit_hod" type="submit" name="submit">
    </form>

i have tried to use array merge as the master array to store everything, but still unable to get the output that i looking for.

the expected output:
in the form page, without checked any check box given and click on submit button, all the UNCHECKED check box values will be inserted into database.

the array merge code is as shown below:-

<?php
    include('connect.php');

    $checked =  ($_POST['date']);
    $unchecked = ($_POST['nondate']);
    $merge = array_merge($checked,$unchecked);
    $diff = array_diff($merge,$checked);

            if(isset($_POST['date']))
            {
                foreach ($_POST['date'] as $dateValue)
                {
                            $insert="INSERT INTO checked_table($colm_ladate) VALUES ('$dateValue')";
                            mysql_query($insert);
                }
                    echo header("location:home.php");
            }
            if (isset($_POST['nondate']))
            {
            foreach ($diff as $dateValue2)
                {
                    $insert2="INSERT INTO unchecked_table($colm_lrdate) VALUES ('$dateValue2')";
                    mysql_query($insert2);

                }
                echo header("location:home.php");
            }

?>
    $checked =  ($_POST['date']);
    $unchecked = ($_POST['nondate']);
    //$merge = array_merge($checked,$unchecked); // no need to merge as unchechk is your master array

    $diff = array_diff($unchecked,$checked);

Hi, it still can't work, but thank you very much for your assist =)

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.