I am trying to create a calender with a checkbox for each date. When a user clicks the submit button, I want to get those selected dates inserted into the database and those which are not selected would be deleted from the database if those already exist before. I have created a index.php page where I wrote the following code to generate dates:

<form name="form1" method="post" action="show.php">
              <?php
               $year="2011";
               $month="8";
               $d=cal_days_in_month(CAL_GREGORIAN,1,2011);

                $i=1;
               while($i<=$d)
                {
                  ?>
               <input type="checkbox" name="date[]" value="<? echo  " $year-$month- " . $i . ""."<br>";  ?>"> 
                <? echo  " $year-$month- " . $i . ""."<br>";  ?>
                      <?
                  $i++;
                     }

                      ?> 
                <input type="submit" name="Submit" value="Submit">
                      </form>

And then I created page another page named show.php where I tried to write codes for inserting information, but I failed. Would you please kindly help me to do this? I have tried my best but I couldn't make it happened. Thanks in advance.

Dani AI

Generated

Good start, — two quick, practical notes before the database work: the value attribute must contain only the date string (no HTML like <br> or extra spaces), and the server must treat the absence of $_POST['date'] as “no dates selected.” and were right that the form POST carries the selected dates, but the next step is validation, normalization and safe DB updates.

Validate and normalize every incoming date to a canonical Y-m-d format before inserting. Example pattern to accept single-digit months/days and normalize:

$validDates = [];
if (!empty($_POST['date']) && is_array($_POST['date'])) {
    foreach ($_POST['date'] as $d) {
        $d = trim($d);
        $dt = DateTime::createFromFormat('Y-n-j', $d);
        if ($dt && $dt->format('Y-n-j') === $d) {
            $validDates[] = $dt->format('Y-m-d'); // normalized
        }
    }
}

Use a transaction and prepared statements to make delete+insert atomic. Scope deletes to the current user and month range to avoid wiping other users' data. Example pattern (PDO):

$db->beginTransaction();
$stmtDel = $db->prepare('DELETE FROM user_dates WHERE user_id=:uid AND dt BETWEEN :from AND :to');
$stmtDel->execute([':uid'=>$uid, ':from'=>$from, ':to'=>$to]);

$stmtIns = $db->prepare('INSERT INTO user_dates (user_id, dt) VALUES (:uid, :dt)');
foreach ($validDates as $dt) {
    $stmtIns->execute([':uid'=>$uid, ':dt'=>$dt]);
}
$db->commit();

If you want fewer writes, fetch existing dates and compute a diff: toInsert = submitted - existing, toDelete = existing - submitted. When rendering the form, query the saved dates and set checked for matches (use htmlspecialchars when outputting values). Always use parameterized queries, add a unique index on (user_id, dt), and include CSRF protection and proper error handling.

Recommended Answers

All 2 Replies

Just check whether your checkboxes are checked or not, and if yes, what their value is? All checkboxes and their values can be found in the $_POST data. :)

The selected dates will be in $_POST as an array of those dates selected. If no dates were checked, $_POST won't exist! So don't just assume an array will be there.

You can store the current month and year in hidden fields in your form. After submission, delete ALL entries for month-year in the database, then insert entries found in the date array.

As your form is now, you will lose past entries if a user updates a month. Existing entries must be read from the database and have their check boxes be checked when you render the form.

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.