Reputation: 45
I am new to Android programming and I am trying to take user input and add it to my external database. Right now my php code is:
<?php
mysql_connect("host","login","pass") or die ("Unable to connect to MySQL");
mysql_select_db("database");
mysql_query("INSERT INTO Table (number, format, name, price) VALUES ('".$_REQUEST['number1']."', ".$_REQUEST['format1'].", ".$_REQUEST['name1'].", ".$_REQUEST['price1'].")");
mysql_close();
?>
I don't see anything wrong with my php, but when I run my program I am not getting any errors, but when I look at my database nothing seems to be actually getting added. My Java code is:
Log.d("debug", "ENTER METHOD ADD");
new Thread(new Runnable(){
public void run(){
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
EditText eNumber = (EditText) findViewById(R.id.barcodeEdit);
EditText eFormat = (EditText) findViewById(R.id.codeFormatEdit);
EditText eName = (EditText) findViewById(R.id.titleEdit);
EditText ePrice = (EditText) findViewById(R.id.priceEdit);
Log.d("debugNumber", eNumber.getText().toString());
Log.d("debugFormat", eFormat.getText().toString());
Log.d("debugName", eName.getText().toString());
Log.d("debugPrice", ePrice.getText().toString());
nameValuePairs.add(new BasicNameValuePair("number1", eNumber.getText().toString()));
nameValuePairs.add(new BasicNameValuePair("format1", eFormat.getText().toString())); //you can add as many you need
nameValuePairs.add(new BasicNameValuePair("name1", eName.getText().toString()));
nameValuePairs.add(new BasicNameValuePair("price1", ePrice.getText().toString()));
try {
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://MY_SITE.com/my.php");
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
//is = entity.getContent();
Log.d("debug", "ENDED TRY");
}catch (Exception e) {
// TODO: handle exception
Log.e("log_tag", e.toString());
//return false;
//Toast.makeText(this, e.toString(), Toast.LENGTH_LONG).show();
}
}
}).start();
That is the bit of code that does my adding. I know I can connect to the database fine, since the code I use to search the database works correctly, but for some reason my add is messed up. I have the code running in a new thread since I learned the other day that you cannot do these http requests in the main thread of the program. I have been looking all over for an answer and have yet to find one, maybe I am just missing a line, any help is greatly appreciated.
Upvotes: 1
Views: 789
Reputation: 6751
I would suggest you to develop a simple html form first to make sure you don't get any problems at your php side like this.
<html xmlns='http://www.w3.org/1999/xhtml'>
<head >
<meta http-equiv='Content-Type' content='text/html; charset=utf-8'/>
<title >Form Page: sampleform</title>
</head>
<body>
<h1>Sample form page</h1>
<form id='sampleform' method='post' action='http://MY_SITE.com/my.php' >
<p>
number1: <input type='text' name='number1' />
</p>
<p>
format1: <input type='text' name='format1' />
</p>
<p>
name1: <input type='text' name='name1' />
</p>
<p>
price1: <input type='text' name='price1' />
</p>
<p>
<input type='submit' name='Submit' value='Submit' />
</p>
</form>
</body>
</html>
Run this quick test and let me know. So that I might help you further.
EDIT:
Yes. The problem might be in your insert statement. Try with the following code.
<?php
$mysqli = new mysqli("Your host", "Db username", "Db password", "Db name");
if ($mysqli->connect_errno) {
echo "Couldn't connect to database";
}
$number = $mysqli->real_escape_string($_POST['number1']);
$format= $mysqli->real_escape_string($_POST['format1']);
$name= $mysqli->real_escape_string($_POST['name1']);
$price= $mysqli->real_escape_string($_POST['price1']);
$query = "INSERT INTO Table (number, format, name, price) VALUES('$number','$format','$name', '$price');";
$result = $mysqli->query($query);
if($result)
{
echo "Successfully inserted";
}
?>
Upvotes: 1
Reputation: 1764
Apparently it is showing no error in code. To debug more line by line you should modify your PHP code and try -
<?php
mysql_connect("host","login","pass") or die ("Unable to connect to MySQL");
mysql_select_db("database");
echo $_REQUEST['number1'];
echo $_REQUEST['format1'];
echo $_REQUEST['name1'];
echo $_REQUEST['price1'];
$sql = "INSERT INTO Table (number, format, name, price) VALUES ('".$_REQUEST['number1']."', '".$_REQUEST['format1']."', '".$_REQUEST['name1']."', '".$_REQUEST['price1']."')";
echo $sql;
mysql_query($sql) or die(mysql_error());
mysql_close();
?>
Also you can try with $_POST syntax instead of $_REQUEST.
You should understand the error by this.
Please share with us the result, so that we can help you further.
Thanks
Upvotes: 0