gpt4 book ai didi

php - 将编辑文本值从 android 插入到 mysql

转载 作者:行者123 更新时间:2023-11-30 21:59:35 25 4
gpt4 key购买 nike

我想要的是从 Android 中的编辑文本中插入一个值到 mysql 数据库中,有人可以用一个简单的例子告诉我怎么做吗?mysqldb 名称是 'android' 包含 1 列 'name'

<EditText
android:layout_width="250dp"
android:layout_height="wrap_content"
android:hint="User Name"
android:id="@+id/new_user_name"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"/>

最佳答案

首先我们将连接到数据库

<?php
define('HOST','yourHost');
define('USER','username');
define('PASS','password');
define('DB','android');
$con = mysqli_connect(HOST,USER,PASS,DB);

然后您需要捕获 post 变量。此变量将与我们的应用一起发送。

$name = $_POST['name'];

现在将值插入您的数据库。

$sql = "insert into Persons (name,address) values ('$name')";
if(mysqli_query($con,$sql)){
echo 'success';
}
else{
echo 'failure';
}
mysqli_close($con);
?>

我使用 EditTextButton 创建了一个简单的布局:

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp"
tools:context=".MainActivity">

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name" />

<EditText
android:id="@+id/editTextName"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="insert"
android:text="Insert" />

<TextView
android:id="@+id/textViewResult"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

MainActivity.java

public class MainActivity extends ActionBarActivity {

private EditText editTextName;
private EditText editTextAdd;

String name = editTextName.getText().toString();
String add = editTextAdd.getText().toString();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

editTextName = (EditText) findViewById(R.id.editTextName);

}

public void insert(View view){

insertToDatabase(name,add);
}

private void insertToDatabase(final String name, final String add){
class SendPostReqAsyncTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
String paramUsername = params[0];


List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("name", name));
nameValuePairs.add(new BasicNameValuePair("address", add));

try {
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(
"http://youwebsite.com/insert-db.php");
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

HttpResponse response = httpClient.execute(httpPost);

HttpEntity entity = response.getEntity();


} catch (ClientProtocolException e) {

} catch (IOException e) {

}
return "success";
}

@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);

Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();
TextView textViewResult = (TextView) findViewById(R.id.textViewResult);
textViewResult.setText("Inserted");
}
}
SendPostReqAsyncTask sendPostReqAsyncTask = new SendPostReqAsyncTask();
sendPostReqAsyncTask.execute(name, add);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
//getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
//int id = item.getItemId();

//noinspection SimplifiableIfStatement
//if (id == R.id.action_settings) {
// return true;
//}
return super.onOptionsItemSelected(item);
}
}

希望这能帮助您更好地理解它。

关于php - 将编辑文本值从 android 插入到 mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43837693/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com