简体   繁体   English

UITextField通过PHP发布到MySQL

[英]UITextField post via PHP to MySQL

I'm trying to post these 4 text-fields through a php-form and then insert it in my database. 我正在尝试通过php表单发布这4个文本字段,然后将其插入我的数据库中。

IBOutlet UITextField *who;
    IBOutlet UITextField *what;
    IBOutlet UITextField *where;
    IBOutlet UITextField *contact;

And the post method in Xcode is: Xcode中的post方法是:

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

My post.php file looks like this: 我的post.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['who'];
$value2 = $_REQUEST['what'];
$value3 = $_REQUEST['where'];
$value4 = $_REQUEST['contact'];

$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)."')") or die(mysql_error());


 if (!mysql_query($sql)) {
die('Error: ' . mysql_error());
}
    echo "You've just posted your text!";
mysql_close();
?>

But when I try to post from the app, nothing happens... 但是当我尝试从应用发布时,什么也没有发生...

Why is <?php header("Location: feed.php"); ?> 为什么<?php header("Location: feed.php"); ?> <?php header("Location: feed.php"); ?> at the top of your code? 代码顶部的<?php header("Location: feed.php"); ?> That will redirect you to another page. 这会将您重定向到另一个页面。

and

if (!mysql_query($sql)) {
  die('Error: ' . mysql_error());
}

executes the query a second time. 再次执行查询。 Do: 做:

if (!$sql) {
  die('Error: ' . mysql_error());
}

instead. 代替。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM