Android Question Added new row in db - MySql Problem, PHP

WebQuest

Active Member
Licensed User
Hi community I'm testing the functions to add a new Row in the db present on my server, I state that I am a beginner with php codes. I am editing a php script that I take as an example. I replaced the parameters for accessing the server with my access parameters, then I replaced the db with mine and modified the name of the table where I entered the parameters and fields to direct the new row to my db. The problem is that no addition to the db occurs and I get no error from the server and from the json log how can I solve it?

In the server error log this is this:
[13-Nov-2019 12:17:07 UTC] PHP warning: mysqli_error () has exactly 1 parameter, 0 given in /Directory/personsdb.php on line 9

I wanted to clarify that I use PhpMyAdmin for the db in Cpanel and that for reading the rows from my app it works.

SCRIPT PHP
B4X:
  <?php
 $host = "localhost";
    $user = "xxxxxxx";     //parameters obscured by me
    $pw = "xxxxxxxxx;
    $db = "xxxxxxxxx";


    $con = mysqli_connect($host,$user,$pw) or die(mysqli_error());
    mysqli_select_db($con,$db) or die(mysqli_error());
    mysqli_query($con,"SET CHARACTER SET utf8");
    mysqli_query($con,"SET NAMES 'utf8'");

 
    $json = file_get_contents("php://input");
    $jsall = array();
    $jsone = array();
    $jsall=json_decode($json, true);
    $jsone=$jsall[0];
 
    $Action = $jsone["Action"];
 
    switch ($Action)
    {
 
    Case "InsertPerson":
 
                                                        $pname=stripslashes(mysqli_real_escape_string($con,$jsone["pname"]));
$pbirthdate=stripslashes(mysqli_real_escape_string($con,$jsone["pbirthdate"]));
 
        $stmt = $con->prepare("INSERT INTO Comande (Post,Id) VALUES (?,?)");
        $rc=$stmt->bind_param($pname, $pbirthdate);
        $rc=$stmt->execute();
 
       break;
   default:
        print json_encode ("Error: Function not defined (" . $action . ")");
    }
?>

B4a CODE
B4X:
Public const Servername As String = "http://www.xxxxxx.com"
           Public const ScriptPath As String = "/xxxxx.php"

B4X:
Sub  InsertNewPerson (JsonString As String) As ResumableSub
    Dim j As HttpJob
    j.Initialize("", Me)
    j.PostString(Servername & ScriptPath, JsonString)
    Wait For (j) JobDone(j As HttpJob)
    Log(j.GetString)
    Dim Result As String = j.GetString
    j.Release
    Return Result
End Sub


Sub Button1_Click

    Dim id As Int=2
    JsonList.Initialize
 
    Dim JsonRow As Map = CreateMap("Action":"InsertPerson","pname":EditText1.Text,"pbirthdate":id)
    JsonList.Add(JsonRow)
 
    JsonGenerator.Initialize2(JsonList)
    Dim JSONstring As String = JsonGenerator.ToString
 
    Wait For(InsertNewPerson(JSONstring)) Complete (Result As String)
    Log(Result)
 
End Sub
 
Last edited:

MarkusR

Well-Known Member
Licensed User
Longtime User
Last edited:
Upvote 0

WebQuest

Active Member
Licensed User
Thanks for the answer I added the instruction ... and I found the problem, only now I can only enter int numeric characters. It happens that in the EditText I can insert on which column I want to write. I need to go through the EditText the string to add to the db how can I do? No I can detect in the example php where to act to indicate a fixed column and through the EditText pass the string
 
Upvote 0

WebQuest

Active Member
Licensed User
Hi Erel I thought about it but from what I understand jdrc2 needs a pc running the b4j server to work anywhere correct me if I'm wrong
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Any pc which is able to run java apps. PC, Server, a VPS (even cheap ones).
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Last edited:
Upvote 0

WebQuest

Active Member
Licensed User
Hi DonManfred I read Erel's tutorial and it seems more complicated to me. So there is no solution with a php script to load data into a mysql db via a b4a app?
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
So there is no solution with a php script to load data into a mysql db via a b4a app?

Sure it is. I always use PHP as bridge... You just need to do it right.

I can´t help you with the mysqli methods as i am using a mysqli class since many years.
 
Upvote 0

WebQuest

Active Member
Licensed User
The missing link is that I can't inherit data sent from the json in the script.


With fixed parameters I can add data to the db via app b4a input.
Do you know the get method to inherit json data in the php script?

B4X:
<?php
$servername = "xxxxxxt";
$username = "xxxxxxt";
$password = "xxxxxxt";
$dbname = "xxxxxxt";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

    mysqli_select_db($conn,$dbname) or die(mysqli_error());
    mysqli_query($conn,"SET CHARACTER SET utf8");
    mysqli_query($conn,"SET NAMES 'utf8'");
 
 
     if (mysqli_error($conn))
       {
        echo "Failed to connect to MySQL: " . mysqli_error();
       }
// Check connection

if (!$conn)
{
    die("Connection failed: " . mysqli_connect_error());
}
else
{
  
$sql = "INSERT INTO Comande (Id, Post)VALUES ('1', 'jhon')";
if (mysqli_query($conn, $sql))
{
    echo "New record created successfully";
}
    else
{
    echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);
}  
?>
 
Last edited:
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Use post request. Send the values as single keyvalue pairs. Or post the complete json and parse the json in php.
Easier is to send keyvalue pairs (at least i am more familar with).
PHP:
if (isset($_REQUEST['orderID'])){$userID=intval($_REQUEST['userID']);} else {$userID=0;}
  if (isset($_REQUEST['name'])){$orderID=$_REQUEST['name']);} else {$name="";}
 
Upvote 0

WebQuest

Active Member
Licensed User
I'm currently trying to send a couple with a b4a app via a map like this.

B4X:
Sub  InsertNewPerson (JsonString As String) As ResumableSub
    Dim j As HttpJob
    j.Initialize("", Me)
    j.PostString(Servername & ScriptPath, JsonString)
    Wait For (j) JobDone(j As HttpJob)
    Log(j.GetString)
    Dim Result As String = j.GetString
    j.Release
    Return Result
End Sub


Sub Button1_Click

    Dim id As Int=1
    Dim JsonRow1 As Map = CreateMap("Id":id,"Post":EditText1.Text)
   
    JsonGenerator.Initialize(JsonRow1)
    Dim JSONstring As String = JsonGenerator.ToString
   
    Wait For(InsertNewPerson(JSONstring)) Complete (Result As String)
    Log(Result)

End Sub

This example from the php manual encodes the php data in json how do I get the opposite?

B4X:
$ myObj-> city = "New York" ;

$ myJSON = json_encode ($ myObj);
 
Upvote 0

Brandsum

Well-Known Member
Licensed User
B4A Code:
B4X:
Sub  InsertNewPerson (data As Map) As ResumableSub
    Dim j As HttpJob
    j.Initialize("", Me)
    j.PostMultipart(Servername & ScriptPath, data,Null)
    Wait For (j) JobDone(j As HttpJob)
    Log(j.GetString)
    Dim Result As String = j.GetString
    j.Release
    Return Result
End Sub

Sub Button1_Click
    Dim id As Int=2
    Dim data As Map = CreateMap("Action":"InsertPerson","pname":EditText1.Text,"pbirthdate":id)
    Wait For(InsertNewPerson(data)) Complete (Result As String)
    Log(Result)
End Sub

PHP Code:
PHP:
<?php
 $host = "localhost";
    $user = "xxxxxxx";     //parameters obscured by me
    $pw = "xxxxxxxxx";
    $db = "xxxxxxxxx";


    $con = mysqli_connect($host,$user,$pw) or die(mysqli_error());
    mysqli_select_db($con,$db) or die(mysqli_error());
    mysqli_query($con,"SET CHARACTER SET utf8");
    mysqli_query($con,"SET NAMES 'utf8'");

    $Action = $_POST["Action"];
   
    switch ($Action)
    {
 
        case "InsertPerson":
            $pname=stripslashes(mysqli_real_escape_string($con,$_POST["pname"]));
            $pbirthdate=stripslashes(mysqli_real_escape_string($con,$_POST["pbirthdate"]));
   
            $stmt = $con->prepare("INSERT INTO Comande (Post,Id) VALUES (?,?)");
            $rc=$stmt->bind_param($pname, $pbirthdate);
            $rc=$stmt->execute();
   
           break;
       default:
            print json_encode ("Error: Function not defined (" . $action . ")");
    }
?>
 
Last edited:
Upvote 0
Top