我對objective-c相當陌生,並且希望使用POST將一些鍵值對傳遞給PHP腳本。我使用下面的代碼,但數據似乎並沒有通過發佈。我也嘗試通過使用NSData發送內容,但都沒有工作。使用NSURLRequest通過POST將鍵值對傳遞給PHP腳本
NSDictionary* data = [NSDictionary dictionaryWithObjectsAndKeys:
@"bob", @"sender",
@"aaron", @"rcpt",
@"hi there", @"message",
nil];
NSURL *url = [NSURL URLWithString:@"http://myserver.com/script.php"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:[NSData dataWithBytes:data length:[data count]]];
NSURLResponse *response;
NSError *err;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
NSLog(@"responseData: %@", content);
這是越來越發送到這個簡單的腳本來執行數據庫插入:
<?php $sender = $_POST['sender'];
$rcpt = $_POST['rcpt'];
$message = $_POST['message'];
//script variables
include ("vars.php");
$con = mysql_connect($host, $user, $pass);
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("mydb", $con);
mysql_query("INSERT INTO php_test (SENDER, RCPT, MESSAGE)
VALUES ($sender, $rcpt, $message)");
echo "complete"
?>
任何想法?
感謝pinkgothic,我現在報價收納我的INSERT語句,但它是隻增加空值到數據庫。我想這意味着價值觀仍然沒有通過正確的方式發佈? – 2010-03-30 11:29:46