简体   繁体   English

与Datetime2和PHP麻烦

[英]Trouble with Datetime2 and php

I've been tasked with updating my University IT department's inventory site. 我的任务是更新大学IT部门的清单站点。 The inventory is stored in a Microsoft SQL Server, and the website's in php. 清单存储在Microsoft SQL Server中,网站存储在php中。 Before, they used a Date datatype called lastUpdated to store when a piece of inventory's page was last updated. 以前,他们使用称为lastUpdated的Date数据类型来存储清单页面的最后更新时间。 Now they want a timestamp added, so I changed the lastUpdated column to datetime2. 现在他们想要添加时间戳,因此我将lastUpdated列更改为datetime2。

I'm trying to edit the php to reflect this change, but it gives me the following error when I submit the form (click the "add inventory" button) : Conversion failed when converting date and/or time from character string. 我正在尝试编辑php以反映此更改,但是在提交表单时(单击“添加库存”按钮),它给了我以下错误:从字符串转换日期和/或时间时转换失败。

I have limited knowledge of php. 我对php的了解有限。 My supervisor knows this, but said I should do it for the learning experience. 我的主管知道这一点,但我说我应该这样做是为了学习。 I tried googling the error, but (most likely due to my lack of experience) nothing has worked. 我尝试使用Google搜索错误,但是(很可能是由于缺乏经验)没有任何效果。 If someone could help me out in this I'd be so appreciative. 如果有人可以帮助我,我将非常感激。

Going to try and paste the relevant code without the extra inventory stuff. 尝试粘贴相关代码而无需额外的库存内容。

$_POST['lastUpdated'] = sanitize($_POST['lastUpdated']);
<div class='row'>
          <label for='lastUpdated'>Last Updated:</label>
          <input type='text' id='ldate' name='lastUpdated' class='readonly' size='20' value='".date("Y-m-d H:i:s")."' readonly />  
          <span id='ldateInfo'>Last updated in inventory</span>
</div>      

$params = array(&$_POST['contactID'], &$_POST['propertyNum'], &$_POST['serialNum'], &$_POST['bldg'], &$_POST['room'], &$_POST['offCampus'], &$_POST['description'], &$_POST['status'], &$_POST['dateAdded'], &$_POST['lastUpdated'], &$_POST['computerName'], &$_POST['warrantyDate']);
    $tsql = "INSERT INTO dbo.tblInventory (contactID, propertyNum, serialNum, bldg, room, offCampus, description, status, dateAdded, computerName, warrantyDate, lastUpdated) 
            VALUES (?,?,?,?,?,?,?,?,?,?,?,?)";
    $insertInventory = $conn->prepare($tsql);
    $insertInventory->execute($params);

There is no straight forward function available in php to get MSSQL Datetime2 string. php中没有直接函数可以获取MSSQL Datetime2字符串。

I would suggest you to create Stored proceduure on MSSQL DB. 我建议您在MSSQL DB上创建存储过程。 You can pass required data to the procedure. 您可以将所需的数据传递给该过程。 get lastUpdated as current date and fire a insert query in stored procured. 获取lastUpdated作为当前日期,并在存储的采购中触发插入查询。

Found stupidly simple solution: The values in $params didn't match up with the values in $tsql. 找到了愚蠢的简单解决方案:$ params中的值与$ tsql中的值不匹配。 Much crying ensued. 随后哭了很多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM