Welcome Guest, Not a member yet? Register   Sign In
Easy Time Question
#1

[eluser]garrettheel[/eluser]
Basically, I just want a way to get the current time when using active record to manipulate a database, MYSQL's native NOW() function seems not to be working. Here's the current code I've got..

Code:
$data = array(
        "username" => $_POST['username'],
        "password" => sha1($_POST['password']),
        "email" => $_POST['email'],
        "register_date" => NOW()
    );

But this doesn't seem to put any time in the database..
#2

[eluser]xwero[/eluser]
you have to add sql functions as strings otherwise php will try to execute them.
#3

[eluser]garrettheel[/eluser]
You wouldn't know the PHP way to get the same string then, would you?
#4

[eluser]xwero[/eluser]
I'm not sure why you would want that as you can do
Code:
"register_date" => 'NOW()'
But to get the same string you have to use the date function.
#5

[eluser]garrettheel[/eluser]
Ah, I just misunderstood you. I thought you meant I had to use PHP to get it in a variable as a string and then do it that way. Thanks Smile
#6

[eluser]garrettheel[/eluser]
Btw, I just tried using 'NOW()' instead of NOW() and it still hasn't filled in the date.. is this something to do with active record?
#7

[eluser]kevinprince[/eluser]
PHP doesn't execute variables or function calls in single quotes like '$var'.

It will execute values in "$var" speach marks however.

The easiest way to fix this is to declare now before you build the array. Also you ideally should not be using the $_POST global, or be posting data directly into the DB without cleaning. Your $data var should look like this.

Code:
$time = now();

$data = array(
        "username" => $this->input->post('username'),
        "password" => sha1($this->input->post('password'),
        "email" => $this->input->post('email'),
        "register_date" => $time
    );

By using the CI input class you automatically filter out everything which isn't alphanumeric or in the allowed characters list. This stops XSS attacks and SQL Injection.

Full details @ http://ellislab.com/codeigniter/user-gui...input.html
#8

[eluser]xwero[/eluser]
[quote author="nextgengames" date="1228757642"]PHP doesn't execute variables or function calls in single quotes like '$var'.

Code:
$time = now();

[/quote]

Now is not a php function AFAIK. Now is a mysql function that is why i've written it should be passed to the AR library as a string.

I guess the problem now is that the AR library escapes the function, like this: `NOW()`. You can check it using the db->last_query method.

To prevent the function from being escaped you can do
Code:
$this->db->set('register_date','NOW()',FALSE);
#9

[eluser]garrettheel[/eluser]
Okay, so the way you described still didn't work.. my field type is datetime in mysql, that's okay isn't it? For the record, the output of now() is "int(1228736789)".

And I've already filtered the data with validation, of course, but I didn't know about that input library you could use instead of the post superglobal, looks like a good idea - thanks!
#10

[eluser]garrettheel[/eluser]
xwero, you were correct Smile Now is there a way I can do that within the $data variable using active record? Or should I just set everything else in $data and have a separate statement for the date?




Theme © iAndrew 2016 - Forum software by © MyBB