mysql insert problems and comparing
Hi guys after searching for 2-3 days,i have totally no clue as to what is wrong so i appreciate some help :)
I am using wampserver..what i am having problem is inseting data into mysql from android ..whenever i press the insert button,a blank row is added to my columns without any information.
This is my php for inserting
<?php
$abc=$_POST['abc'];
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("dbinsert", $con);
$sql="INSERT INTO Detail (Utext) VALUES ('".$abc."')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "1 record added";
mysql_close($con)
?>
for my android
package com.project.insert;
import java.util.ArrayList;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class PassData extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button Insert=(Button)findViewById(R.id.button1);
Insert.setOnClickListener(new View.OnClickListener(){
public void onClick(View v)
{
try
{
EditText CompleteText=(EditText)findViewById(R.id.editText1);
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("abc", "'"+CompleteText.getText().toString()+"'开发者_运维知识库"));
Log.e(""+CompleteText.getText().toString(),"0");
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://10.0.2.2/InsertStatement.php");
HttpResponse response = httpclient.execute(httppost);
}
catch(Exception e)
{
Log.e("log_tag", "Error in http connection "+e.toString());
}
}
});
}
}
I don't see where you do anything with nameValuePairs
to put it into the request.
HttpPost httppost = new HttpPost("http://10.0.2.2/InsertStatement.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8));
HttpResponse response = httpclient.execute(httppost);
might do the trick.
For security reasons, you might want to add the '
on the server (and not the android code - right now you use both which would show a mysql error) and use mysql_real_escape_string
server-side.
精彩评论