<b>可捕获的致命错误</b>:类DateTime的对象无法转换为字符串

时间:2015-06-12 07:29:39

标签: php sql sql-server datetime

我试图在sql server中执行插入查询。但是,每次执行查询时,它都会返回

  

可捕获的致命错误:类DateTime的对象不可能   转换为 C:\ xampp \ htdocs \ fusion \ insertTrade.php 中的字符串   在线 29

我无法理解发生了什么,因为在那条线上,我甚至没有做任何与日期时间有关的事情。任何人都可以帮忙吗?

insertTrade.php:

$fullName = $_POST["fullName"];
  $ticSymbol = $_POST["tickSymbol"];
  $limitOrder = $_POST["limitOrder"];
  $limitPrice = $_POST["limitPrice"];
  $quantity = $_POST["quantity"];
  $contingentOrder = $_POST["contingentOrder"];
  $contingentLimitPrice = $_POST["contingentPrice"];
  $contingentQuantity = $_POST["contingentQuantity"];
  $date = new DateTime();
  $date -> setDate($_POST["year"], $_POST["month"], $_POST["day"]);
  $date -> setTime($_POST["hours"], $_POST["minutes"], $_POST["seconds"]);
  // var_dump($date);
      $query = "INSERT INTO [competitionTrade]
                   ([fullName]
                   ,[date]
                   ,[limitOrder]
                   ,[quantity]
                   ,[tickerSymbol]
                   ,[limitPrice]
                   ,[contingentOrder]
                   ,[contingentQuantity]
                   ,[contingentLimitPrice])
             VALUES
                   (<$fullName, varchar(50),>
                   ,<$date, datetime,>
                   ,<$limitOrder, varchar(50),> //line 29
                   ,<$quantity, int,>
                   ,<$ticSymbol, varchar(50),>
                   ,<$limitPrice, float,>
                   ,<$contingentOrder, varchar(50),>
                   ,<$contingentQuantity, int,>
                   ,<$contingentLimitPrice, float,>)";
      $stmt = sqlsrv_query($conn, $query);

编辑1:根据D4V1D的建议,我将其更改为:

  $fullName = $_POST["fullName"];
      $ticSymbol = $_POST["tickSymbol"];
      $limitOrder = $_POST["limitOrder"];
      $limitPrice = $_POST["limitPrice"];
      $quantity = $_POST["quantity"];
      $contingentOrder = $_POST["contingentOrder"];
      $contingentLimitPrice = $_POST["contingentPrice"];
      $contingentQuantity = $_POST["contingentQuantity"];
      $date = new DateTime();
      $date -> setDate($_POST["year"], $_POST["month"], $_POST["day"]);
      $date -> setTime($_POST["hours"], $_POST["minutes"], $_POST["seconds"]);
      // $dateString = $date->format('Y-m-d H:i:s');
      // var_dump($date);
          $query = "INSERT INTO [competitionTrade]
                       ([fullName]
                       ,[date]
                       ,[limitOrder]
                       ,[quantity]
                       ,[tickerSymbol]
                       ,[limitPrice]
                       ,[contingentOrder]
                       ,[contingentQuantity]
                       ,[contingentLimitPrice])
                 VALUES
                       (<$fullName, varchar(50),>
                       ,<$date->format('Y-m-d H:i:s'), datetime,> //line 29
                       ,<$limitOrder, varchar(50),>
                       ,<$quantity, int,>
                       ,<$ticSymbol, varchar(50),>
                       ,<$limitPrice, float,>
                       ,<$contingentOrder, varchar(50),>
                       ,<$contingentQuantity, int,>
                       ,<$contingentLimitPrice, float,>)";
          $stmt = sqlsrv_query($conn, $query);

现在说

  

未定义的属性:Dateline :: $ format in C:\ xampp \ htdocs \ fusion \ insertTrade.php 29

1 个答案:

答案 0 :(得分:3)

这是因为您尝试在[date]字段中包含字符串$query中的对象日期时间(因此Object could not be converted to string错误)。

您需要这样做:

$date = $date->format('Y-m-d H:i:s'); // $date is now a string
$query = "INSERT INTO [competitionTrade]
                   ([fullName]
                   ,[date]
                   ,[limitOrder]
                   ,[quantity]
                   ,[tickerSymbol]
                   ,[limitPrice]
                   ,[contingentOrder]
                   ,[contingentQuantity]
                   ,[contingentLimitPrice])
             VALUES
                   (<$fullName, varchar(50),>
                   ,<$date, datetime,>
                   ,<$limitOrder, varchar(50),>
                   ,<$quantity, int,>
                   ,<$ticSymbol, varchar(50),>
                   ,<$limitPrice, float,>
                   ,<$contingentOrder, varchar(50),>
                   ,<$contingentQuantity, int,>
                   ,<$contingentLimitPrice, float,>)";