2017-09-01 73 views
-1

enter image description here我知道這個問題已被問了很多次。但沒有任何工作。我試圖只使用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); 
    } 
} 
+3

[小博](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)是不安全的! –

+1

不要只回應'失敗'。檢查錯誤將幫助您更多。或者至少幫助我們幫助你。回聲「錯誤:」。 $ sql。 「
」。 $ con> error – Barns

+1

您是否試圖編碼您想要插入的值以確認插入操作是否正確? – Barns

回答

1

更改此

$store_name = isset($_POST['store_name']) ? $_POST['store_name'] : null; 

     $sql = "insert into user_feedback (store_name,name,feedback) values ('$store_name','$name','$feedback')"; 

與此:

$store_name = isset($_POST['storename']) ? $_POST['storename'] : null; 
    $sql = "insert into user_feedback (store_name,name,feedback) values ('".$store_name."','".$name."','".$feedback."')"; 

在你的代碼設置的POST變量STORENAME而不是store_nome 附:爲了看看生活,你可以使用okHTTP庫或凌空將數據發送到服務器。

UPDATE

嘗試使用凌空要求

//not use AsyncTask because volley extends AsyncTask 
private void inserDB(....){ 
RequestQueue queue = Volley.newRequestQueue(this);  
url = "your URL"; 
StringRequest postRequest = new StringRequest(Request.Method.POST, url, 
    new Response.Listener<String>() 
    { 
        @Override 
        public void onResponse(String response) { 
            // response 
            Log.d("Response", response); 
        } 
    }, 
    new Response.ErrorListener() 
    { 
         @Override 
         public void onErrorResponse(VolleyError error) { 
             // error 
             Log.d("Error.Response", response); 
       } 
    } 
) {     
    @Override 
    protected Map<String, String> getParams() 
    {  
            Map<String, String>  params = new HashMap<String, String>();  
            params.put("POSTkey1", "value");  
            params.put("POSTkey2", "value"); 
              
            return params;  
    } 
}; 
queue.add(postRequest); 
} 
+0

不行不行。儘管如此,數據庫是空的 – user7939485

+0

你檢查表名是否正確?你有store_name更換商店名嗎? –

+0

是的。這是正確的。 – user7939485

0

您要發送的

storename 

而是試圖得到儘可能

store_name (as $_POST['store_name'] in php). 
+0

我明白了。但這不是問題,因爲數據庫是一樣的。每次我嘗試插入它都沒有。 – user7939485