iOS xcode เชื่อมต่อกับ MySQL localhost ผ่าน XMAPP ไม่ได้คะ
ทำ Xcode ตามกระทู้นี้คะ https://www.thaicreate.com/mobile/ios-iphone-add-insert-data-to-web-server.html
แต่ว่าทำตามแล้วขึ้น
ERROR แบบนี้คะ ข้อมูลไม่ถูกใส่เข้าไปใน Database คะ
รูป MySQL คะ
>>>โค้ดของไฟล์ saveData.php คะ
Code
<?
/*** for Sample
$_POST["sName"] = "a";
$_POST["sTel"] = "b";
*/
$strName = $_POST["sName"];
$strTel = $_POST["sTel"];
$objConnect = mysql_connect("localhost","root","root");
$objDB = mysql_select_db("UserSupport");
/*** Insert ***/
$strSQL = "INSERT INTO member (Name,Tel)
VALUES (
'".$strName."',
'".$strTel."'
)
";
echo "sName";
$objQuery = mysql_query($strSQL);
$arr = null;
if(!$objQuery)
{
$arr["Status"] = "0";
$arr["Message"] = "Insert Data Failed";
}
else
{
$arr["Status"] = "1";
$arr["Message"] = "Insert Data Successfully";
}
echo json_encode($arr);
?>
>>>อันนี้โค้ดใน xCode ViewController.m คะ
Code
- (IBAction)btnSave:(id)sender {
//Name=Poo&Tel=023456789"
NSMutableString *post = [NSMutableString stringWithFormat:@"sName=%@&sTel=%@",[txtName text],[txtTEl text]];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSURL *url = [NSURL URLWithString:@"http://localhost/saveData.php"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
timeoutInterval:10.0];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:request delegate:self];
// Show Progress Loading...
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
loading = [[UIAlertView alloc] initWithTitle:@"" message:@"Please Wait..." delegate:nil cancelButtonTitle:nil otherButtonTitles:nil];
UIActivityIndicatorView *progress= [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(125, 50, 30, 30)];
progress.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
[loading addSubview:progress];
[progress startAnimating];
// [progress release];
[loading show];
if (theConnection) {
self.receivedData = [NSMutableData data];
} else {
UIAlertView *connectFailMessage = [[UIAlertView alloc] initWithTitle:@"NSURLConnection " message:@"Failed in viewDidLoad" delegate: self cancelButtonTitle:@"Ok" otherButtonTitles: nil];
[connectFailMessage show];
// [connectFailMessage release];
}
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[receivedData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
sleep(5);
[receivedData appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
// [connection release];
// [receivedData release];
// inform the user
UIAlertView *didFailWithErrorMessage = [[UIAlertView alloc] initWithTitle: @"NSURLConnection " message: @"didFailWithError" delegate: self cancelButtonTitle: @"Ok" otherButtonTitles: nil];
[didFailWithErrorMessage show];
//[didFailWithErrorMessage release];
//inform the user
NSLog(@"Connection failed! Error - %@", [error localizedDescription]);
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
// Hide Progress
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
[loading dismissWithClickedButtonIndex:0 animated:YES];
// Return Status E.g : { "Status":"1", "Message":"Insert Data Successfully" }
// 0 = Error
// 1 = Completed
if(receivedData)
{
//NSLog(@"%@",receivedData);
//NSString *dataString = [[NSString alloc] initWithData:receivedData encoding:NSASCIIStringEncoding];
//NSLog(@"%@",dataString);
id jsonObjects = [NSJSONSerialization JSONObjectWithData:receivedData options:NSJSONReadingMutableContainers error:nil];
// value in key name
NSString *strStatus = [jsonObjects objectForKey:@"Status"];
NSString *strMessage = [jsonObjects objectForKey:@"Message"];
NSLog(@"Status = %@",strStatus);
NSLog(@"Message = %@",strMessage);
// Completed
if( [strStatus isEqualToString:@"1"] ){
UIAlertView *completed =[[UIAlertView alloc]
initWithTitle:@": -) Completed!"
message:strMessage delegate:self
cancelButtonTitle:@"OK" otherButtonTitles: nil];
[completed show];
}
else // Error
{
UIAlertView *error =[[UIAlertView alloc]
initWithTitle:@": ( Error!"
message:strMessage delegate:self
cancelButtonTitle:@"OK" otherButtonTitles: nil];
[error show];
}
}
// release the connection, and the data object
// [connection release];
// [receivedData release];
}
รบกวนแนะนำหนูหน่อยค่า
ขอบคุณค่ะ ^^Tag : Mobile, MySQL, iOS, Objective-C, Mobile
Date :
2014-04-04 17:25:51
By :
gift9590
View :
2599
Reply :
10
พอเอาลิ้งไปวางช่อง address ก็ขึ้นตามรูปเลยคะ
หนูเอาไฟล์ savedata.php ไปไว้ใน Application > Xampp > htdocs คะ
Date :
2014-04-07 17:58:00
By :
gift9590
Error นี้มีวิธีแก้ไขเยอะครับ ค้นหาได้ในกระทู้เก่า ๆ ครับ (คือตอนนี้จะต้องแก้ไขบน php ให้มัน Save ได้ปกติก่อนครับ)
Date :
2014-04-07 18:22:10
By :
mr.win
ตอนนี้เหลือ อันนี้คะ
Warning: mysql_connect(): Access denied for user 'root'@'localhost' (using password: YES) in /Applications/XAMPP/xamppfiles/htdocs/saveData.php on line 9
{"Status":"0","Message":"Insert Data Failed"}
หนูไปเช็ค User กับ Pass ใน phpmyadmin แล้วก็เป็น root root
ตามที่เขียนในโค้ดแล้วอะคะ
พอมีวิธีแก้ไขอีกไหมคะ รบกวนด้วยค่าาา T---T
Date :
2014-04-07 19:16:17
By :
gift9590
ยังแก้ไขไม่ได้เลยค่ะ T----T
Date :
2014-04-22 23:53:41
By :
gift9590
แก้ไข Connection ของ PHP กับ MySQL ให้ถูกก็ได้แล้วครับ
Date :
2014-04-26 08:22:53
By :
mr.win
ติดเหมือนกันเลย แก้ยังไงก็ไม่ออกสักกะที
Date :
2016-09-20 13:12:35
By :
แบงค์
ในxcodeมันขึ้นแบบนี้อ่ะคับ พอเวลารันแล้วมันไม่เชื่อมให้อ่ะคับ
Date :
2016-09-20 13:16:27
By :
แบงค์
Load balance : Server 03