我正在开发一个通过PHP将数据插入MySQL数据的应用程序。 一切似乎都好。但主要问题是我插入的数据 空白到MySQL数据库。
这是我的代码......
1)这是我的名为insert.php
<?php
$host='my ip address';
$uname='user';
$pwd='password';
$db="user";
$con = mysql_connect($host,$uname,$pwd) or die("connection failed");
mysql_select_db($db,$con) or die("db selection failed");
//$party=$_POST['party'];
//$code=$_POST['code'];
//$emply=$_POST['emply'];
//$date=$_POST['date'];
//echo $code+' '+'aaa';
mysql_query("INSERT INTO myapp(party,code,emply,date) VALUES('$_REQUEST[party]','$_REQUEST[code]','$_REQUEST[emply]','$_REQUEST[date]') ");
//mysql_query("insert into myapp(code) values($code)");
mysql_close($con);
?>
这在“//”阻止意味着我试图这样做 2)这是我的java代码使用(Android Studio)
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.os.Bundle;
import android.os.StrictMode;
import android.text.InputType;
import android.text.method.PasswordTransformationMethod;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.Toast;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
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 org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class MainActivity extends Activity {
protected static final String PrgUpdate = null;
Button mysql;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
Button mysql = (Button)findViewById(R.id.mysql);
InputStream is = null;
mysql.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String party="Abhisheck";
String code="All update";
String emply="Bhavasar";
String date="01/04/2014";
List<NameValuePair> nameValuePair= new ArrayList<NameValuePair>(1);
nameValuePair.add(new BasicNameValuePair("party",party));
nameValuePair.add(new BasicNameValuePair("code",code));
nameValuePair.add(new BasicNameValuePair("emply",emply));
nameValuePair.add(new BasicNameValuePair("date",date));
System.out.println(nameValuePair.toString());
try{
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://youripaddress.com/insert.php");
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair));
HttpResponse response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
Toast.makeText(getApplicationContext(),"Data enter succesful",Toast.LENGTH_SHORT).show();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
Log.e("ClientProtocolException","Client");
e.printStackTrace();
} catch (IOException e) {
Log.e("Ioexception","Ioexption");
e.printStackTrace();
}
}
});
}
不要担心“”“”日期(字段)“”“”“它是字符字段。 我的数据库mysql在这里
1 id int(6) UNSIGNED No None AUTO_INCREMENT
2 party char(50) latin1_swedish_ci Yes NULL
3 code varchar(50) latin1_swedish_ci Yes NULL
4 emply varchar(50) latin1_swedish_ci Yes NULL
5 date char(50) latin1_swedish_ci Yes NULL
现在有什么实际问题。 为什么不在MySQL中插入记录。
答案 0 :(得分:0)
Insert
是以异步方式在数据库中插入数据的类。您可以通过调用execute()
方法来调用该类。您需要覆盖doInBackground()
的{{1}}方法。
您可以在此处的文档中找到更多详细信息:AsyncTask
AsyncTask
答案 1 :(得分:0)
在doInbackground()
中使用它runOnUiThread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
}
});