doujiyan0971 2016-04-27 01:22
浏览 69

试图将php对象插入mysql DateTime对象“ad_endDate”

EDIT: So I looked up PDO Insert statements. Still kind of confusing. so now my code reads:

<!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>

I'm getting 2 warnings:

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

and I'm still getting all zeros in my table. Does it matter if it's a wordpress table? I'm not having any issues with the now() functions just DateTime()functions. Is there another way to setup a date like this?


I have been trying to find a way to add a custom end date value to a mysql database field with the type "DateTime"

Specifically, the customer wants all ads to be "ended" on a tuesday at noon following 2 weeks of activity. so far I've been helped at phphelp forums and have this snippet:

    <?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;

This works fine except my ad_enddate row shows 0000-00-00 00:00:00 and i'm at wit's end to solve this issue. I've changed my code to msqli to pdo, and nothing. I'm completely new to php and need this by last week.

  • 写回答

2条回答 默认 最新

  • dsbpaqt61965 2016-04-27 05:59
    关注

    this enter link description here can help You with date problem, I think it's due to $end_date format.

    And this is great answer about SQL Injections in PHP :) enter link description here

    评论

报告相同问题?

悬赏问题

  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM