我知道這個問題已被問了很多次。但沒有任何工作。我試圖只使用PHP,Android和MySQL從應用程序插入數據到數據庫。我嘗試了一切,但不明白這裏有什麼問題?我已經添加了這兩個代碼。每當我嘗試插入任何內容時,都會在應用程序端顯示「成功」。但MySQL DB保持空白。任何幫助表示讚賞。數據沒有插入到mysql數據庫:android和php的問題
CODE:
user_feedback.php
<?php
include_once("dbConfig.php");
$store_name = isset($_POST['store_name']) ? $_POST['store_name'] : null;
$name = isset($_POST['name']) ? $_POST['name'] : null;
$feedback = isset($_POST['feedback']) ? $_POST['feedback'] : null;
$sql = "insert into user_feedback (store_name,name,feedback) values ('$store_name','$name','$feedback')";
if(mysqli_query($con,$sql)){
echo 'success';
}
else{
echo 'failure';
}
mysqli_close($con);
?>
的Java
Feedback.java
package net.simple.insertintomysql;
import android.os.AsyncTask;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
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 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;
public class MainActivity extends ActionBarActivity {
EditText editTextStoreName;
EditText editTextName;
EditText editTextFeedback;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editTextStoreName = (EditText) findViewById(R.id.editTextstoreName);
editTextName = (EditText) findViewById(R.id.editTextname);
editTextFeedback = (EditText) findViewById(R.id.editTextFeedback);
}
public void insert(View view){
String storename = editTextStoreName.getText().toString();
String name = editTextName.getText().toString();
String feedback= editTextFeedback.getText().toString();
insertToDatabase(storename,name,feedback);
}
private void insertToDatabase(String storename, String name, String feedback){
class SendPostReqAsyncTask extends AsyncTask<String, Void, String> {
public String storename=null;
public String name=null;
public String feedback=null;
@Override
protected String doInBackground(String... params) {
String paramStorename = params[0];
String paramName = params[1];
String paramFeedback = params[2];
//InputStream is = null;
runOnUiThread(new Runnable() {
@Override
public void run() {
String storename = editTextStoreName.getText().toString();
String name = editTextName.getText().toString();
String feedback = editTextFeedback.getText().toString();
}
});
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("storename", storename));
nameValuePairs.add(new BasicNameValuePair("name", name));
nameValuePairs.add(new BasicNameValuePair("feedback", feedback));
try {
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(
"http://192.168.1.2/new/user_feedback.php");
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
//is = entity.getContent();
} 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(storename, name, feedback);
}
@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);
}
}
[小博](http://bobby-tables.com/)說***腳本有風險SQL注入攻擊。](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php)***瞭解[編寫](http://en.wikipedia.org/wiki/Prepared_statement)語句[MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php)。即使[轉義字符串](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string)是不安全的! –
不要只回應'失敗'。檢查錯誤將幫助您更多。或者至少幫助我們幫助你。回聲「錯誤:」。 $ sql。 「
」。 $ con> error – Barns
您是否試圖編碼您想要插入的值以確認插入操作是否正確? – Barns