mysql - Php oop insert date/time in database -


i learning oop, can't figure out how insert date database. have tried:

$time=date('now'); $time=date("timestamp"); $time=date("y-m-d h:i:s"); $time=date("m/d/y g:i a"); 

when submit error:

you have error in sql syntax; check manual corresponds mysql server version right syntax use near '2014-03-11 14:18:07'' @ line 1

i can't figure out. before did use:

$result = mysql_query ("insert test (title,url,time) values ('$title','$url',now())"); 

i tried use now() , not seem work. :/

update:

insert:

public function insert($cat,$title,$article,$author,$image,$time) {     $sql=mysql_query("insert blog_posts(cat, title, article, time, author, image) values('$cat', '$title', '$article', '$author', '$image, '$time'");     if(!$sql)     {         echo mysql_error();     } } 

proccess file:

  $cat=$_post['cat'];     $title=$_post['title'];     $article= $_post['article'];     $author=$_post['author'];     $image=$_post['image'];     $time= date( 'y-m-d h:i:s' );      $n=new db();     $n->connect();     $n->insert($cat,$title,$article,$author,$image,$time); 

forget mysql_* functions.

if want create future-proof applications use pdo. oop.

$db = new pdo('mysql:host='.db_host.';dbname='.db_name, db_user, db_pass); $db->exec('set names utf8'); // set transfer's charset utf-8  $title = 'here title'; $url = 'here url'; $time= date( 'y-m-d h:i:s' ); // dont know type time column is. (date, datetime, timestamp)-> ??  $prepare = $db->prepare("insert test (title, url, time) values (:title, :url, :time)"); $prepare->bindparam(':title', $title); $prepare->bindparam(':url', $url); $prepare->bindparam(':time', $time);  $prepare->execute(); // run query, in case, insert! 

if time column of type date or datetime, can use now() or curdate() so:

$prepare = $db->prepare("insert test (title, url, time) values (:title, :url, now())"); 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

android - Keyboard hides my half of edit-text and button below it even in scroll view -

css - Make div keyboard-scrollable in jQuery Mobile? -