Jump to content


Photo

MySQL Update Call With Dates


  • Please log in to reply
1 reply to this topic

#1 JustinK101

JustinK101
  • Members
  • PipPipPip
  • Advanced Member
  • 503 posts
  • LocationSan Diego, California, US

Posted 06 December 2005 - 12:51 AM

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.

#2 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 24 December 2005 - 03:03 AM

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.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users