[英]Php oop insert date/time in database
我目前正在学习OOP,但是我不知道如何在数据库中插入日期。 我努力了:
$time=date('now');
$time=date("timestamp");
$time=date("Y-m-d H:i:s");
$time=date("m/d/y g:i A");
提交时出现错误:
您的SQL语法有误; 在第1行的'2014-03-11 14:18:07''附近检查与MySQL服务器版本相对应的手册以使用正确的语法
我不知道这个。 在我使用之前:
$result = mysql_query ("INSERT INTO test (title,url,time) VALUES ('$title','$url',now())");
我尝试使用now() ,但似乎不起作用。 :/
更新:
插入:
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();
}
}
处理文件:
$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);
您缺少报价:
'$author', '$image, '$time'"
^^^^^
HERE
INSERT INTO blog_posts(cat, title, article, time, author, image) VALUES('$cat', '$title', '$article', '$author', '$image', '$time'"
您的参数也有问题。
Columns: cat, title, article, time, author, image
Values: '$cat', '$title', '$article', '$author', '$image', '$time'
忘了mysql_ *函数。
如果要创建面向未来的应用程序,则可以使用PDO 。 这是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!
如果时间列的类型为日期或日期时间,则可以像下面这样使用NOW()或CURDATE() :
$prepare = $db->prepare("INSERT INTO test (title, url, time) VALUES (:title, :url, NOW())");
与此代码中的对象无关。
并且第一个代码段基本上都覆盖了整个覆盖范围,因此它的最后一个是最新的。 您到底想达到什么目的? 您可以再发布一些代码吗?
如果您希望使用面向mysql OBJECT的mysql,请使用MySQLi或PDO驱动程序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.