谁使用localhost phpmyadmin将数据插入mysql数据库?

时间:2016-06-14 20:11:46

标签: java php android mysql phpmyadmin

我试图通过wamp服务器在我的msql数据库中插入一行。数据库位于localhost。

我使用android stuido作为ide,并且不会在日志中收到任何错误或警告。我找不到我的代码有什么问题。我猜测问题是使用httpclient还是php文件,因为新的用户名和密码已成功传递给NewProductActivty.java。如何解决这个问题?

这是我的java和php代码:

functions.php

<?php
$DB_HOST = "localhost";
$DB_DATABASENAME = "kimnerede";
$DB_USERNAME = "root";
$DB_PASS = "";
define("BASARISIZ", "-1");
define("BASARILI", "1");
define("ARKADAS_BULUNAMADI_ERROR", "-2");
define("ARKADAS_ZATEN_MEVCUT_ERROR", "-3");
define("PROFIL_BULUNAMADI_ERROR", "-4");

function dbConnect() {

    $db = mysqli_connect(DB_HOST, DB_DATABASENAME, DB_PASS, DB_USERNAME);

    if (!$db)
        return null;

    mysqli_query($db, 'SET NAMES utf8');

    return $db;

    }

其他功能:

function arkadasEkle($db, $name, $pass) {

    $sorgu = "INSERT INTO deneme123 (name, pass) VALUES ('$name', '$pass')";

    if(!mysqli_query($db, $sorgu))
        return false;

    return true;

}
?>

addclient.php

<?php

//require_once('functions.php'); 

include('functions.php');
if((!isset($_POST['name']) || empty($_POST['name'])) &&
   (!isset($_POST['pass']) || empty($_POST['pass'])))
    die(BASARISIZ);

$name= $_POST['name'];
$pass= $_POST['pass'];

$db = dbConnect();

if(arkadasEkle($db, $name, $pass))
    die(BASARILI);

die(BASARISIZ);

?>

这些php文件位于C:\wamp\www\kimnerede目录。

这是连接和插入的类:

public class NewProductActivity extends AsyncTask<String,Void,String>{

    public static final String KIM_NEREDE_BASE_URL = "http://10.0.2.2/kimnerede/";
    public static final String KIM_NEREDE_PROFIL_KAYDET_URL = KIM_NEREDE_BASE_URL + "addclient.php";
    private static final String TAG = "NetworkManager";

    private Context context;

    public NewProductActivity(Context context)
    {
        this.context = context;
    }

    public static String Ekle(String p_name, String p_pass)
    {
        BufferedReader in = null;

        try {

            HttpClient client = new DefaultHttpClient();
            HttpPost request = new HttpPost(KIM_NEREDE_PROFIL_KAYDET_URL);

            List<NameValuePair> parametreList = new ArrayList<NameValuePair>();
            parametreList.add(new BasicNameValuePair("name", p_name));
            parametreList.add(new BasicNameValuePair("pass", p_pass));

            HttpResponse response = client.execute(request);

            in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));


            return in.readLine();
        } catch (Exception e) {
            Log.d(TAG, "Profil kaydedilirken hata olustu", e);
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

        return null;

    }

    @Override
    protected String doInBackground(String... params) {

        Ekle(params[0],params[1]);

        return null;
    }
}

通过在LoginActivity.java中创建对象来调用此类:

public void SignUpClicked(View view) {

        new NewProductActivity(this).execute(UserName.getText().toString(),Password.getText().toString());

    }

2 个答案:

答案 0 :(得分:0)

如果您没有收到任何错误,请确保在php.ini文件中将其设置为actually display错误:

display_errors = on

还要确保已设置:

error_reporting = E_ALL & ~E_NOTICE

答案 1 :(得分:0)

<强> connection.php

<?php
$connection = mysqli_connect("localhost", "root", "", "kimnerede") or die("unable to connect to the database");
?>

<强> insertData.php

<?php
$name = $_REQUEST['name'];
$pass = $_REQUEST['pass'];

include_once("connection.php");

$query = "INSERT INTO deneme123 (name, pass) VALUES ('$name', '$pass')";
mysqli_query($connection, $query) or die("unable to execute the query");
?>

<强> DBOperations.java

public class DBOperations {
public boolean addDataToDB(String name, String pass){
        try{
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
            try {
                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost("http://www.sample.com/phpfilelocation/insertData.php");
                List<NameValuePair> nameValuePairs = new ArrayList<>();
                nameValuePairs.add(new BasicNameValuePair("name", name));
                nameValuePairs.add(new BasicNameValuePair("pass", pass));
                httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                httpclient.execute(httpPost);
                return true;
            }
            catch (Exception ex) {
               ex.printStackTrace();
               return false;
            }
        }
        catch(Exception e){
            e.printStackTrace();
            return false;
        }
    }
}

您的AsyncTask

public class NewProductActivity extends AsyncTask<String, Void, Boolean>{
String name, pass;
ProgressDialog progressDialog;
DBOperations dbo = new DBOperations():

        public NewProductActivity(String name, String pass){
            this.name = name;
        this.pass = pass;
        }

        @Override
        protected void onPreExecute() {
            progressDialog = ProgressDialog.show(context, "Please wait", "Sending data...", true);
        }

        @Override
        protected Boolean doInBackground(String... params) {
            try{
            boolean added = dbo.addDataToDB(name, pass);
            return added;
        }
        catch(Exception e){
            e.printStackTrace();
            return false;
        }
        }

      @Override
        protected void onPostExecute(Boolean result) {
        if(result){
            Toast.makeText(context, "successfully added.", Toast.LENGTH_SHORT).show();
        }
        else{
            Toast.makeText(context, "failed to add data.", Toast.LENGTH_SHORT).show();
        }

        progressDialog.dismiss();
    }   
}

运行AsyncTask,

new NewProductActivity("name", "pass").execute();

将此行添加到您的清单文件

<uses-permission android:name="android.permission.INTERNET" />