如何将android值插入phpmyadmin数据库

时间:2017-08-17 16:54:06

标签: php android phpmyadmin

我想在phpmyadmin中将我的应用程序android中的值插入数据库, 我调试了我的应用程序,我认为数据已经退出了应用程序。 我认为问题可能出在PHP文件或数据库中

Java代码

public class SignUp extends Fragment {

EditText fName, lName, userName, mail, pass;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.activity_photographer_sign_up,container, false);

    fName = (EditText)view.findViewById(R.id.fname);
    lName = (EditText)view.findViewById(R.id.lname);
    userName = (EditText)view.findViewById(R.id.username);
    mail = (EditText)view.findViewById(R.id.email);
    pass = (EditText)view.findViewById(R.id.password);
    Button btn=(Button)view.findViewById(R.id.btn);
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            PhotographerSignUp.Register(getContext(),fName.getText().toString(),lName.getText().toString(),userName.getText().toString(),mail.getText().toString(),pass.getText().toString());
        }
    });
    return view;
}

public static void Register(Context context, String firstname, String lastname, String username, String mail, String password){

    HashMap<String,String> h = new HashMap<String,String>();
    h.put("fName",firstname);
    h.put("lName",lastname);
    h.put("userName",username);
    h.put("mail",mail);
    h.put("pass",password);
    PhotographerSignUp.dopost(context,"http://localhost/client/insert.php",h);
}
static void dopost(final Context context, final String url, final HashMap<String ,String> params){

    new AsyncTask<Void,Void,String>()
    {
        ProgressDialog progressDialog;

        @Override
        protected void onPreExecute() {

            progressDialog = new ProgressDialog(context);
            progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            progressDialog.setMessage("Loading....");
            progressDialog.show();
            super.onPreExecute();
        }
        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            if (s!=null) { progressDialog.hide();}
        }

        @Override
        protected String doInBackground(Void... voids) {
            try {

                URL UrlConnection = new URL(url);
                HttpURLConnection urlConnection = (HttpURLConnection) UrlConnection.openConnection();
                try {
                    urlConnection.setDoInput(true);
                    urlConnection.setDoOutput(true);
                    urlConnection.setRequestMethod("POST");
                    OutputStream os = urlConnection.getOutputStream();
                    BufferedWriter writer = new BufferedWriter(
                            new OutputStreamWriter(os, "UTF-8"));
                    for(String key : params.keySet())
                    {
                        writer.write(key+"="+params.get(key)+"&");
                    }
                    writer.flush();
                    writer.close();
                    os.close();

                    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
                    StringBuilder stringBuilder = new StringBuilder();
                    String line;
                    while ((line = bufferedReader.readLine()) != null) {
                        stringBuilder.append(line).append("\n");
                    }
                    bufferedReader.close();

                    return stringBuilder.toString();
                }
                finally{
                    urlConnection.disconnect();
                }
            }
            catch(Exception e) {
                Log.e("ERROR", e.getMessage(), e);
                // return null;
            }
            return "";
        }
    }.execute();

}

}

将应用程序与数据库连接的PHP代码

connection.php

<?php
 define('hostname', 'localhost');
 define('user', 'root');
 define('password', '');
 define('databaseName', 'test');
 $connect = mysqli_connect(hostname, user, password, databaseName);
 ?>

insert.php

<?php
if($_SERVER["REQUEST_METHOD"]=="POST"){
require 'connection.php';
createStudent();
}
function createStudent()
{
global $connect;

$fName = $_POST["fName"];   
$lName = $_POST["lName"];
$userName = $_POST["username"];
$mail = $_POST["mail"]; 
$pass = $_POST["pass"];

$query = " Insert into info(fName,lName,userName,mail,pass) values 
('$fName','$lName','$userName','$mail','$pass');";

mysqli_query($connect, $query) or die (mysqli_error($connect));
mysqli_close($connect);

}
?>

我的数据库 enter image description here

0 个答案:

没有答案