如何将值传递给phpmyadmin数据库?

时间:2012-09-15 14:32:04

标签: php iphone objective-c xcode phpmyadmin

作为标题

这是我的代码

h.file

#define kPostURL @"http://localhost/discuss.php"
#define kName @"name"
#define kMessage @"message"
#define kNum @"num"

@interface TestDisViewController : UIViewController{

IBOutlet UITextField *nameText;
IBOutlet UITextView *messageText;
NSURLConnection *postConnection;

UILabel *bbb;
}

@property(strong, nonatomic)IBOutlet UILabel *bbb;
-(IBAction)post:(id)sender;
@property(weak) NSString *aaa;

@end

和m.file

“aaa”是另一个视图控制器

的字符串
#import "TestDisViewController.h"
#import "EatDiscussViewController.h"
@implementation TestDisViewController

@synthesize aaa;
@synthesize bbb =bbb;


-(void) postMessage:(NSString*) message Name:(NSString *) name withNum:(NSString *) num{


if (name != nil && message != nil  && num != nil){

    NSMutableString *postString = [NSMutableString stringWithString:kPostURL];
    [postString appendString:[NSString stringWithFormat:@"?%@=%@", kName, name]];
    [postString appendString:[NSString stringWithFormat:@"&%@=%@", kMessage, message]];
    [postString appendString:[NSString stringWithFormat:@"#%@=%@", kNum, num]];
    [postString setString:[postString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:postString]];
    [request setHTTPMethod:@"POST"];
    postConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];

}

- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(@"Taaaaaaaaab:%@",aaa);
self.bbb.text=aaa;
}

}

-(IBAction)post:(id)sender{

[self postMessage:messageText.text Name:nameText.text withNum:bbb.text];
[messageText resignFirstResponder];
messageText.text = nil;
nameText.text = nil;
bbb.text=nil;
[self.navigationController popViewControllerAnimated:YES];

}

@end

我删除了一些不需要使用的代码〜

最后我的php.code


@mysql_select_db($database) or die("Error");

$user_name = $_GET["name"];

$commentary = $_GET["message"];

$stores_num = $_GET["num"];

$query = "INSERT INTO comment VALUES ('', '$stores_num' , '$user_name' , '$commentary')";

mysql_query("SET NAMES 'utf8'");

mysql_query($query) or die (mysql_error("error"));

mysql_close();

我可以将两个值传递给数据库

但我无法通过三个值........

请帮助我!!

2 个答案:

答案 0 :(得分:1)

错误:[postString appendString:[NSString stringWithFormat:@“#%@ =%@”,kNum,num]];

修复:[postString appendString:[NSString stringWithFormat:@“&%@ =%@”,kNum,num]];

答案 1 :(得分:0)

$user_name = $_GET["name"]; 
$commentary = $_GET["message"]; 
$stores_num = $_GET["num"]; 

$query = "INSERT INTO comment VALUES ('', '$stores_num' , '$user_name' , '$commentary')"

如果您没有列出查询中的字段,则不一定按照您预期的顺序添加;尝试显式添加字段名称:

$query = "INSERT INTO comment (num, stores_num, user_name, commentary) VALUES ('', '$stores_num' , '$user_name' , '$commentary')"

如果num是一个自动增量字段,您甚至不需要:

$query = "INSERT INTO comment (stores_num, user_name, commentary) VALUES ('$stores_num' , '$user_name' , '$commentary')"

<强>无论其

您正在使用mysql_*函数,这些函数已被弃用。您应该考虑转移到PDOmysqli_ - 它们都会帮助您编写更安全的代码。

相关问题