0

I have a contact form, that I've written in php, and posting the content to my database.

Its 4 labels – name, text, place and contact.

My PHP code looks like this:

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

What I do now, is that I have 4 UITextFields in Xcode:

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

And then I tried to use this code to post through the form in 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);
}

But the output in NSLog is:

<UITextField: 0x745f230; frame = (66 277; 187 30); text = 'fsdmfsfsf'; clipsToBounds = YES; opaque = NO; autoresize = RM+BM; gestureRecognizers = <NSArray: 0x7185a00>; layer = <CALayer: 0x745f3d0>>

Someone who can explain this?

tracifycray
  • 1,423
  • 4
  • 18
  • 29
  • Use of the `mysql_*` functions in PHP is discouraged in favour of [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) ([find out why](http://stackoverflow.com/questions/12859942)). – Till Helge Mar 15 '13 at 11:54
  • Sorry, I don't really understand... – tracifycray Mar 15 '13 at 12:06
  • So, I should do like this instead?: $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()); – tracifycray Mar 15 '13 at 12:16

1 Answers1

0

If I understand what you're trying to do then your two blocks of code don't seem to make sense. From your PHP you are trying to get the following variables:

  • name
  • text
  • place
  • contact

But in your XCode example you're posting 4 different variables to the URL:

  • who
  • what
  • where
  • contact

So the first thing to do would be to correct one or the other. I'll go for the PHP, so heres what I think you want to do (assuming you got your column names correct in the database):

 $value1 = $_GET['who'];
 $value2 = $_GET['what'];
 $value3 = $_GET['where'];
 $value4 = $_GET['contact'];

 $sql = "INSERT INTO content (`name`, `text`, `place`, `contact`) VALUES ('$value1',       '$value2', '$value3', '$value4')";

So at least now you're XCode/PHP makes sense (at least to me, this could be completely the wrong end of the stick for all I know).

EDIT: Just noticed that your PHP is using mysql_* functions which are becoming depreciated, you should use PDO or MySQLi.

Compy
  • 1,157
  • 1
  • 12
  • 24