Android Question Update my msql using Php

imak123

Member
Dear sir,
I have a B4A code to pass variable from B4A to php and upload my sqli database.Below is all my code. My programe could update one variable eg. nameEdit.Text however,
I do not know how to pass two variable from B4A to php.


The following work when pass one variable Name, but I don't know how to pass another variable name 'Phone ' to the php at the same time.

savedata.PostString("https://www.repairfairyhk.com/upPost.php","save=&Name="&nameEdt.Text )

Thanks for your kind help.
Mak




Code:
<?php   
    #connect to database
    // information on server
    $servername="localhost";
    $username = "repairfair_memberLog";
    $password = "Rf70939";
    $databaseName = "repairfair_member"; //DataBase name

    //Create Connection
    $mysqli= new mysqli($servername,$username,$password,$databaseName);
    if($mysqli-> connect_error){
        die("Connection failed: ".$mysqli-> connect_error);
    }
    
    //Save Data
        

    if(isset($_POST['save'])){
        $name=$_POST['Name'];       
        $sql = "INSERT INTO test(Name) VALUES ('$name')";
        
        //Execute to insert data
        if($mysqli->query($sql) == True){
            die("Data Saved !");
        }else{
            die("Error:". $sql. "<br>". $mysqli-> error);
        }
        //Close connection
        $mysqli->close();
    }
 ?>

/*******************************************************************************************
B4A Code :
Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.
Private Button1 As B4XView
Dim imageSelectedList As List

Private Panel1 As B4XView
Private ImageView1 As ImageView
Private ImageView2 As ImageView
Private ImageView3 As ImageView
Dim xui As XUI

Private upLoadBtn As B4XView
Private nameEdt As EditText
Private phoneNumEdt As EditText
Private detailEdT As B4XView

Dim savedata As HttpJob


End Sub

savedata.Initialize("savedata",Me)
savedata.PostString("https://www.repairfairyhk.com/upPost.php","save=&Name="&nameEdt.Text )

Sub jobDone(job As HttpJob)
'save data
Select job
Case savedata
If job.Success Then
'success message
ToastMessageShow("Saved ",False)
nameEdt.Text = ""
phoneNumEdt.Text=""
Else
ToastMessageShow("Check your internet connection",False)
End If
savedata.Release


End Select

[/CODE]
 

Andrew (Digitwell)

Well-Known Member
Licensed User
Longtime User
The php code is not very secure. Your are not protecting your SQL parameters correctly. It is possible to perform an SQL injection attack.

If you want to write an API in PHP I would recommend either using SLIM or Laravel.

SLIM v3 is great for lightweight REST API's.

Take a look at this example, http://www.codediesel.com/php/create-a-quick-rest-api-using-slim-framework/

Once you have an API up and running you should be able to send it JSON

so here is an example of setting up the parameters and calling the URL.

B4X:
    Private msgmap As Map = CreateMap()
    msgmap.Put("param1","Hello")
    msgmap.Put("param2","there")
    'You can add as many params here
    
    
    Private gen As JSONGenerator
    gen.Initialize(msgmap)
    ' this produces {"param1":"Hello","param2":"there"}

    Private job As HttpJob
    job.Initialize("",Me)
    job.poststring(<URL>,gen.ToString)
    ' The next line is important'
    job.GetRequest.SetContentType("application/json")
    
    wait for (job) JobDone(job As HttpJob)
    If (job.Success) Then
        Do something on success
    Else
       Do something on failure
    End if
 
Upvote 0
Top