对不起,这是我在这里的第一个问题。我正在做一个制作应用程序的学校项目,并且在使用 sql php 连接时遇到了困难。
它将空白张贴到数据库中。
我很抱歉格式。我没有包含整个代码,只包含我认为会影响我的结果的区域。
这是我的代码:
预订.h
#define kPostURL @"http://XXX/app/quickbooking.php"
#define kFirstname @"firstname"
#define kLastname @"lastname"
#define kContactno @"contactno"
#define kEmail @"email"
#define kClub @"club"
#define kDatetime @"datetime"
@interface TellUs : UIViewController <UINavigationControllerDelegate, UIActionSheetDelegate, UITextFieldDelegate,UIPickerViewDataSource, UIPickerViewDelegate,UITextFieldDelegate, SKPSMTPMessageDelegate>{
IBOutlet UITextField *messageText;
IBOutlet UITextField *messageTextName;
IBOutlet UITextField *messageTextLastName;
IBOutlet UITextField *messageTextEmail;
IBOutlet UIButton *doneEditingButton;
IBOutlet UIButton *sendMail;
IBOutlet UILabel *feedbackMsg;
IBOutlet UIDatePicker *myDatePicker;
IBOutlet UITextField *yourTextField;
IBOutlet UIToolbar *toolBar;
IBOutlet UIBarButtonItem *barButton;
IBOutlet UISegmentedControl *segmentedControl;
IBOutlet UILabel *myDatePickerLabel;
IBOutlet UILabel *myPickerLabel;
IBOutlet UIPickerView *myPicker;
IBOutlet UITextField *yourTextFieldPicker;
IBOutlet UIButton *tellUsWhatYouThink;
NSArray *dataSource;
NSArray *fieldsArray;
CGFloat animatedDistance;
IBOutlet UIButton *postBooking;
NSURLConnection *postConnection;
}
@property (nonatomic, retain) UITextField *messageText;
@property (nonatomic, retain) UITextField *messageTextName;
@property (nonatomic, retain) UITextField *messageTextLastName;
@property (nonatomic, retain) UITextField *messageTextEmail;
@property (nonatomic, retain) UIButton *doneEditingButton;
@property (nonatomic, retain) UIButton *sendMail;
@property (nonatomic, retain) UILabel *feedbackMsg;
@property (nonatomic, retain) UISegmentedControl *segmentedControl;
@property (nonatomic, retain) UIButton *tellUsWhatYouThink;
@property (nonatomic, retain) UIDatePicker *myDatePicker;
@property (nonatomic, retain) UITextField *yourTextField;
@property (nonatomic, retain) UILabel *myDatePickerLabel;
@property (nonatomic, retain) UILabel *myPickerLabel;
@property (nonatomic, retain) UIPickerView *myPicker;
@property (nonatomic, retain) NSArray *dataSource;
@property (nonatomic, retain) UITextField *yourTextFieldPicker;
@property (nonatomic, retain) IBOutlet UITextView *textView;
@property (nonatomic, retain) UIButton *postBooking;
-(void) postFirstname:(NSString *)firstname withLastname:(NSString *)lastname withContactno:(NSString *)contactno withEmail:(NSString *)email withClub:(NSString *)club withDatetime:(NSString *)datetime;
- (IBAction)postBooking:(id)sender;
@end
预订.m
@synthesize feedbackMsg;
@synthesize tellUsWhatYouThink;
@synthesize sendMail;
@synthesize doneEditingButton;
@synthesize messageText;
@synthesize messageTextName;
@synthesize messageTextLastName;
@synthesize messageTextEmail;
@synthesize myDatePicker, yourTextField, segmentedControl;
@synthesize myDatePickerLabel;
@synthesize myPickerLabel, myPicker, dataSource;
@synthesize yourTextFieldPicker;
@synthesize textView;
@synthesize postBooking;
-(void) postFirstname:(NSString *)firstname withLastname:(NSString *)lastname withContactno:(NSString *)contactno withEmail:(NSString *)email withClub:(NSString *)club withDatetime:(NSString *)datetime {
if (firstname != nil && lastname != nil && contactno != nil
&& email != nil && club != nil && datetime != nil){
NSMutableString *postString = [NSMutableString stringWithString:kPostURL];
[postString appendString:[NSString stringWithFormat:@"?%@=%@", kFirstname, firstname]];
[postString appendString:[NSString stringWithFormat:@"&%@=%@", kLastname, lastname]];
[postString appendString:[NSString stringWithFormat:@"&%@=%@", kContactno, contactno]];
[postString appendString:[NSString stringWithFormat:@"&%@=%@", kEmail, email]];
[postString appendString:[NSString stringWithFormat:@"&%@=%@", kClub, club]];
[postString appendString:[NSString stringWithFormat:@"&%@=%@", kDatetime, datetime]];
[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];
}
}
-(IBAction)postBooking:(id)sender{
[self postFirstname: messageTextName.text withLastname: messageTextLastName.text withContactno: messageText.text withEmail: messageTextEmail.text withClub: myPickerLabel.text withDatetime: myDatePickerLabel.text];
[messageTextName resignFirstResponder];
messageTextName.text = nil;
messageTextLastName.text = nil;
messageText.text = nil;
messageTextEmail.text = nil;
myPickerLabel.text = nil;
myDatePickerLabel.text = nil;
[self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void)viewDidUnload {
}
- (void)dealloc {
[segmentedControl release];
[myDatePickerLabel release];
[myPickerLabel release];
[myPicker release];
[dataSource release];
[super dealloc];
[sendMail release];
[messageText release];
[doneEditingButton release];
[fieldsArray release];
[feedbackMsg release];
[tellUsWhatYouThink release];
[messageTextName release];
[messageTextLastName release];
[messageTextEmail release];
[yourTextField release];
[yourTextFieldPicker release];
[postBooking release];
}
@end
PHP 文件:
<?php
session_start();
include("connectdb.php");
$first_name = $_GET['messageTextName'];
$last_name = $_GET['messageTextLastName'];
$contactno = $_GET['messageText'];
$email = $_GET['messageTextEmail'];
$club = $_GET['myPickerLabel'];
$date_time = $_GET['myDatePickerLabel'];
$query = "INSERT INTO booking VALUES ('', '$first_name', '$last_name', '$contactno', '$email', '$club', '$date_time')";
mysql_query($query) or die (mysql_error("ERROR"));
mysql_close();
?>