doubu2730 2017-04-16 16:48
浏览 50
已采纳

使用JSON / PHP将DATETIME插入到mysql数据库中

I have a database that contains an DATETIME column, I want to insert current date into it using JSON/PHP, but I kept having this error :

Object of class DateTime could not be converted to string in /storage/h3/744/754744/public_html/SendBookingReq.php on line 13 And this is my php file

<?php
require("password.php");
$connect = mysqli_connect("localhost", XXXX, XXXX, XXXX);
$driver_id = $_POST["driver_id"];
$email = $_POST["email"];
$duree = $_POST["duree"];
$distance = $_POST["distance"];
$response = array();
$dt_obj = new DateTime($response['DateTime'], new 
DateTimeZone('America/Chicago')); 
$dt_obj->setTimezone(new DateTimeZone('Europe/Paris')); 
$dt_obj->format('d-m-Y H:i:s');
echo $dt_obj; 
 function AddRequest() {
    global $connect, $driver_id, $email, $duree, $distance, $dt_obj ;
    $statement = mysqli_prepare($connect, "INSERT INTO demande (driver_id, pass_id, duree, distance, send_moment) VALUES (?, (SELECT user_id FROM passager WHERE email = ?), ?, ?, '$dt_obj')");
    mysqli_stmt_bind_param($statement, "isdis", $driver_id, $email, $duree, $distance,$dt_obj);
    mysqli_stmt_execute($statement);
    mysqli_stmt_close($statement);     
}

    $response["success"] = false;  

    AddRequest();
    $response["success"] = true;  

echo json_encode($response);
 ?>

And honestly ,I looked a lot for what letter should I use with datetime ex: "s" for string but I didn't find any.

  • 写回答

2条回答 默认 最新

  • doudu5029 2017-04-16 17:20
    关注

    Error

    Object of class DateTime could not be converted to string in /storage/h3/744/754744/public_html/SendBookingReq.php on line 13

    This error occurs because of echo $dt_obj on line 13, but echo outputs one or more strings. You may want to use var_dump($dt_obj) or print_r($dt_obj) instead.

    EDITED AFTERWARDS

    <?php
    require("password.php");
    $connect = mysqli_connect("localhost", XXXX, XXXX, XXXX);
    $driver_id = $_POST["driver_id"];
    $email = $_POST["email"];
    $duree = $_POST["duree"];
    $distance = $_POST["distance"];
    $response = array();
    $dt_obj = new DateTime($response['DateTime'], new 
    DateTimeZone('America/Chicago')); 
    $dt_obj->setTimezone(new DateTimeZone('Europe/Paris'));
    
    /* create a DATETIME string to use in mysqli_stmt_bind_param() in line 24
    *  https://dev.mysql.com/doc/refman/5.5/en/date-and-time-literals.html */ 
    $send_time = $dt_obj->format('Y-d-m H:i:s');
    
    function AddRequest() {
        global $connect, $driver_id, $email, $duree, $distance, $send_time ;
        echo($send_time);
        /* proper number of parametres (?) according to the db table's number of columns (5) */
        $statement = mysqli_prepare($connect, "INSERT INTO demande (driver_id, pass_id, duree, distance, send_moment) 
                                               VALUES (?, (SELECT user_id FROM passager WHERE email = ?), ?, ?, ?)");
    
        /* http://php.net/manual/en/mysqli-stmt.bind-param.php */
        mysqli_stmt_bind_param($statement, "isdis", $driver_id, $email, $duree, $distance,$send_time);
        mysqli_stmt_execute($statement);
        mysqli_stmt_close($statement);     
    }
    
    $response["success"] = false;  
    
    AddRequest();
    $response["success"] = true;  
    
    echo json_encode($response);
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?