编辑: 所以我查找了 PDO Insert 语句。还是有点困惑。所以现在我的代码是:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>test14.php</title>
</head>
<?php
$servername = "server";
$username = "username";
$password = "password";
$dbname = "dbname";
//Setup for endDate value
date_default_timezone_set("America/Denver");
$startDate = new DateTime('12:00:00');
echo "<p>Today is: {$startDate->format( 'l Y-m-d g:i:s' )}</p>";
// N gives a numeric day to the days of the week
// 2 means Tuesday
while ( $startDate->format( 'N' ) != 2 )
$startDate->modify( '+1 days' );
echo "<p>Start date is: {$startDate->format('l Y-m-d g:i:s')}</p>";
//Set endDate at Tuesday
$endDate = $startDate ->modify("+2 Weeks");
echo "<p>End date is: {$endDate->format('l Y/m/d g:i:s')}</p>";
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = $conn->prepare("INSERT INTO wp_awpcp_ads (ad_enddate)
VALUES (:end_date)
");
$sql->bindParam(':end_date',strtotime($endDate), PDO::PARAM_STR);
// use exec() because no results are returned
$conn->exec($sql);
echo "New record created successfully";
}
catch(PDOException $e)
{
echo $sql . "<br>" . $e->getMessage();
}
$conn = null;
?>
<body>
</body>
</html>
我收到 2 条警告:
Warning: strtotime() expects parameter 1 to be string, object given in [site_url]/test14.php on line 40
Warning: PDO::exec() expects parameter 1 to be string, object given in [site_url]/test14.php on line 42
New record created successfully
而且我的表中仍然全是零。如果它是一个 wordpress 表,这有关系吗?我对 now()
函数没有任何问题,只有 DateTime()
函数。还有其他方法可以设置这样的日期吗?
我一直在尝试寻找一种方法来将自定义结束日期值添加到类型为“DateTime”的 mysql 数据库字段
具体而言,客户希望所有广告在事件 2 周后的星期二中午“结束”。到目前为止,我在 phphelp 论坛上得到了帮助,并得到了这个片段:
<?php
$servername = "server";
$username = "username";
$password = "password";
$dbname = "dbname";
//Setup for endDate value
date_default_timezone_set("America/Denver");
$startDate = new DateTime('12:00:00');
echo "<p>Today is: {$startDate->format( 'l Y-m-d H:i:s' )}</p>";
// N gives a numeric day to the days of the week
// 2 means Tuesday
while ( $startDate->format( 'N' ) != 2 )
$startDate->modify( '+1 days' );
echo "<p>Start date is: {$startDate->format('l Y-m-d H:i:s')}</p>";
//Set endDate at Tuesday
$endDate = $startDate ->modify("+2 Weeks");
echo "<p>End date is: {$endDate->format('l Y/m/d H:i:s')}</p>";
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "INSERT INTO wp_awpcp_ads
(
ad_contact_name,
ad_contact_email,
ad_contact_phone,
ad_title,
ad_category_id,
ad_item_price,
ad_details,
disabled,
disabled_date,
ad_postdate,
ad_startdate,
ad_last_updated,
ad_enddate
)
VALUES
(
'$_POST[name]',
'$_POST[email]',
'$_POST[phone]',
'$_POST[title]',
'$_POST[category]',
'$_POST[price]',
'$_POST[details]',
1,
now(),
now(),
now(),
now(),
$endDate->format('Y-m-d H:i:s'))";
// use exec() because no results are returned
$conn->exec($sql);
echo "New record created successfully";
}
catch(PDOException $e)
{
echo $sql . "<br>" . $e->getMessage();
}
$conn = null;
这工作正常,除了我的 ad_enddate
行显示 0000-00-00 00:00:00
并且我无计可施来解决这个问题。我已经将我的代码更改为 msqli 到 pdo,什么也没有。我是 php 的新手,上周需要这个。
最佳答案
这个enter link description here可以帮助您解决日期问题,我认为这是由于 $end_date 格式。
这是关于 PHP 中 SQL 注入(inject)的很好的回答 :) enter link description here
关于php - 试图将 php 对象插入 mysql DateTime 对象 "ad_endDate",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36878675/