B4J Question PHP file not parsing parameter string from Poststring

Sifu

Active Member
Hello,

i've a strange problem which costed me quite a bit of time to debug, still I don't know why my php file won't accept the string of parameters.
Note that the php files are working well, I use them on other projects in combination with another development environment.
So at a certain point I thought it's the database which is the culprit, but no.
I don't want to use JSON for this although I could, it should work also with these few parameters.
I also stopped using jRDC to be in between my app(s) and the DB, and fall back to PHP as I don't want to renew the key each time SSL is renewed (there is probably a script to to that)
and next to that it suddenly stopped working even after renewing the key and restarting the server. I don't need speed or async for this.
Anyway for me personally this is more convenient.

In short, if I use more than 1 parameter, then Poststring seems not working. Well that is that the parameters are not accepted or ignored but it returns succesfully.
I also used a nice website https://ptsv2.com where you can check what your script actually POSTs.(see below)

When I use only this in B4J, URLupdloginstat represents an URL:
B4X:
j.PostString(URLupdloginstat,"id="&id) 'id=1

in combination with this:
PHP:
<?php
//disable errors when live
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
require_once 'config.php'; //connection is working OK
try {
$stmt = $pdo->prepare("UPDATE IGNORE pagecontrol SET disabled=disabled+1 WHERE id=:id");
$stmt->bindParam(':id', $_POST['id'], PDO::PARAM_INT);
$affected_rows = $stmt->rowCount();
if($stmt->execute()) { echo "Something went good"; } else { echo "Something went wrong"; };
}
catch(PDOException $e)
    {
    echo "Something went wrong"  . $e->getMessage();
    }
var_dump($_POST);
$pdo = NULL;
?>
Then each time id=1 is send the disabled column gets incremented just fine

But when I use one of these in B4J, (behind the tested lines you can see the outcome as received on the POST test site):

B4X:
Sub Updateloginctrl(id As Int,ds As Int,notiftxt As String)
    Log ("id is " & id & "DS is " & ds & "text is " & notiftxt)
    Dim j As HttpJob
    j.Initialize("j", Me)

    'j.PostString(URLupdloginstat,Array("disabled="&ds,"notification="&notiftxt,"id="&id)) 'results in--> [Ljava.lang.Object;@464b4d18
    'j.PostString(URLupdloginstat,"disabled=&ds notification=&notiftxt id=&id") 'results in--> disabled=&ds notification=&notiftxt id=&id
    'j.PostString(URLupdloginstat,"disabled=ds&notification=notiftxt&id=id") 'results in--> disabled=ds&notification=notiftxt&id=id
    'j.PostString(URLupdloginstat,"disabled="&ds&"notification="&notiftxt&"id="&id) 'results in--> disabled=0notification=blablablaid=1
   
    'this should be the correct one for sending, this is the same string as used in another dev ide
    j.PostString(URLupdloginstat,"disabled="&ds&"&notification="&notiftxt&"&id="&id) 'results in--> disabled=1&notification=blablabla&id=1
    
    'j.GetRequest.SetContentType("application/x-www-form-urlencoded") 'makes no difference if used
    
    'j.PostMultipart(URLupdloginstat, CreateMap("disabled": ds, "notification":notiftxt,"id":id), Null) 'java error in B4J
...........

of which I think this one is correct:
B4X:
j.PostString(URLupdloginstat,"disabled="&ds&"&notification="&notiftxt&"&id="&id)
because it results in this string disabled=1&notification=blablabla&id=1 which normally is accepted.

in combination with this php, which is normally working correct but not with the received data:
PHP:
<?php
//disable errors displaying when live
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
require_once 'config.php'; //connection is working OK
try {
$stmt = $pdo->prepare("UPDATE  IGNORE pagecontrol SET disabled = :disabled, notification = :notification WHERE id= :id");
$stmt->bindParam(':disabled', $_POST['disabled'], PDO::PARAM_INT);
$stmt->bindParam(':notification', $_POST['notification'], PDO::PARAM_STR);
$stmt->bindParam(':id', $_POST['id'], PDO::PARAM_INT);
if($stmt->execute()) { echo "Something went good"; } else { echo "Something went wrong"; };
}
catch(PDOException $e)
    {
    echo "Something went wrong"  . $e->getMessage();
    }
var_dump($_POST);
$pdo = NULL;
?>
Returned is: Something went good,
While actually nothing changed in the db.

Does anyone have an idea why the parameters are not accepted?
Thanks!
 

PaulMeuris

Active Member
Licensed User
MySQL 8.0 Reference Manual
UPDATE IGNORE statements, including those having an ORDER BY clause, are flagged as unsafe for statement-based replication. (This is because the order in which the rows are updated determines which rows are ignored.) Such statements produce a warning in the error log when using statement-based mode.
Is it possible that the update is ignored because of a duplicate id (primary key)?
 
Upvote 0

Sifu

Active Member
Is it possible that the update is ignored because of a duplicate id (primary key)?
Well it is a very simple table which has only 2 rows and I use the WHERE statement.
I tried also without the IGNORE.
Also the php code with the increment function works ok

My guess is the notification string contains unsanitized characters which can break the sql command.
I've thought about that, but I thinks that's not the case as it would be only a space and even without that it's getting refused.
In the upper B4X code you can see the string that is received at the test site, behind the code.

Most probably it's a tiny simple thing, in the past I've spend days to find out that sometimes you need a ` instead of a '.
(I'm checking posts with poststring related with php, maybe there is a clue.

using this http://httpbin.org/post now as the reciever, it return this:
B4X:
Waiting for debugger to connect...
Program started.
Call B4XPages.GetManager.LogEvents = True to enable logging B4XPages events.
id is 1DS is 0text is hello
(Http client initialized with accept all option.)
{
  "args": {},
  "data": "",
  "files": {},
  "form": {
    "disabled": "0",
    "id": "1",
    "notification": "hello"
  },
  "headers": {
    "Accept-Encoding": "gzip",
    "Content-Length": "34",
    "Content-Type": "application/x-www-form-urlencoded",
    "Host": "httpbin.org",
    "User-Agent": "okhttp/4.9.0",
    "X-Amzn-Trace-Id": "Root=1-62a5bc2b-000ec7435432a3fe5929e4ad"
  },
  "json": null,
  "origin": "xx.xx.xx.xx",
  "url": "http://httpbin.org/post"
}
succes
So that looks perfectly ok,
lucky I have short hair so I can't pull it out...
 
Last edited:
Upvote 0

Sifu

Active Member
In another thread I saw someone forgot to put www before the URL, so I tried with and without, no change.
I've just added this line
B4X:
file_put_contents("post.log", print_r($_POST, true));
to the php so it creates a local log, as I found nothing in other logs.
And now suddenly it writes it in the DB, this line can't have any influence at all as far as I know... OK have to calm down, I just turned 50 last week pfffff.

I don't get it, it works, now I have to figure that out, thanks for thinking with me.
 
Upvote 0
Top