0

我正在尝试将数据从 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);

}
}

我在 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 表中。我的代码有什么问题?提前致谢

4

1 回答 1

0

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

于 2012-11-04T20:11:44.030 回答