0

I have this line of code in my iOS app that attempts to insert data into my mysql database:

NSString *strURL = [NSString stringWithFormat:@"http://example.org/postSqueal.php?thePost=%@&byUserID=%@&nickname=%@", self.textView.text, [UniqueUserIdentification getUserID], nickname];
NSData *dataURL = [NSData dataWithContentsOfURL:[NSURL URLWithString:strURL]];

Basically I am getting the user to type in a bunch of stuff into a UITextView (like a twitter tweet) for example, and when they press "post" button it is supposed to store this data in my database along with some other information like their nickname. My code works perfectly fine until I started entering spaces. Then my code would not insert to the database. I found out this is because it would mean a space in the url which is not a valid url, so by this I concluded that I need to somehow surround the text view text with quotation marks in my url. I tried putting quotation marks like the following:

NSString *strURL = [NSString stringWithFormat:@"http://example.org/postSqueal.php?thePost=\"%@\"&byUserID=%@&nickname=%@", self.textView.text, [UniqueUserIdentification getUserID], nickname];
    NSData *dataURL = [NSData dataWithContentsOfURL:[NSURL URLWithString:strURL]];

However it still does not work. Anyone know what might be wrong? When I manually type out the url with quotation marks around text with spaces it works, but in my code when I try to add quotation marks like that, the insert doesn't work. Also is there any other characters I might have to worry about giving me trouble when converting it into the url?

Here is my php script:

<?php

$con=mysqli_connect("editout","editout","editout","editout");

if (mysqli_connect_errno())
{
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$thePost = $_GET["thePost"];
$byUserID = $_GET["byUserID"];
$nickname = $_GET["nickname"];

mysqli_query($con,"INSERT INTO myTable
VALUES ('$thePost', '$byUserID', '$nickname', 0, 0)");

mysqli_close($con);

?>
JohnyFonno
  • 31
  • 6
  • i's worth considering that if you just "use JSON" it's all done for you. Hers's some full, total example code for sending JSON to a server. It's one of those things that is "much easier once you do it that way" http://stackoverflow.com/a/26094744/294884 – Fattie Oct 27 '14 at 10:54
  • Ok thank you Joe i'll check it out. – JohnyFonno Oct 27 '14 at 11:11

2 Answers2

0

URL encode the query parameters. See this question for more information for Objective-C. urlencode is the function to encode strings in PHP.

Community
  • 1
  • 1
Elias
  • 1,532
  • 8
  • 19
0

You have two options:

  1. Url encode your strings on iOS side and then url decode on php side before entering
  2. Change the HTTP method to POST as opposed to GET which would preserve the spaces as intended.
Community
  • 1
  • 1
dnshio
  • 914
  • 1
  • 8
  • 21
  • I did just this and everything except for the single quotation mark ' appears to be working. The spaces work now, the double quotation marks work now ("), but for some reason the single quotation mark (') is not working :S – JohnyFonno Oct 27 '14 at 11:15
  • You should use the php function addslashes [1] to escape single quotes before entering the data in to the db and then use stripslashes [2] when retrieving the data from the database. [1] http://php.net/manual/en/function.addslashes.php [2] http://nl3.php.net/manual/en/function.stripslashes.php – dnshio Oct 27 '14 at 12:17
  • There is nothing I can do on the iOS side to fix it? Literally every character except the single quotation is working :S In the answers I don't see anyone complaining about it so it makes me think I am doing something wrong :S – JohnyFonno Oct 27 '14 at 14:08