通过ios = 0000-00-00从php插入日期到mysql

时间:2015-02-09 12:54:11

标签: php ios mysql insert

我正在尝试将我的iOS应用中的一些数据插入到使用phpmyadmin设置的数据库中。它工作得很好,我遇到的问题是一个条目总是得到时间戳0000-00-00。我试图更新此条目,但这种情况从未发生过。

这是我的代码:

-(void)update
{

NSURL *url = [NSURL URLWithString:@"xxxxxxxxxxxxxxxxxxxx.php"];

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];

// NSData *data=[NSData dataWithContentsOfURL:url];

NSString *authCredentials = [NSString stringWithFormat:@"%@:%@", @"xxxxxxxxx", @"xxxxxxxxxxx"];

NSString *authValue = [NSString stringWithFormat:@"Basic %@",[authCredentials base64EncodedStringWithWrapWidth:0]];

[request setValue:authValue forHTTPHeaderField:@"Authorization"];
[NSURLConnection sendAsynchronousRequest:request

 queue:[NSOperationQueue mainQueue]
 completionHandler:^(NSURLResponse * response,
 NSData * data,
 NSError * error){

[request setHTTPMethod:@"POST"];

NSUserDefaults*prefs=[NSUserDefaults standardUserDefaults];

NSString * a=[prefs objectForKey:@"user"];

NSString *b=[prefs objectForKey:@"expiration_date_full"];

NSString* c=[prefs objectForKey:@"expiration_date_werbung"];

NSString *noteDataString = [NSString    stringWithFormat:@"user=%@&expiration_date_full=%@&expiration_date_werbung=%@",    a,b,c];

                           [request setHTTPBody:[noteDataString dataUsingEncoding:NSUTF8StringEncoding]];

                          if (!error){

NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration     defaultSessionConfiguration];
 NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration:     defaultConfigObject delegate: nil delegateQueue: [NSOperationQueue mainQueue]];
                               NSURLSessionDataTask * dataTask =    [defaultSession dataTaskWithRequest:request completionHandler:^(NSData *dataRaw, NSURLResponse *header, NSError *error) {
                                   NSDictionary *json = [NSJSONSerialization
                                                         JSONObjectWithData:dataRaw
                                                         options:kNilOptions error:&error];
                                   NSString *status = json[@"status"];

                                   if([status isEqual:@"1"]){
                                       NSLog(@"upload erfolgt");
                                   } else {
                                       NSLog(@"upload failed");
                                   }
                               }];


 [dataTask resume];


  }

 }

];}

更新电话:

UserLoader *loader=[[UserLoader alloc] init]
[loader update];

NSLog(@"loader started");

.php文件:

<?php
$verbindung =     mysqli_connect("xxxxxxxxxxx","xxxxxxxxxxxx","xxxxxxxxxxxxxxxxx","v8.xxxxxxxxxxxx    xxxx");
if (mysqli_connect_errno()){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$user = ($_POST['user']);
$expiration_date_full = ($_POST['expiration_date_full']);
$expiration_date_werbung = ($_POST['expiration_date_werbung']);
if($stmt = mysqli_prepare($verbindung, "SELECT user FROM table user = ?")){
mysqli_stmt_bind_param($stmt, "s", $user);
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt, $havetoinput);
mysqli_stmt_fetch($stmt);}
mysqli_stmt_close($stmt);
}
if($havetoinput == ""){
if($stmt = mysqli_prepare($verbindung, "INSERT INTO table (user,     expiration_date_full, expiration_date_werbung) VALUES (?, ?, ?)")){
    mysqli_stmt_bind_param($stmt, "sss", $user, $expiration_date_full,     $expiration_date_werbung);
    mysqli_stmt_execute($stmt);
    mysqli_stmt_close($stmt);
}  
}else{
if($stmt = mysqli_prepare($verbindung, "UPDATE table SET     expiration_date_full = ?, expiration_date_werbung = ? WHERE user = ?")){
    mysqli_stmt_bind_param($stmt, "sss", $expiration_date_full,     $expiration_date_werbung, $user);
    mysqli_stmt_execute($stmt);
    mysqli_stmt_close($stmt);
}        
}
mysqli_close($verbindung);
?>

结果: enter image description here

1 个答案:

答案 0 :(得分:2)

这取决于你的数据库结构,但我认为当你在用户密钥上绑定params时,它应该是i + ss中的i,并确保列类型是如果你想在日期时间列中插入一个字符串,它可以用其他方式格式化,使用STR_TO_DATE(),然后再转换为datetime

UPDATE table SET expiration_date_full = STR_TO_DATE(?, '%Y-%m-%d'), expiration_date_werbung = STR_TO_DATE(?, '%Y-%m-%d') WHERE user = ?"