通过android和php在mysql数据库中插入空白记录

Posted

技术标签:

【中文标题】通过android和php在mysql数据库中插入空白记录【英文标题】:Blank record insert in mysql database through android and php 【发布时间】:2015-05-10 14:45:38 【问题描述】:

我正在开发一个通过 php 将数据插入 mysql 数据的应用程序。 一切似乎都很好。但主要问题是我插入的数据 进入 MySQL 数据库空白。

这是我的代码......

1) 这是我的 PHP 文件,名为 insert.php

 <?php
 $host='my ip address';
 $uname='user';
 $pwd='password';
 $db="user";
 $con = mysql_connect($host,$uname,$pwd) or die("connection failed");
 mysql_select_db($db,$con) or die("db selection failed");
//$party=$_POST['party'];
//$code=$_POST['code'];
//$emply=$_POST['emply'];
//$date=$_POST['date'];
//echo $code+' '+'aaa';
mysql_query("INSERT INTO myapp(party,code,emply,date)          VALUES('$_REQUEST[party]','$_REQUEST[code]','$_REQUEST[emply]','$_REQUEST[date]')     ");
//mysql_query("insert into myapp(code) values($code)");
mysql_close($con);
?>

“//”中的这个被阻止意味着我尝试过 2)这是我使用(android Studio)的java代码

import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.os.Bundle;
import android.os.StrictMode;
import android.text.InputType;
import android.text.method.PasswordTransformationMethod;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ListView;
import android.widget.Spinner;
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 org.json.JSONObject;
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;
import java.util.Random;


public class MainActivity extends Activity 
protected static final String PrgUpdate = null;
Button mysql;

@Override
public void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    if (android.os.Build.VERSION.SDK_INT > 9) 
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
    
    Button mysql = (Button)findViewById(R.id.mysql);
    InputStream is = null;
    mysql.setOnClickListener(new View.OnClickListener() 
        @Override
           public void onClick(View v) 

            String party="Abhisheck";
            String code="All update";
            String emply="Bhavasar";
            String date="01/04/2014";

            List<NameValuePair> nameValuePair= new ArrayList<NameValuePair>(1);
            nameValuePair.add(new BasicNameValuePair("party",party));
            nameValuePair.add(new BasicNameValuePair("code",code));
            nameValuePair.add(new BasicNameValuePair("emply",emply));
            nameValuePair.add(new BasicNameValuePair("date",date));
            System.out.println(nameValuePair.toString());
            try
                HttpClient httpClient = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost("http://youripaddress.com/insert.php");
                httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair));
                HttpResponse response = httpClient.execute(httpPost);
                HttpEntity entity = response.getEntity();
                InputStream is = entity.getContent();
                Toast.makeText(getApplicationContext(),"Data enter succesful",Toast.LENGTH_SHORT).show();

             catch (UnsupportedEncodingException e) 
                e.printStackTrace();
             catch (ClientProtocolException e) 
                Log.e("ClientProtocolException","Client");
                e.printStackTrace();
             catch (IOException e) 
                Log.e("Ioexception","Ioexption");
                e.printStackTrace();
            
      
    );

不要担心 """"日期(字段)""""" 它是字符字段。 我的数据库mysql在这里

名称类型归类属性 Null 默认

1 id int(6) UNSIGNED No None AUTO_INCREMENT

2 party char(50) latin1_swedish_ci Yes NULL

3 code varchar(50) latin1_swedish_ci Yes NULL

4 emply varchar(50) latin1_swedish_ci Yes NULL

5 date char(50) latin1_swedish_ci Yes NULL

现在真正的问题是什么。 为什么记录不插入 MySQL。

【问题讨论】:

将错误报告添加到文件顶部,紧跟在打开 PHP 标记之后,例如 &lt;?php error_reporting(E_ALL); ini_set('display_errors', 1);,然后是其余代码,以查看它是否产生任何结果。还将or die(mysql_error()) 添加到mysql_query()。在 Java 方面,我帮不了你。 您检查了吗,所有值都可以插入? :) 第一件事是我的应用程序永远不会崩溃。 @vishalk 我通过 echo 命令检查所有值,但它打印“空白” 然后它会插入空白记录:) 【参考方案1】:

Insert 是将数据异步插入数据库的类。您可以通过调用execute() 方法来调用该类。您需要覆盖AsyncTaskdoInBackground() 方法。 您可以在此处的文档中找到更多详细信息:AsyncTask

class Insert extends AsyncTask<String, String, String>


    @Override
    protected String doInBackground(String... params) 
        // TODO Auto-generated method stub

        String party="Abhisheck";
        String code="All update";
        String emply="Bhavasar";
        String date="01/04/2014";

        List<NameValuePair> nameValuePair= new ArrayList<NameValuePair>(1);
        nameValuePair.add(new BasicNameValuePair("party",party));
        nameValuePair.add(new BasicNameValuePair("code",code));
        nameValuePair.add(new BasicNameValuePair("emply",emply));
        nameValuePair.add(new BasicNameValuePair("date",date));
        System.out.println(nameValuePair.toString());

        try
        
            HttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost("http://10.0.2.2/insert.php");
            httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair));
            HttpResponse response = httpClient.execute(httpPost);
            HttpEntity entity = response.getEntity();
            InputStream is = entity.getContent();
            //Toast.makeText(getApplicationContext(),"Data enter succesful",Toast.LENGTH_SHORT).show();

         catch (UnsupportedEncodingException e) 
            e.printStackTrace();
         catch (ClientProtocolException e) 
            Log.e("ClientProtocolException","Client");
            e.printStackTrace();
         catch (IOException e) 
            Log.e("Ioexception","Ioexption");
            e.printStackTrace();
        

        return null;
    



public class MainActivity extends Activity 
    protected static final String PrgUpdate = null;
    Button mysql;

    @Override
    public void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if (android.os.Build.VERSION.SDK_INT > 9) 
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
        
        Button mysql = (Button)findViewById(R.id.mysql);
        ProgressDialog dialog;
        InputStream is = null;
        mysql.setOnClickListener(new View.OnClickListener() 

            @Override
            public void onClick(View v) 
                new Insert().execute();
            
        );
    

【讨论】:

评论不用于扩展讨论;这个对话是moved to chat。【参考方案2】:

在 doInbackground() 中使用这个

runOnUiThread(new Runnable()     

                @Override  
                public void run()     
                    // TODO Auto-generated method stub

                
            ); 

【讨论】:

以上是关于通过android和php在mysql数据库中插入空白记录的主要内容,如果未能解决你的问题,请参考以下文章

PHP从Android将JSON数据插入MySQL数据库

无法通过php在mysql表中插入数据

通过html和php表单将数据插入MySQL数据库时遇到问题[重复]

无法在 mysql 数据库表中显示从 android 应用程序插入的印地语值

通过 PHP 将数据插入 MySQL 表时出错

如何通过xampp连接php、MySQL和android?