My SQL中的日期总是有效

时间:2013-03-11 17:33:09

标签: php mysql json

我一直在撕扯我的头发并牺牲了众神 - 并且做了我可以做的功课我会遇到与其他两个帖子相同的问题,这两个帖子似乎都没有解决它们

Properly formatted MySQL date insert statement returns all 0's

MYSQL Date field always outputs 0000-00-00

当我回显我得到格式正确的值我正在使用日期而不是DATETIME MySQL在日期之前正确输入了一个名称,但随后又输入了一个空白的长斑(图片) - 它似乎只是进入一个字段或某种方式我没有正确设置MySQL或其他东西?

我的代码是

catch(PDOException $e)
    {
    echo "Im sorry dave I cant do that";
    echo $e->getMessage();
    }

        //*next bit is to insure that if connection is lost database is not  
   partially updated-I think-
        //* $DBHandle->beginTransaction();

        $firstnameOBS= $_POST['touristfirstname'];
        $todaysdateOBS= $_POST['touristdatetoday'];
        $picturenow= $_POST['picturesubmitted'];

        $JSONfirstname = json_encode($firstnameOBS);
        $JSONtodaysdate = json_encode($todaysdateOBS);
        $JSONpicturenow = json_encode($picturenow);
        echo $JSONtodaysdate ;

        //* Below is the send from PHP page to My Sql Server -
                //*  JSON encode here 

    try {    
        $senditin = $DBHandle->prepare("INSERT INTO 
    `Observations`.`fkarnd`(`firstname`,`datetoday`,`picturesubmitted`) VALUES   
 (:firstname 
    , :datetoday ,:picturesubmitted)", array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));

        //* $senditin->bindValue(':firstname', $JSONfirstname, PDO::PARAM_STR);
        //* $senditin->bindValue(':datetoday', $JSONtodaysdate, PDO::PARAM_STR);
        //* $senditin->bindValue(':picturesubmitted', $JSONpicturenow,
     PDO::PARAM_LOB);

        //* $myinputarray = array('firstname'=> $JSONfirstname, 'datetoday' => 
    $JSONtodaysdate, 'picturesubmitted' => $JSONpicturenow );

        $DBHandle->beginTransaction();
        $senditin->execute(array('firstname'=> $JSONfirstname, 'datetoday' => 
    $JSONtodaysdate, 'picturesubmitted' => $JSONpicturenow ));
        //* commit allows transaction begun to complete
        $DBHandle->commit();

     }  

    catch ( PDOException $e ) {
      echo "I'm sorry, I can't do that Dave......";
      file_put_contents( 'dbErrors.txt', $e->getMessage(), FILE_APPEND );   
        //* rollback function call here? a nasty exception has appeared,      
     }
        echo "<br>"."successful submission";
        $DBHandle = null;

     ?>

1 个答案:

答案 0 :(得分:2)

你的意思是调用json_decode($ todaysdateOBS)吗?看起来这可能是问题的根源,因为json_encode将尝试以json格式编码,无论你发送给它,它都不是sql(http://php.net/manual/en/function.json-encode.php)中的有效日期字段。