首页 > 解决方案 > 使用按钮将文本插入 mysql 数据库

问题描述

我正在做一个项目,我需要通过单击按钮将文本输入数据库。这两个选项是问题和演示。这两个选项都显示为按钮。例如,当我单击问题时,我想做的是……文本“问题”应该发送到我数据库中的列(称为决策)。

任何帮助,将不胜感激,

谢谢!

检查会话.php

<?php

//Define your host here.
$HostName = "localhost";

//Define your database username here.
$HostUser = "root";

//Define your database password here.
$HostPass = "";

//Define your database name here.
$DatabaseName = "queue_lnsconnection";

?>

选择一个.php

<?php
session_start();
include 'CheckSession.php';


$con = mysqli_connect($HostName,$HostUser,$HostPass,$DatabaseName);

  $Table_Name = $_REQUEST['test_android'];
   $Full_Name = $_SESSION['name'];
    $Decision = $_POST['decision'];


$CheckSQL = "SELECT table_name
FROM information_schema.tables
WHERE table_schema = 'queue_lnsconnection'
AND table_name = '". $Table_Name . "'";



// $check = mysqli_fetch_array(mysqli_query($con,$CheckSQL));
 $result = mysqli_query($con,$CheckSQL);
 //$row = mysqli_num_rows($result);
 //if(isset($check))

 if($result ==  true ){
    $Sql_Query = "INSERT INTO ".$Table_Name."(name,decision) values ('".$Full_Name."','".$Decision."')";

     if(mysqli_query($con,$Sql_Query))
    {
        echo 'Request added to Queue';
    }
    else
    {
        echo 'Something went wrong';
    }
 }

mysqli_close($con);

?>

决策活动.java

public class DecisionActivity extends AppCompatActivity{

    // Creating button;
    Button Question;
    Button Demo;

    // Creating Volley RequestQueue.
    RequestQueue requestQueue;


    // Creating Progress dialog.
    ProgressDialog progressDialog;

    // Storing server url into String variable.
    String HttpUrl = "http://192.168.2.150:8080/chooseOne.php";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_decision);
        Question = (Button) findViewById(R.id.decQuestion);
        Demo = (Button) findViewById(R.id.decDemo);
        // Creating Volley newRequestQueue .
        requestQueue = Volley.newRequestQueue(DecisionActivity.this);

        // Assigning Activity this to progress dialog.
        progressDialog = new ProgressDialog(DecisionActivity.this);
        Question.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                decisionChosen();
            }
        });
        Demo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                decisionChosen();
            }
        });

    }

    public void decisionChosen(){

        // Showing progress dialog at user registration time.
        progressDialog.setMessage("Please Wait, We are Inserting Your Data on Server");
        progressDialog.show();

        // Creating string request with post method.
        StringRequest stringRequest = new StringRequest(Request.Method.POST, HttpUrl,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String ServerResponse) {

                        // Hiding the progress dialog after all task complete.
                        progressDialog.dismiss();

                        // Showing Echo Response Message Coming From Server.
                        Toast.makeText(DecisionActivity.this, ServerResponse, Toast.LENGTH_LONG).show();
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {

                        // Hiding the progress dialog after all task complete.
                        progressDialog.dismiss();

                        // Showing error message if something goes wrong.
                        Toast.makeText(DecisionActivity.this, volleyError.toString(), Toast.LENGTH_LONG).show();
                    }
                }) {


        };

        // Creating RequestQueue.
        RequestQueue requestQueue = Volley.newRequestQueue(DecisionActivity.this);

        // Adding the StringRequest object into requestQueue.
        requestQueue.add(stringRequest);

    }
}

标签: androidandroid-volley

解决方案


推荐阅读