-2
我試圖用JSON格式插入數據到MySQL,但它不工作。數據不會被插入到數據庫中。你能告訴我做錯了嗎?這是我的PHP文件:iOS插入JSON到MySQL
<?php
$con = mysql_connect("http://mydomain", "dbUserName", "password");
if(!$con)
{
die('Connect fail', mysql_error());
}
mysql_select_db("dbName", $con);
if(isset($_REQUEST['data']))
{
$json = $_REQUEST['data'];
$data = json_decode($json);
mysql_query("INSERT INTO tbAccount
(AccFullName, AccAboutMe)
VALUES ('$data->AccFullName', '$data->AccAboutMe')");
}
$con->exec($mysql_query);
echo "New record created successfully";
mysql_close($con);?>
和實施.M在應用程序中的OBJ-C:
- (IBAction)insert:(id)sender;{NSMutableString *postString = [NSMutableString stringWithString:kPostURL];
NSString *jsonString = [[NSString alloc] initWithFormat:@"{\"AccFullName\":\"%@\", \"AccAboutMe\":\"%@\"}", self.name.text, aboutMe.text];
[postString appendString:[NSString stringWithFormat:@"?data=%@", jsonString]];
[postString setString:[postString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:postString]];
[request setHTTPMethod:@"POST"];
postConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
NSLog(@"Post string %@", postString);}
你混合mysql_與mysqli_來電或PDO調用(不太清楚它)**是不會工作** – RiggsFolly
因此,在應用程序是一切就OK了,對不對?只有我的PHP文件不正確?我正在使用本教程:https://www.youtube.com/watch?v = v1Jdrit4PfE,並且數據完全插入到mysql中。 – kenzolek
不知道應用程序,PHP是一團糟 – RiggsFolly