PDO:日期不插入,建议?

时间:2017-07-13 07:52:07

标签: php pdo

我正在尝试使用以下代码将日期插入数据库,我收到以下错误:错误:SQLSTATE [22007]:无效的日期时间格式:7错误:类型日期的输入语法无效:“”

所以日期没有通过,但我可以从POST返回日期:echo $ _SESSION ['dateOpen'];这会将日期返回为2014-06-01

我有点像菜鸟,所以欢迎任何建议。使用postgresql数据库。

try {
$sql2 = "INSERT INTO excavation.contexts_spatial 
    (area_easting, 
    area_northing, 
    context_number, 
    open_date,
    close_date,
    excavation_method,
    contamination,
    zooarchaeology_comments,
    ceramic_comments) VALUES (
    :area_easting, 
    :area_northing, 
    :context_number, 
    :open_date, 
    :close_date, 
    :excavation_method,
    :contamination,
    :zooarchaeology_comments,
    :ceramic_comments)";

$stmt2 = $conn->prepare($sql2);

// prepare sql and bind parameters
$stmt2->bindParam(':area_easting', $area_easting, PDO::PARAM_INT);       
$stmt2->bindParam(':area_northing', $area_northing, PDO::PARAM_INT); 
$stmt2->bindParam(':context_number', $nextContext, PDO::PARAM_INT);
$stmt2->bindParam(':open_date', $open_date, PDO::PARAM_STR);
$stmt2->bindParam(':close_date', $close_date, PDO::PARAM_STR);
$stmt2->bindParam(':excavation_method', $excavation_method, PDO::PARAM_STR);
$stmt2->bindParam(':contamination', $contamination, PDO::PARAM_STR);
$stmt2->bindParam(':zooarchaeology_comments', $excavation_method, PDO::PARAM_STR);
$stmt2->bindParam(':ceramic_comments', $excavation_method, PDO::PARAM_STR);



// insert a row
$area_easting = $_SESSION['area_easting'];
$area_northing = $_SESSION['area_northing'];
$nextContext = $_SESSION['nextContext'];
$open_date = $_SESSION['dateOpen'];
$close_date = $_SESSION['dateClose'];
$excavation_method = $_SESSION['excavationMethod'];
$contamination = $_SESSION['contamination'];
$zooarchaeology_comments = $_SESSION['zooarchaeologyComments'];
$ceramic_comments = $_SESSION['ceramicComments'];
$stmt2->execute();

echo "New records created successfully in contexts spatial<br />";
}
catch(PDOException $e)
{
 echo "Error: " . $e->getMessage();
}

1 个答案:

答案 0 :(得分:3)

您正在执行两次查询:在之前之前,您将会话变量分配给您在查询中绑定的参数,并在之后。

您需要删除第一个$stmt2->execute();语句。