dtcrw26206
2014-10-19 19:52
浏览 56
已采纳

POST从iOS到MySQL数据库

I'm trying to post data from my UITextFields to a MySQL database. The console says my connection is successful, but for some reason the data isn't posting to the database? Any idea as to why? Here's my code:

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();

?>

图片转代码服务由CSDN问答提供 功能建议

我正在尝试将数据从我的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  ,非原子的)IBOutlet UITextField * email; 
 @ property(strong,nonatomic)IBOutlet UITextField * guests; 
 
 @ property(nonatomic,copy)NSDictionary * venueDetail; 
 @ end 
   
 
 

ViewController.m

   - (IBAction)addParty:(id)sender 
 {
 
NSNString *  strURL = [NSString stringWithFormat:@“guestfirst =%@&amp; guestlast =%@”,firstname.text,lastname.text]; 
 
NSData * postData = [strURL dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; 
NSString * postLength  = [NSString stringWithFormat:@“%d”,[postData length]]; 
NSMutableURLRequest * request = [[NSMutableURLRequest alloc] init]; 
 [请求setURL:[NSURL URLWithString:@“http:// my  url.com/phpfile.php"]];
[request setHTTPMethod:@“POST”]; 
 [请求setValue:postLength forHTTPHeaderField:@“Content-Length”]; 
 [请求setValue:@“application /  x-www-form-urlencoded“forHTTPHeaderField:@”Current-Type“]; 
 [请求setHTTPBody:postData]; 
 
 
NSURLConnection * conn = [[NSURLConnection alloc] initWithRequest:request delegate:self]; \  n 
if(conn)
 {
 NSLog(@“连接成功”); 
} 
else 
 {
 NSLog(@“无法建立连接”); 
} 
 
}  
   
 
 

<强> post.php中

 <代码>&LT;腓
include('连接 .php'); 
 
if(isset($ _POST [“guestfirst”])&amp;&amp;  isset($ _POST [“guestlast”])){
 $ guestfirst = $ _POST [“guestfirst”]; 
 $ guestlast = $ _POST [“guestlast”]; 
} else {
 $ guestfirst =“none  “; 
 $ guestlast =”none“; 
} 
 
 //将值插入数据库
 $ sql =”INSERT INTO事件(guestfirst,guestlast)VALUES('$ guestfirst','$ guestlast')  ;“; 
 $ res = mysql_query($ sql,$ conn)或die(mysql_error()); 
 
mysql_close($ conn); 
 
if($ res){
 $ response = array('  status'=&gt;'1');  
} else {
die(“查询失败”); 
} 
 
echo json_encode($ res); 
exit(); 
 
?&gt; 
   \  n 
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongshao8566 2014-10-19 20:05
    已采纳

    There are a few problems I see with your current code.

    1. When you're fetching existing articles, you are assigning the query to $get_events, but fetching from $events.
    2. You're fetching before your insert. (maybe that's your intent?)
    3. You output more than one JSON object
    4. Your query uses $events rather than `events`.
    5. You call die() if a MySQL error occurs, so you will never receive a JSON response saying there was an error.
    6. You are using an old MySQL library AND your script is vulnerable to SQL injection.

    Below is a version that should fix all of these except for the old library:

    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 (or whatever else you called it)

    <?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();
    ?>
    
    点赞 打赏 评论

相关推荐 更多相似问题