Android Question Question about MYSQL with PHP connection in B4A.

ciapw

Member
Licensed User
Hi. all. i am trying to update my row in database based on my inserted data (i am trying the input one only). The data being inserted is from the spinner and the edittext view. From the code.. it says inserted.. but my db is not being updated with the newest data being sent. Here is the code for PHP : Where do I miss? Thanks :)
PHP:
<?php
$host = "127.0.0.1";
$user = "root";
$pw = "";
$db = "tarm";


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

$action = $_GET["action"];
switch ($action)

{
    /* case "CountPersons":
        $q = mysqli_query($con,"SELECT * FROM persons");
        $count = mysqli_num_rows($q);
        print json_encode($count);
    break;
 
    Case "GetPersons":
        $q = mysqli_query($con,"SELECT name, age FROM persons");
        $rows = array();
        while($r = mysqli_fetch_assoc($q))
        {
            $rows[] = $r;
        }
        print json_encode($rows);
    break;
 
    case "InsertNewPerson":
        $name = $_GET["name"];
        $age = $_GET["age"];
        $q = mysqli_query($con,"INSERT INTO persons (name, age) VALUES ('$name', $age)");
        print json_encode("Inserted");
    break; */
 
    case "input":
        $id = $_GET["id"];
        $jml = $_GET["jml"];
        $q = mysqli_query($con,"UPDATE daftarobat SET jml='$jml', WHERE id='$id'");
        print json_encode("Inserted");
    break;
}

?>
B4A code :
B4X:
#Region  Project Attributes
    #ApplicationLabel: Admin App-1
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
    #BridgeLogger:True
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Private PersonsListview As ListView
    
    Private byklbl As Label
    Private bykobat As EditText
    Private inbtn As Button
    Private jenislbl As Label
    Dim obatMap As Map
    Private obat As Spinner
    Dim jnsobat(5) As String
    Private stbtn As Button
    Private ServerIP As String

    Dim s As String
    Dim k As String
    Dim kode As String
    Dim n As Int
End Sub

Sub Activity_Create(FirstTime As Boolean)   
    Activity.LoadLayout("1.bal")
    Activity.Title = "Administrator App"
    
    obatMap.Initialize   
    ServerIP="192.168.0.102"
    
    jnsobat(0)="Obat Demam"
    jnsobat(1)="Obat Flu"
    jnsobat(2)="Obat Maag"
    jnsobat(3)="Obat Diare"
    jnsobat(4)="Obat Batuk"   
    
    obat.Prompt="Pilih Obat"
    obat.AddAll(jnsobat)
    obatMap.Put("Obat Demam","demam")
    obatMap.Put("Obat Flu","flu")
    obatMap.Put("Obat Maag","maag")
    obatMap.Put("Obat Diare","diare")
    obatMap.Put("Obat Batuk","batuk")
    
End Sub

Sub obat_ItemClick (Position As Int, Value As Object)
    kode = obatMap.Get(Value)   
    'Log(kode)
End Sub



Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub


Sub stbtn_Click
    
End Sub

Sub bykobat_EnterPressed
    n = bykobat.Text
End Sub

Sub inbtn_Click   
    If bykobat.Text="" Then
        Msgbox("Mohon Isi Banyak Obat yang Diinput","ERROR")
        Return
    End If
    
    Dim input As HttpJob
    input.Initialize("insert",Me)
    input.Download2("http://" & ServerIP & "/tarmistor/testtarm.php", Array As String ("action", "input", "id",kode ,"jml",n))
    Dim mess As String
    mess = kode& NumberFormat(n,1,0)
    Log(mess)
    
End Sub

Sub JobDone(Job As HttpJob)
    ProgressDialogHide
    If Job.Success Then
        Dim res As String
        res = Job.GetString
        Log("Back from Job:" & Job.JobName )
        Log("Response from server: " & res)
                
        Dim parser As JSONParser
        parser.Initialize(res)
        
        Select Job.JobName
                        
            Case "GetP"
                Dim ListOfPersons As List
                Dim PersonName As String
                Dim PersonAge As Int
                
                ListOfPersons = parser.NextArray 'returns a list with maps
                
                PersonsListview.Clear
                
                If ListOfPersons.Size=0 Then
                    PersonsListview.AddSingleLine ("No persons found...")
                Else
                    For i = 0 To ListOfPersons.Size - 1
                        Dim Person As Map
                        Person = ListOfPersons.Get(i)
                                            
                        PersonName = Person.Get("name")
                        PersonAge = Person.Get("age")
                        
                        PersonsListview.AddSingleLine (PersonName & ", " & PersonAge)
                        
                    Next
                End If
        
            Case "CountP"
                PersonsListview.AddSingleLine ("Persons in table: " & parser.NextValue)
                
            Case "insert"
                'Do nothing
                
        End Select
        
        
        
    Else
        ToastMessageShow("Error: " & Job.ErrorMessage, True)
    End If
    Job.Release
End Sub
 

sorex

Expert
Licensed User
you're updating something that probably doesn't exist.

I don't see any reference to the insert in your B4A code. (only input which is update)
 

Erel

Administrator
Staff member
Licensed User

ciapw

Member
Licensed User
Hi all.. thanks for the replies.. I finally figure it out. To update the query, i should use this code :
PHP:
 case "input": #nambahin jml stok
        $id = $_GET["id"];
        $jml = $_GET["jml"];                   
        $q = mysqli_query($con,"UPDATE daftarobat SET jml=(jml+'$jml') WHERE id='$id'");
        print json_encode("Inserted");
        break;
the jml=(jml+'$jml') is the code which update the query..

and for the sql injection prevention.. i need to learn more about that :D
 
Top