发送ASIFormDataRequest请求

时间:2012-10-29 21:26:56

标签: iphone mysql ipad asiformdatarequest

我正在尝试将数据从iPhone发送到php文件以在mySql中插入一些数据

在iPhone中

我编写了以下代码:

-(IBAction) SendToServer{

NSString *phpUrl = @"http://mamdouhsaleh.com/myFolder/RegisteredPersons.php";

NSString *dbName = @"my_dbName";
NSString *localHost = @"localhost";
NSString *dbUser = @"my_userName";
NSString *dbPwd = @"my_password";

NSString *PersonName2 = personName.text;
NSString *CourseTitle2 = courseTitle.text;
NSString *MobileNo2 = mobileNo.text;
NSString *Email2 = email.text;


NSURL *link = [NSURL URLWithString:[phpUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:link];

[request setRequestMethod:@"POST"];
[request setPostValue:dbName forKey:@"dbName"];
[request setPostValue:localHost forKey:@"localHost"];
[request setPostValue:dbUser forKey:@"dbUser"];
[request setPostValue:dbPwd forKey:@"dbPwd"];
[request setPostValue:@"" forKey:@"submit"];

[request setPostValue:PersonName2 forKey:@"PersonName"];
[request setPostValue:CourseTitle2 forKey:@"CourseTitle"];
[request setPostValue:MobileNo2 forKey:@"MobileNo"];
[request setPostValue:Email2 forKey:@"Email"];

[request setTimeOutSeconds:120];
[request setDelegate:self];
NSError *error = [request error];
[request startAsynchronous];

NSDictionary *questions = nil;
if (!error) {
    NSData *response = [request responseData];
    NSString *json = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
    questions = [json objectFromJSONString];
    NSLog(@"Data: %@", questions);

}
}

和im RegisteredPersons.php我写道:

<?php

if (isset($_POST['submit'])) {

$dbName = $_POST['dbName'];
$localHost = $_POST['localHost'];
$dbUser = $_POST['dbUser'];
$dbPwd = $_POST['dbPwd'];

$con = mysql_connect($localHost,$dbUser,$dbPwd);
$db_found = mysql_select_db($dbName);

mysql_query('SET CHARACTER SET UTF8');
mysql_query("SET NAMES utf8; ");

$myPageId = mysql_query("SELECT MAX(ID) FROM RegisteredPersons");
$row = mysql_fetch_row($myPageId);

$personId = $row[0]++;
$PersonName = $_POST['PersonName']; 
$CourseTitle = $_POST['CourseTitle'];
$MobileNo = $_POST['MobileNo']; 
$Email = $_POST['Email']; 

mysql_query("INSERT INTO RegisteredPersons VALUES (". $personId . ", '" . $PersonName . "', '" . $CourseTitle . "', " . $MobileNo . ", '" . $Email . "')");


mysql_close();

}

?>

但是在RegisteredPersons表中不会发生插入。我的代码有什么问题? 在此先感谢

1 个答案:

答案 0 :(得分:0)

我通过更换解决了这个问题      $ personId = $ row [0] ++; 通过      $ personId = $ row [0] +1;