I am trying to enter the date and time into datetime field in MySql. Right now, it is entering the datetime as 0000-00-00 00:00:00. I have 3 tables: article, user, and userarticles.
article: id (int), title (varchar), url (varchar), body (text), date (datetime)
user: id (int) ….
userarticle: uid (int), aid (int) -where uid is user.id and aid is article.id
here is part of my user controller:
function newArticle()
{
$this->load->view('newArticleview');
}
function insertArticle()
{
//insert from form into article table
$this->db->insert('article', $_POST);
//get ID of the row just added
$rowID = $this->db->insert_id();
$uid = $this->tank_auth->get_user_id();
$data = array('uid' => $uid, 'aid' => $rowID);
//insert uid and aid into userarticles table
$this->db->insert('userarticles', $data);
redirect('');
}
my newArticleview
<html>
<head>
<title>Save Me - Add New Article</title>
</head>
<body>
<?php
$date = date("Y-m-d H:i:s");
echo form_hidden('date', $date);
echo form_open('user/insertArticle');
?>
<p>Title:<input type="text" name="title"></p>
<p>URL:<input type="text" name="url"></p>
<p><textarea name="body" rows="20"></textarea></p>
<p><input type="submit" value="Submit"></p>
</form>
</body>
</html>
Everything is inserted into the database correctly except for datetime, does anyone know what is wrong?
The hidden form is showing up correctly as <input type="hidden" name="date" value="2011-04-08 21:51:10" />
You are outputting a hidden form field, before the form starts. Not sure if that will break CI but it’s invalid markup. the form_open helper can take a 3rd argument – an array of hidden fields. So something like this can be done instead/addition:
You don’t need to put the date in the view, really, unless you have specific use for it (i.e for use in javascript or something)
I have something like this in my model for created by/modified by times:
Where
createdis my column name.If you aren’t using a model, you could put this in your controller instead, but in the interest of MVC it should be in a model really.
Don’t forget you can always do:
print_r($this->input->post())orprint_r($_POST)to see what’s really being picked up – using the input library helps to sanities everything for you automatically, it’s not a requirement, obviously, but the functionality is built in to CI.