Android Question PHP crashes if JSON string contains Ampersand

Discussion in 'Android Questions' started by mangojack, Jun 30, 2015.

  1. mangojack

    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
    Code:
    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");
       
    }

    ?>
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    mangojack likes this.
  3. sorex

    sorex Expert Licensed User

    try to replace each & with &amp;

    it might think that an entity is following.
     
    mangojack likes this.
  4. mangojack

    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 ;)
     
  5. sorex

    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.
     
    DonManfred likes this.
  6. mangojack

    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 ..

    There goes my sleep .. now dreaming of Implodes and Explodes o_O
     
  7. sorex

    sorex Expert Licensed User

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

    DonManfred Expert Licensed User

    Can you post an example JSON you want to transport to your PHP? Maybe provide a small example which calls a php. Maybe i can help with the PHP-Part...
     
    mangojack likes this.
  9. mangojack

    mangojack Well-Known Member Licensed User

    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..
     
  10. mangojack

    mangojack Well-Known Member Licensed User

    Thanks for the offer @DonManfred .. I will pass that offer for the time being ..
    I will go back to basics and try not to achieve so much so quickly ;)
     
Loading...