我试图这样做,当用户输入他们的名字,姓氏和当前位置时,日期/时间应自动输入(记录)到日期列并保存。然后它应该显示表中的所有行。我一直收到错误(下面)。
<!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>
<title></title>
</head>
<center>
<h2> ACW LOCATION TRACOR </h2>
</center>
<body>
<table width="200" border="5" cellpadding="0" cellspacing="1">
<tr>
<th> Username </th>
<th> First_Name </th>
<th> Surname </th>
<th> Current_Location </th>
<th> TimeStamp </th>
<tr>
<?php
$server = 'SQL2008.net.dcs.hull.ac.uk';
$connectionInfo = array("Database"=>"rde_531545");
$conn = sqlsrv_connect($server,$connectionInfo);
$query='create table Location ';
$query .= '(Username int NOT NULL IDENTITY(500, 23), First_Name varchar(50) NOT NULL, Surname varchar(50) NOT NULL, Current_Location varchar(50) NOT NULL, Date Datetime NOT NULL, PRIMARY KEY (Username))';
$result = sqlsrv_query($conn, $query);
if (!$result){
if( ($errors = sqlsrv_errors() ) != null){
foreach( $errors as $error ){
echo "<p>Error: ".$error[ 'message']."</p>";
}
}
}
else{
echo "<p>DB successfully created</p>";
}
sqlsrv_close($conn);
date_default_timezone_set('Europe/London');
$Date = new DateTime();
$DateStr = $Date->format('Y-m-d H:i:s');
$server = 'SQL2008.net.dcs.hull.ac.uk';
$connectionInfo = array( "Database"=>"rde_531545");
$conn = sqlsrv_connect($server,$connectionInfo);
$insert_query = "INSERT INTO Location (First_Name, Surname, Current_Location, Date) VALUES (?, ?, ?, ?)";
$params = array("Jon","Doey","Leeds", $DateStr);
$result = sqlsrv_query($conn,$insert_query,$params);
$params = array("John","Doe","Hull", $DateStr);
$result = sqlsrv_query($conn,$insert_query,$params);
$SelectQuery='SELECT Username, First_Name, Surname, Current_Location, Date FROM Location';
$results = sqlsrv_query($conn, $SelectQuery);
while($row = sqlsrv_fetch_array($results, SQLSRV_FETCH_ASSOC)){
echo "<tr><td>".$row['Username']."</td><td>".$row['First_Name']."</td><td>".$row['Surname']."</td><td>".$row['Current_Location']."</td><td>".$dateStr = $Date->format("Y-m-d H:i:s")."</td></tr>";
}
sqlsrv_close($conn);
?>
</table>
</body>
</html>
期望的输出:
512 |简| Doe |船体| 2012/12/12 18:24:45 PM
我一直收到此错误,也没有将日期保存到数据库中:
可捕获的致命错误:第69行的C:\ RDEUsers \ NET \ 531545 \ LocationPOST.php中无法将类DateTime的对象转换为字符串