Reputation: 1759
I have a MySQL Database and I want to add an Entry with my Android App. For this I have a PHP Script in the middle. So my Android app should run the script with the value which shall be inserted into the Database and the Script calls the Database to insert it.
My MySQL Database named GCM_ID
has just one column named GCM_ID
and looks like this:
GCM_ID
------
id1
id2
id3
...
This is my PHP Script:
<?php
mysql_connect("my_domain","my_user","my_password");
mysql_select_db("GCM_ID");
$id=$_REQUEST['GCM_ID'];
$flag['code']=0;
if($r=mysql_query("insert into GCM_ID(GCM_ID) VALUES('$id')",$con))
{
$flag['code']=1;
echo"hi";
}
print(json_encode($flag));
mysql_close($con);
?>
The domain, user and password are correct, I tested this already with another Script.
This is the Java Code of my Android app to connect to the PHP Script:
public void insert(String gcm_id) {
ArrayList<NameValuePair> nameValuePairs = new ArrayList<>();
nameValuePairs.add(new BasicNameValuePair("GCM_ID", gcm_id));
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("link/to/my/php/script");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
Log.e("pass 1", "connection success ");
} catch (Exception e) {
Log.e("Fail 1", e.toString());
Toast.makeText(getApplicationContext(), "Invalid IP Address",
Toast.LENGTH_LONG).show();
}
try {
BufferedReader reader = new BufferedReader
(new InputStreamReader(is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result = sb.toString();
Log.e("pass 2", "connection success ");
} catch (Exception e) {
Log.e("Fail 2", e.toString());
}
try {
JSONObject json_data = new JSONObject(result);
code = (json_data.getInt("code"));
if (code == 1) {
Toast.makeText(getBaseContext(), "Inserted Successfully",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getBaseContext(), "Sorry, Try Again",
Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
Log.e("Fail 3", e.toString());
}
}
I got the code snippet from here and modified the PHP Script and Android code a bit. When I run my app, I got this error:
Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /home/a2161963/public_html/insertGcmIds.php on line 9
Warning: mysql_close(): supplied argument is not a valid MySQL-Link resource in /home/a2161963/public_html/insertGcmIds.php on line 16
I'm new to PHP, thats why I don't
The first problem is see is that the tutorial with the code snippet added a Pair of Values to the MySQL Database and thats why he used an ArrayList<NameValuePair>
. Can I use this NameValuePair
with just one Value? Can I do it somehow else?
Am I doing something else wrong?
Upvotes: 0
Views: 737
Reputation: 2934
Remove $con from mysql_query and mysql_close it will work
mysql_connect("my_domain","my_user","my_password");
mysql_select_db("GCM_ID");
$id=$_REQUEST['GCM_ID'];
$flag['code']=0;
if($r=mysql_query("insert into GCM_ID(GCM_ID) VALUES('$id')"))
{
$flag['code']=1;
echo"hi";
}
echo json_encode($flag);
mysql_close();
And stop using mysql_ extensions as it is deprecated.... Instead use mysqli or pdo. Try w3schools.com for mysqli extensions.
Upvotes: 1