Android Question MySql Insert

daniedb

Active Member
Licensed User
Longtime User
Hi Guys

Working through the excellent tutorial from Erel and some over memebrs using MYSQL to insert/List data in a MySql Database/Table.
List view work fine, but as soon as I try to Insert a New record it tells me
Response from server: Access denied for user 'root'@'localhost' (using password: NO)
Access denied for user 'root'@'localhost' (using password: NO)

I've got 2 PHP file, get.php for getting the info, and ins.php for inserting.
The Username/Paths/Password/Database all is entered correctly. Double/Tripple check that

Can anyone see something wrong in the code. I did give permissions in Phpmyadmin, for domain to access my Database.
B4X:
'Activity module
Sub Process_Globals
   Private LIST_CATCH  As String
End Sub

Sub Globals
   Type TwoLines (First As String, Second As String)
   Dim ViewCatchersList As ListView
   Private ExitBtn As Button
   Private NameSurnametxt As EditText
   Private AddressTxt As EditText
   Private AddBtn As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
   Activity.LoadLayout("Main")
   FetchNamesList
End Sub
Sub FetchNamesList
   ProgressDialogShow("Fetching list of Name")
   ExecuteRemoteQuery("SELECT NameSurname, Address FROM catchers ", LIST_CATCH)
End Sub

Sub ExecuteRemoteQuery(Query As String, JobName As String)
   Dim job As HttpJob
   job.Initialize(JobName, Me)
   job.PostString("http://www.xxxx.co.za/get.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 LIST_CATCH
         Dim GetCatchList As List
         GetCatchList = parser.NextArray 'returns a list with maps
         For i = 0 To GetCatchList.Size - 1
           Dim m As Map
           m = GetCatchList.Get(i)
           'We are using a custom type named TwoLines (declared in Sub Globals).
           'It allows us to later get the two values when the user presses on an item.
           Dim tl As TwoLines
           tl.First = m.Get("Address")
           tl.Second = m.Get("NameSurname")
           ViewCatchersList.AddTwoLines2(tl.First, tl.Second, tl)
         Next
  Case "INSERT_CATCH"
  'print the result to the logs
  Log(Job.GetString)   
     End Select
   Else
     ToastMessageShow("Error: " & Job.ErrorMessage, True)
   End If
   Job.Release
End Sub

Sub ExitBtn_Click
   Activity.Finish
End Sub

Sub AddBtn_Click
  Dim InsCatchers As HttpJob
  InsCatchers.Initialize("INSERT_CATCH", Me)
  InsCatchers.Download2("http://www.xxx.co.za/ins.php", Array As String("NameSurname", NameSurnametxt.text, "Address", AddressTxt.text))
End Sub

PHP Insert code
B4X:
<?
$databasehost = "localhost";
$databasename = "db";
$databaseusername ="user";
$databasepassword = "pw";

$con = mysql_connect($host,$user,$pw) or die(mysql_error());
mysql_select_db($db) or die(mysql_error());
mysql_query("SET CHARACTER SET utf8");
mysql_query("SET NAMES 'utf8'");

$Address = $_GET["Address"];
$NameSurname = $_GET["NameSurname"];

$res = mysql_query("Insert into catchers (NameSurname, Address) VALUES ('$NameSurname', $Address)");

print ("I have inserted $NameSurname and $Address in my table");
?>

I have tried Poststring and Download 2 in Httputils. Same results

Thanks
Danie
 

sorex

Expert
Licensed User
Longtime User
did you give access for that user in your database, even better... write (insert) access ?
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
What do you mean by "write (insert) acccess"?

sometimes only the SELECT option is being tagged as granted right.

but then you would get other errors, I guess either the server address is wrong or the user/pass combination.

are you sure the database server is the same as the webserver?

usually they split it up to spread resources.
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
also check the database name, sometimes they use the user prefix then it will be username_mydb instead of mydb
 
Upvote 0

daniedb

Active Member
Licensed User
Longtime User
also check the database name, sometimes they use the user prefix then it will be username_mydb instead of mydb
Hi Sorex. Yip checked that.
Like I said. The retrieve of data and display in a List works 100%
I've use the exact connection string in the ins.php code aswell
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
put an echo of some rubbish before the insert and see if that gets printed.
 
Upvote 0

daniedb

Active Member
Licensed User
Longtime User
GOT IT SOLVED
MY STUPID MISTAKE
$databasehost = "localhost";
$databasename = "db";
$databaseusername ="user";
$databasepassword = "pw";

$con = mysql_connect($host,$user,$pw) or die(mysql_error());

Wrong variables declared in the connection string.
Think it's time for Bed, havn't slept for 3 days

Thanks for the help guys
 
Upvote 0
Top