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
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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.