dongyuan2388
dongyuan2388
2017-09-01 20:12
浏览 54

数据没有插入mysql数据库:android和php问题

enter image description hereI know this question has been asked many times. but nothing worked. I am trying to just insert data from app to DB using PHP, android, and MySQL. I tried everything but didn't understand what is wrong here? I have added both the codes. whenever I try to insert anything it shows "success" on the app side. but MySQL DB remains blank. Any help is appreciated.

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);
    }
}
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dongshang1529
    dongshang1529 2017-09-01 20:18
    已采纳

    Change this

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

    with this:

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

    In your code you are set the POST variable storename and not store_nome P.S. To look at life you could use okHTTP libraries or volley to send data to the server.

    UPDATE

    Try use volley request

    //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);
    }
    
    点赞 评论
  • dongshen7561
    dongshen7561 2017-09-01 20:17

    You are sending as

    storename
    

    but trying to get as

    store_name (as $_POST['store_name'] in php).
    
    点赞 评论

相关推荐