0
我有一個聯繫表單,我用php編寫,並將內容發佈到我的數據庫。PHP通過UITextFields發佈到MySQL
它的4個標籤 - 名稱,文本,地點和聯繫人。
我的PHP代碼如下所示:
<?php header("Location: feed.php"); ?>
<?php
define ('DB_NAME','database_name');
define ('DB_USER','user');
define ('DB_PASSWORD','root');
define ('DB_HOST','localhost');
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if (!link) {
die('Could not connect: ' .mysql_error());
}
$db_selected = mysql_select_db(DB_NAME, $link);
if (!$db_selected) {
die('Can\'t use ' . DB_NAME . ': ' . mysql_error());
}
$value1 = $_REQUEST['name'];
$value2 = $_REQUEST['text'];
$value3 = $_REQUEST['place'];
$value4 = $_REQUEST['contact'];
$sql = "INSERT INTO content (`name`, `text`, `place`, `contact`) VALUES ('$value1', '$value2', '$value3', '$value4')";
if (!mysql_query($sql)) {
die('Error: ' . mysql_error());
}
echo "You've just posted your text!";
mysql_close();
?>
我現在要做的是,我有4個UITextFields在Xcode:
IBOutlet UITextField *who;
IBOutlet UITextField *what;
IBOutlet UITextField *where;
IBOutlet UITextField *contact;
然後我嘗試使用此代碼後通過在Xcode形式:
- (IBAction)post:(id)sender
{
NSLog(@"%@", who);
NSLog(@"%@", what);
NSLog(@"%@", where);
NSLog(@"%@", contact);
// create string contains url address for php file, the file name is post.php, it receives parameter :name
NSString *strURL = [NSString stringWithFormat:@"http://website.com/post.php?who=%@&what=%@&where=%@&contact=%@",who, what, where, contact];
// to execute php code
NSData *dataURL = [NSData dataWithContentsOfURL:[NSURL URLWithString:strURL]];
// to receive the returend value
NSString *strResult = [[NSString alloc] initWithData:dataURL encoding:NSUTF8StringEncoding];
NSLog(@"%@", strResult);
}
但在NSLog的輸出爲:
<UITextField: 0x745f230; frame = (66 277; 187 30); text = 'fsdmfsfsf'; clipsToBounds = YES; opaque = NO; autoresize = RM+BM; gestureRecognizers = <NSArray: 0x7185a00>; layer = <CALayer: 0x745f3d0>>
有人能解釋這個嗎?
在PHP中使用'mysql_ *'函數不鼓勵使用[PDO](http://php.net/pdo)或[MySQLi](http://php.net/mysqli)([find爲什麼](http://stackoverflow.com/questions/12859942))。 – 2013-03-15 11:54:42
對不起,我真的不明白... – tracifycray 2013-03-15 12:06:59
所以,我應該這樣做呢?:$ sql = mysql_query(「INSERT INTO content VALUES('','」。mysql_real_escape_string($ value1)。'','' 「.mysql_real_escape_string($ value2)。」','「.mysql_real_escape_string($ value3)。」','「。mysql_real_escape_string($ value4)。」')「)或die(mysql_error()); – tracifycray 2013-03-15 12:16:31