• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps problems inserting into online database

jonbonazza

Android Expert
ok, so an app I am working on needs to read from an online database and display the results dynamically. This works fine, but then when I press a button in my UI, I need it to add a new row into the same database. When I test the app and click the button, it doesn't throw any exceptions or anything and once it finsihes the process goes right back to letting me do w/e I need on the UI. When I test the PHP script through the browser, it goes through all he echos I have in the script and reaches the bottom of the script no problem, however it doesn't add the row... I am at a loss here... Maybe you all can help.

Here is my PHP file:
Code:
<?php
mysql_connect("localhost","XXXXX","XXXXX");
mysql_select_db("bonafie0_mm");
echo "connected to database";
mysql_query("INSERT INTO Comments (user, comment) VALUES ('".$_REQUEST['user']."', ".$_REQUEST['comment']."')");
echo "done. :D";
mysql_close();
?>

and here is my android code:
Code:
private void postComment()
	{
		String user = editName.getText().toString();
		String comment = editComment.getText().toString();
		OutputStream os = null;
		ArrayList<NameValuePair> nameValuePair = new ArrayList<NameValuePair>();
		nameValuePair.add(new BasicNameValuePair("user", user));
		nameValuePair.add(new BasicNameValuePair("comment", comment));
		try
		{
			HttpClient client = new DefaultHttpClient();
			HttpPost post = new HttpPost(ROTM_POST_URL);
			post.setEntity(new UrlEncodedFormEntity(nameValuePair));
			HttpResponse response = client.execute(post);
			
		}
		catch(IOException e)
		{
			Toast.makeText(RoTM.this, "Unable to post comments", Toast.LENGTH_SHORT).show();
		}
		editName.setText("");
		editComment.setText("");
	}

Any ideas?
 
Sure. Here is the correct code:

PHP File:
Code:
<?php
mysql_connect("localhost","username","password");
mysql_select_db("databaseName");
echo "connected to database";
mysql_query("INSERT INTO TableName (column1, column2) VALUES ('".$_REQUEST['user']."', '".$_REQUEST['comment']."')");
echo "done. :D";
mysql_close();
?>

Android code:
Code:
private void postComment()
	{
		String user = editName.getText().toString();
		String comment = editComment.getText().toString();
		OutputStream os = null;
		ArrayList<NameValuePair> nameValuePair = new ArrayList<NameValuePair>();
		nameValuePair.add(new BasicNameValuePair("user", user));
		nameValuePair.add(new BasicNameValuePair("comment", comment));
		try
		{
			HttpClient client = new DefaultHttpClient();
			HttpPost post = new HttpPost(ROTM_POST_URL);
			post.setEntity(new UrlEncodedFormEntity(nameValuePair));
			HttpResponse response = client.execute(post);
			
		}
		catch(IOException e)
		{
			Toast.makeText(RoTM.this, "Unable to post comments", Toast.LENGTH_SHORT).show();
		}
		editName.setText("");
		editComment.setText("");
	}

Hope this helps. :)
 
Back
Top Bottom