POST从iOS到MySQL数据库

时间:2014-10-19 19:52:52

标签: php ios mysql objective-c

我正在尝试将数据从我的UITextFields发布到MySQL数据库。控制台说我的连接成功,但由于某种原因数据没有发布到数据库?知道为什么?这是我的代码:

ViewController.h

-(IBAction)addParty:(id)sender;

@property (strong, nonatomic) IBOutlet UITextField *firstname;
@property (strong, nonatomic) IBOutlet UITextField *lastname;
@property (strong, nonatomic) IBOutlet UITextField *phone;
@property (strong, nonatomic) IBOutlet UITextField *email;
@property (strong, nonatomic) IBOutlet UITextField *guests;

@property (nonatomic, copy) NSDictionary *venueDetail;
@end

ViewController.m

- (IBAction)addParty:(id)sender
{

NSString *strURL = [NSString stringWithFormat:@"guestfirst=%@&guestlast=%@", firstname.text, lastname.text];

NSData *postData = [strURL dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d",[postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:@"http://myurl.com/phpfile.php"]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Current-Type"];
[request setHTTPBody:postData];


NSURLConnection *conn = [[NSURLConnection alloc]initWithRequest:request delegate:self];

if(conn)
{
    NSLog(@"Connection Successful");
}
else
{
    NSLog(@"Connection could not be made");
}

}

post.php中

<?Php
include('connect.php');

if (isset ($_POST["guestfirst"]) && isset ($_POST["guestlast"])){
    $guestfirst = $_POST["guestfirst"];
    $guestlast = $_POST["guestlast"];
} else {
    $guestfirst = "none";
    $guestlast = "none";
}

// Insert value into DB
$sql = "INSERT INTO events (guestfirst, guestlast) VALUES ('$guestfirst', '$guestlast');";
$res = mysql_query($sql,$conn) or die(mysql_error());

mysql_close($conn);

if($res) {          
$response = array('status' => '1');                 
} else {
die("Query failed");
}

echo json_encode($res);
exit();

?>

1 个答案:

答案 0 :(得分:1)

我目前的代码中存在一些问题。

  1. 当您提取现有文章时,您将查询分配给$get_events,但是从$events获取。
  2. 您在插入之前获取。 (也许那是你的意图?)
  3. 您输出多个JSON对象
  4. 您的查询使用$events而不是`events`
  5. 如果发生MySQL错误,则调用die(),因此您永远不会收到JSON响应,说明存在错误。
  6. 您正在使用旧的MySQL库并且您的脚本容易受到 SQL注入的攻击。<​​/ li>

    以下版本应修复除旧库以外的所有内容:

    connect.php

    <?php
    $conn = mysql_connect('localhost', 'root', 'root');
    mysql_select_db('testing');
    ?>
    

    post.php中

    <?php
    require_once("connect.php");
    
    if (isset ($_POST["guestfirst"]) && isset ($_POST["guestlast"]))
    {
        $guestfirst = mysql_real_escape_string($_POST["guestfirst"]);
        $guestlast = mysql_real_escape_string($_POST["guestlast"]);
    }
    else
    {
        $guestfirst = "No Entry";
        $guestlast = "none";
    }
    
    $sql = "INSERT INTO `events` (guestfirst, guestlast) VALUES ('$guestfirst', '$guestlast');";
    $res = mysql_query($sql, $conn) or $error = mysql_error();
    
    if($res)
    {
        $ret = array(
            'inserted' => true
        );
    }
    else
    {
        $ret = array(
            'inserted' => false,
            'error' => $error
        );
    }
    
    mysql_close($conn);
    echo json_encode($ret);
    exit();
    ?>
    

    list.php(或其他任何你称之为的)

    <?php
    require_once("connect.php");
    
    $get_events = mysql_query("SELECT * FROM `events`", $conn);
    $articles = array();
    
    while ($row = mysql_fetch_assoc($get_events))
    {
        $articles[] = $row;
    }
    
    mysql_close($conn);
    echo json_encode($articles);
    exit();
    ?>