Android Question Insert variables into a distant phpMyadmin database

ciginfo

Well-Known Member
Licensed User
Longtime User
Hello,
I know how to extract data from a phpMyadmin database on a remote server but I cannot create new lines and insert various variables.
Below is the code used. Either syntax error, or it is the name of the variable which passes and not its content.
Could you help me by telling me the exact syntax to use.
thank you so much

B4X:
Private Sub Button1_Click
    'Here, Error message : "champs1 does'nt exist"
    ExecuteRemoteQuery("INSERT INTO Mytable (col1, col2, col3) VALUES (champs1, champs2, champs3)",ENVOI)

    'Here, it runs but it writes Champs1, champs2, et champs3 and not the content of the variables
    ExecuteRemoteQuery("INSERT INTO Mytable (column1, column2, column3) VALUES ('champs1', 'champs2', 'champs3')",ENVOI)
End Sub

Sub ExecuteRemoteQuery(Query As String, JobName As String)
    Dim job As HttpJob
    job.Initialize(JobName, Me)
    job.PostString("https://mysite.fr/applis/myappli.php", Query)
End Sub

Sub JobDone(Job As HttpJob)
    ProgressDialogHide
    If Job.Success Then
        Dim res As String
        res = Job.GetString
        Log("Response from server: " & res)
        Dim parser As JSONParser
        parser.Initialize(res)
        Select Job.JobName
            Case CHERCHE_NOMS
                Dim NOMS As List
                MAILS = parser.NextArray 'returns a list with maps
                For i = 0 To NOMS.Size - 1
                    Dim m As Map
                    m = NOMS.Get(i)
            '  etc..........
                Next
            Case ENVOI
                Dim champs1, champs2, champs3 as string
                champs1 = "Hello"
                champs2 = "The"
                champs3 = "World"
        End Select
    Else
        Log(Job.ErrorMessage)
        ToastMessageShow("Error: " & Job.ErrorMessage, True)
    End If
    Job.Release
End Sub
 

Andrew (Digitwell)

Well-Known Member
Licensed User
Longtime User
How about:
B4X:
 ExecuteRemoteQuery($"INSERT INTO Mytable (col1, col2, col3) VALUES (${champs1}, ${champs2}, ${champs3})"$,ENVOI)

You may also need the single quotes
 
Upvote 0

ciginfo

Well-Known Member
Licensed User
Longtime User
How about:
B4X:
ExecuteRemoteQuery($"INSERT INTO Mytable (col1, col2, col3) VALUES (${champs1}, ${champs2}, ${champs3})"$,ENVOI)

You may also need the single quotes
Thanks a lot, but this syntax doesn't work.
If I place the variables in the ENVOI select of Sub JobDone (Job As HttpJob)
Case ENVOI
Dim champs1, champs2, champs3 as string
champs1 = "Hello"
champs2 = "The"
champs3 = "World"
I get the following error message:
"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' , )' at line 1"

If I put the same variables in the sub, just before ExecuteRemoteQuery I get this error message:
ResponseError. Reason: , Response: INSERT INTO Mytable (Col1, col2, col3) VALUES (champs1, Champs2, Champs3)\nUnknown column 'Champs1' in 'field list'

An other Idea ?
 
Upvote 0

Andrew (Digitwell)

Well-Known Member
Licensed User
Longtime User
if you look at your error the problem was that the last parameter was empty (my bolding)
"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' , )' at line 1"


use a LOG in the ExecuteRemoteQuery function and output the string from my example, at the start of the function.

Log(Query)


see this.
It is the same as my solution, but without the smartstring.
It looks like you do need to use single quotes.

It also look like you are assigning the values in the JobDone AFTER they need to be used.
 
Upvote 0

ciginfo

Well-Known Member
Licensed User
Longtime User
Thank you, now like this it runs
B4X:
ExecuteRemoteQuery("INSERT INTO Mytable (col1, col2, col3) VALUES ('" & Champs1 & "', '" & Champs1 & "','" & Champs1 & "')",ENVOI)
 
Upvote 0

Andrew (Digitwell)

Well-Known Member
Licensed User
Longtime User
Glad you got it working. This is the same as using smart strings with the Single quotes as I said earlier.

B4X:
ExecuteRemoteQuery("INSERT INTO Mytable (col1, col2, col3) VALUES ('" & Champs1 & "', '" & Champs1 & "','" & Champs1 & "')",ENVOI)
is functionally the same as 
ExecuteRemoteQuery($"INSERT INTO Mytable (col1, col2, col3) VALUES ('${champs1}','${champs1}', '${champs1}')"$,ENVOI)
 
Upvote 0
Top