Android PHP数据插入问题

时间:2015-06-02 08:38:13

标签: php android

这里我创建了一个用于将一些数据插入PHP Mysql数据库的android应用程序。所以,我在这里首先解释我的代码中发生的事情。我创建了两个字段名称和地址,布局很好。现在,当我单击插入按钮时,成功消息即将到来,数据也会进入数据行创建,但问题是数据变为空白,表示字段字符串不会进入数据库。

以下是代码:

package com.oplo.user.listing;

import android.app.Activity;
import android.os.AsyncTask;
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.IOException;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends Activity {
    private EditText editTextName;
    private EditText editTextAdd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        editTextName = (EditText) findViewById(R.id.editTextName);
        editTextAdd = (EditText) findViewById(R.id.editTextAddress);
    }
    public void insert(View view){
        String name = editTextName.getText().toString();
        String add = editTextAdd.getText().toString();

        insertToDatabase(name,add);
    }
    private void insertToDatabase(String name, String add){
        @SuppressWarnings({"deprecation", "ResourceType"})
        class SendPostReqAsyncTask extends AsyncTask<String, Void, String> {
            @Override
            protected String doInBackground(String... params) {
                String paramUsername = params[0];
                String paramAddress = params[1];


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

                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://xyz.in/demo.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代码

<?php
  define('HOST','asdasd.in');
  define('USER','sadsad');
  define('PASS','sadasd');
  define('DB','asdasdasd');
  $con = mysqli_connect(HOST,USER,PASS,DB);
  $name = $_POST['name'];
  $address = $_POST['address'];

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

请检查代码并告诉我本程序中的错误位置。

提前致谢。

2 个答案:

答案 0 :(得分:1)

你的函数insertToDatabase()有两个字符串变量&quot; name&#39;并且&#39;添加&#39;作为一个论点。您在此函数中具有相同的新变量名称。这可能是问题所在。

答案 1 :(得分:0)

您的问题是您使用单引号查询。这样变量就不会被拉动。双引号字符串将作为变量而不是字符串插入您的变量。 用以下代码替换你的sql:

$sql = "insert into Persons (name,address) values ('${name}','${address}')";

将原始数据插入数据库配合是一种非常糟糕的做法...尝试使用预处理语句或mysqli :: real_escape_string或至少使用filter_input()函数过滤它们