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



我正在尝试在SQL服务器中执行插入查询。但是,每次我执行查询时,它都会返回

可捕获的致命错误:类 DateTime 的对象不能 转换为 C:\xampp\htdocs\fusion\insertTrade.php29

我不明白发生了什么,因为在那一行我什至没有对日期时间做任何事情。谁能帮忙?

插入交易.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);

它现在说

未定义的属性:C:\xampp\htdocs\fusion\insertTrade.php 中的 DateTime::$format 在第 29

这是因为您尝试在字符串$query中包含[date]字段中的对象 Datetime(因此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,>)";

相关内容

  • 没有找到相关文章

最新更新