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.

图片转代码服务由CSDN问答提供 功能建议

我有一个包含DATETIME列的数据库,我想使用JSON / PHP将当前日期插入其中,但是 我一直有这个错误:

第13行的/storage/h3/744/754744/public_html/SendBookingReq.php中无法将类DateTime的对象转换为字符串 这是我的php文件

 &lt;?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-&gt; setTimezone(new DateTimeZone('Europe / Paris'));  
 $ dt_obj-&gt;格式('d-m-Y H:i:s'); 
echo $ dt_obj;  
函数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); 
?&gt; 
   
 
 

老实说,我看了很多关于我应该用datetime ex写的字母:“ s“for string但我找不到。

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

2条回答 默认 最新

相关推荐 更多相似问题