Android Question PHP crashes if JSON string contains Ampersand

mangojack

Well-Known Member
Licensed User
Hi .. I have been trying to solve this for two days now .. I have read many threads here and php forums and tried many suggestions .. (encode, decodes .. StringUtils) but nothing is working .. so any help would be welcome.

I am sending a JSON string with Http.Poststring to PHP script to Insert data to mySQL.
If any portion / field of the string contains an "&" ampersand the php fails.

In the end I am try to store a complete URL .. but even a simple title "Abc&123" will cause it to fail.
My Lack of PHP expertise doesn't help either ..:oops: Is there a simple way of sending an array containing a URL and storing it in a mySQL db or is there more to it that that.

Regards

PHP
B4X:
include ("config.php");
$con = mysql_connect($host,$user,$pw) or die(mysql_error());
mysql_select_db($db) or die(mysql_error());
mysql_query("SET CHARACTER SET utf8");

{       
     
   $json = $_POST["MyJSON"];
  $jsall = array();
  $jsone = array();
  $jsall=json_decode($json, true);
  $x = 0;
  while($x < count($jsall)) {
     $jsone=$jsall[$x];
   
     $id=$jsone["ID"];
     $title=$jsone["Title"];
     $url=$jsone["Url"];

  //$url = filter_var($jsone["Url"], FILTER_SANITIZE_URL);
   
  //$nurl=htmlspecialchars($url);

  echo $url;

       
  //$q = mysql_query("INSERT INTO test_tbl(table_id, title, url) VALUES($id, '$title', '$nurl')");
               
  $x++;
  }
         
     print json_encode("Inserted Data");
   
}

?>
 

mangojack

Well-Known Member
Licensed User
Thanks @sorex .. tried that but did not help.

It just beats me slightly .. that this would be a reasonably common problem .. and comes with out of the box fixes. But out there there just seems to be a myriad of fixes, recommends and workarounds that do not work.
Maybe I'm just not getting this side of programming (web services) yet.
I will take up Erel's suggestion above as an alternative method for the time being .. I just find it difficult to let things beat me (which explains too many late nights)

Regards to all ;)
 

sorex

Expert
Licensed User
what error do you get?

the replace should happen at the B4A side.

you can also use a CSV like method, that just needs some explodes to get the data you need.
 

mangojack

Well-Known Member
Licensed User
Yes did the replace within app .. but with all Ive read I did not believe it would solve the problem.
basically the PHP script bombs out as soon as "&" detected ... I will learn later how to get back error info etc

I went back to basics .. Single record inserts with .download2 (Get) and .Poststring (Post) .. getting there:confused:
All worked as wanted . full unadulterated URL saved to db. At this point thats all I wanted to achieve.
Just would have liked to do the full db export via Map array/JSON string thingy but am aware of alternatives

Thanks You ..

you can also use a CSV like method, that just needs some explodes to get the data you need.
There goes my sleep .. now dreaming of Implodes and Explodes o_O
 

sorex

Expert
Licensed User
the problem is that & is used a parameter seperation when sending over data so you'll split things.
 

mangojack

Well-Known Member
Licensed User
the problem is that & is used a parameter seperation when sending over data so you'll split things.
Yes , I understand that ... and have also learn't so much more ... If It was not for B4A ... I would not know these things..

Thanks for your Help..
 
Top