Jump to content

MySQL Update Call With Dates


JustinK101

Recommended Posts

Hello, I have a form that has a series of checkboxes. I store if the checkbox is checked and what time it was checked. The problem is when I update one or two checkboxes and submit it changes all the dates in the database. It updates all the dates because I am using the mysql UPDATE function, and since the date is different from what is stored previously it just overides the date, even though I didnt acutally change a checkbox state necessary. Perhaps some code would help.

 

$_1_date = date('Y-m-d G:i:s', time() - $time_offset);

$_2_date = date('Y-m-d G:i:s', time() - $time_offset);

$_3_date = date('Y-m-d G:i:s', time() - $time_offset);

             // UPDATE EXISTING CHECKLIST
            if(isDuplicate("checklist" , "associated_order_id" , $order_id))
            {
                $sql = "UPDATE checklist SET 
                
                   _1 = '$_1', _1_date = '$_1_date', _2 = '$_2', _2_date = '$_2_date' ,
                  _3 = '$_3', _3_date = '$_3_date'
                
                WHERE `associated_order_id` = '" . $order_id . "' LIMIT 1";
                
                mysql_query($sql) || die(mysql_error());
                echo '<p align="center" class="success">Checklist updated <b>successfully</b>! </p>';
                die();
            }
            
            // CREATE NEW CHECKLIST
            else
            {
                $sql = "INSERT INTO checklist 
                (
                    associated_order_id, _1, _1_date,  _2, _2_date,  _3, _3_date                
                )
                VALUES 
                        
                (
                    '$order_id', '$_1', '$_1_date', '$_2', '$_2_date', '$_3', '$_3_date'                             
                )"; 
                
                mysql_query($sql) || die(mysql_error());
                echo '<p align="center" class="success">Checklist created <b>successfully</b</p>';
                
                die();
            } 

 

So _1, _2, _3 store the value of the check boxes and _1_date, _2_date, _3_date store the current date.

Link to comment
https://forums.phpfreaks.com/topic/2990-mysql-update-call-with-dates/
Share on other sites

  • 3 weeks later...

That's because you're not checking the initial state of the checkboxes you set -- you're always updated all of the checkboxes! You need to only include those checkboxes whose values have changed. Since you know what they were when you populated the page (i.e. in hidden values or query the DB again), you can check against those first, and build your UPDATE clause accordingly.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.