Welcome Guest, Not a member yet? Register   Sign In
Insert Current Date
#1

[eluser]harpster[/eluser]
Just learning CI and getting used to the MVC concept with the on-line videos and examples in the CI User Guide. I'm not sure about this part so I'm going to show what I did that worked and ask for how others would do the same thing. Very simply I need to insert the current date with my posted form data. I suppose I could have done this with a hidden field in the view to include that in the POST array. Doing it in the model after the fact seems to take more code as shown below. Is there a way to simplify this or is it better to use a hidden field in the view for the date? Thanks!


Code:
function add_record()
    {
        $this->db->insert('cycling', $_data);
        
        //Add CURRENT DATE to Record
        $id=$this->db->insert_id();  //get id of last record
        $this->db->where('id', $id);
        $data = array('ridedate' => date('Y-m-d'));
        $this->db->update('cycling', $data);
        return;
    }
#2

[eluser]cideveloper[/eluser]
how are you building the "$_data" array for the insert. Why couldn't you just set ridedate there.

if you are looping through the post variables to generate $_data, which I think is a bad idea, then you can just add ridedate to the end of $_data.

Code:
other code to get form variables....

$_data['ridedate'] = date('Y-m-d')
$this->db->insert('cycling', $_data);
#3

[eluser]harpster[/eluser]
I see... actually what I put down was not correct as I'm not using $data, I'm using $_POST to speed up my learning process so I can quickly make the complete CRUD model.

Code:
$this->db->insert('cycling', $_POST);

I'm only doing this temporarily until I figure out how to do Form Validation. So in this case the form data is already in the POST array and I'm just trying to add in the current date. But I see your point... when I do the form validation I can just add in the date there. Didn't see the forest through the trees... thanks!
#4

[eluser]mi6crazyheart[/eluser]
@harpster Hey why don't u use timestamp date data type. Timestamp will really kill(as it'll automatically add current date & time to u'r record) all u'r pain of inserting data & time.

There are different variation of timestamp date data type exist. You can get some detail info from here : http://dev.mysql.com/doc/refman/5.0/en/timestamp.html
#5

[eluser]InsiteFX[/eluser]
You should be using CI input->post! You just assking for XSS injection.

InsiteFX




Theme © iAndrew 2016 - Forum software by © MyBB