简体   繁体   English

PHP oop在数据库中插入日期/时间

[英]Php oop insert date/time in database

I am currently learning OOP, but I can't figure out how to insert date into database. 我目前正在学习OOP,但是我不知道如何在数据库中插入日期。 I 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 I submit I get error: 提交时出现错误:

You have an error in your SQL syntax; 您的SQL语法有误; check the manual that corresponds to your MySQL server version for the right syntax to use near '2014-03-11 14:18:07'' at line 1 在第1行的'2014-03-11 14:18:07''附近检查与MySQL服务器版本相对应的手册以使用正确的语法

I can't figure this out. 我不知道这个。 Before I did use: 在我使用之前:

$result = mysql_query ("INSERT INTO test (title,url,time) VALUES ('$title','$url',now())");

I tried to use now() , but it does not seem to work. 我尝试使用now() ,但似乎不起作用。 :/ :/

Update: 更新:

Insert: 插入:

public function insert($cat,$title,$article,$author,$image,$time)
{
    $sql=mysql_query("INSERT INTO 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);

You're missing a quote: 您缺少报价:

'$author', '$image, '$time'"
               ^^^^^
                HERE

INSERT INTO blog_posts(cat, title, article, time, author, image) VALUES('$cat', '$title', '$article', '$author', '$image', '$time'"

Your parameters are also out of order. 您的参数也有问题。

Columns: cat, title, article, time, author, image
Values: '$cat', '$title', '$article', '$author', '$image', '$time'

Forget about the mysql_* functions. 忘了mysql_ *函数。

If you want to create future-proof applications you will use PDO . 如果要创建面​​向未来的应用程序,则可以使用PDO This is OOP . 这是OOP

$db = new PDO('mysql:host='.DB_HOST.';dbname='.DB_NAME, DB_USER, DB_PASS);
$db->exec('SET NAMES utf8'); // Set this transfer's charset to utf-8

$title = 'here is a title';
$url = 'here is a URL';
$time= date( 'Y-m-d H:i:s' ); // I dont know what type the time column is. (date, datetime, timestamp)-> ??

$prepare = $db->prepare("INSERT INTO test (title, url, time) VALUES (:title, :url, :time)");
$prepare->bindParam(':title', $title);
$prepare->bindParam(':url', $url);
$prepare->bindParam(':time', $time);

$prepare->execute(); // Run the query, in this case, insert!

If the time column is of type date or datetime, you can use NOW() or CURDATE() like so: 如果时间列的类型为日期或日期时间,则可以像下面这样使用NOW()CURDATE()

$prepare = $db->prepare("INSERT INTO test (title, url, time) VALUES (:title, :url, NOW())");

theres nothing to do with objects in this code. 与此代码中的对象无关。

and the first snippet of code is bascily overwriting all over so its left with the latest. 并且第一个代码段基本上都覆盖了整个覆盖范围,因此它的最后一个是最新的。 what exactly are you trying to achieve? 您到底想达到什么目的? could you post some more of your code? 您可以再发布一些代码吗?

if you wish to use mysql OBJECT oriented, use MySQLi or PDO driver. 如果您希望使用面向mysql OBJECT的mysql,请使用MySQLi或PDO驱动程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM