Jump to content

Inserting into online database


jonbonazza

Recommended Posts

ok, so an android 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 the 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:

[syntax=php]

<?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();

?>

[/syntax]

 

and for those of you who are experienced with java and/or the android SDK, here is my android code:

[syntax=java]

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("");

}

[/syntax]

 

Any ideas? I posted this on the only two reputable android dev forums I could find, but no one has been able to help me as of yet... Since I am fairly confident in my java abilities, I figured my problem probably lies in my PHP code.

Link to comment
Share on other sites

Add some code to the query to display the mysql errors...

 

The query is probably just failing, and mysql won't tell you that unless you add something like

mysql_query("SELECT statement", $connection) or die (mysql_error);

 

There are more elegant ways to display the error, but this should help you find an answer

 

 

Link to comment
Share on other sites

I would suggest generating a query (send in the data in the url, and just echo out the sql statement that it is passing to the server.)

 

then take and run that query directly against the database to see if it runs. That should help see why it is failing, then you can manipulate it there until it works, then transfer that to the code.

 

Also, you should use mysql_real_escape_string() on the data fields to be safe :)

Link to comment
Share on other sites

$sql = "INSERT INTO Comments (user, comment) VALUES ('".mysql_real_escape_string($_REQUEST['user'])."', ".mysql_real_escape_string($_REQUEST['comment'])."')";
mysql_query($sql) or die ("Query: ".$sql."<br /><br />".mysql_error());
echo "done. ";
mysql_close();

 

Something like that

Link to comment
Share on other sites

Mmkay, I edited my code to do what you suggested and I get the following result:

 

Query: INSERT INTO Comments (user, comment) VALUES ('name', TestComment')

 

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

 

What is considered line 1? If it means the mysql_connect() function, it was copied from my grab_comment PHP file that works... why is it throwing an error on this one?

 

edit: here is my updated script:

<?php
mysql_connect("localhost","bonafie0_mm","mm121289");
mysql_select_db("bonafie0_mm");
echo "connected to database";
$sql = "INSERT INTO Comments (user, comment) VALUES ('".mysql_real_escape_string($_REQUEST['user'])."', 

".mysql_real_escape_string($_REQUEST['comment'])."')";
mysql_query($sql) or die ("Query: ".$sql."<br /><br />".mysql_error());
echo "done. ";
mysql_close();
?>

 

Link to comment
Share on other sites

Actually, that error message came from the database server.  The 'line 1' is referring to line 1 of the query.  Since your query contains only one line, it is not helpful here.

 

The problem is that there is a quote character missing just before the comment string.  The code should be:

$sql = "INSERT INTO Comments (user, comment) VALUES ('".mysql_real_escape_string($_REQUEST['user'])."', 
'".mysql_real_escape_string($_REQUEST['comment'])."')";

 

or use sprintf to build the query, I've recently discovered this helps keep the code cleaner and easier to read

$sql = sprintf("INSERT INTO Comments (user, comment) VALUES ('%s', '%s')", 
mysql_real_escape_string($_REQUEST['user']),
mysql_real_escape_string($_REQUEST['comment']));

 

 

EDIT: I mean the line number is not helpful.  The error message was very helpful.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.