Android单选按钮值无法存储在phpmysql中:“必填字段缺失”
Posted
技术标签:
【中文标题】Android单选按钮值无法存储在phpmysql中:“必填字段缺失”【英文标题】:Android radio button value cannot be stored in phpmysql: "Required Fields are Missing" 【发布时间】:2017-08-14 03:18:56 【问题描述】:所以我在 android Studio 中创建了几个 editText 字段,每个字段都通过 php 连接到 mysql(我正在使用 xampp btw)。现在我的问题是,**在将 tbuyer
从 editText 视图更改为 RadioGroup 中的 2 RadioButton 即(r_tb; r_ntb
)后,它会返回“缺少必填字段”**x。
现在为了从这两个中获取字符串,我使用 RadioGroup 声明了带有条件的 String role=" ";
:
if (radioGroup.getCheckedRadioButtonId() == r_tb.getId())
role = "Trade Buyer";
else if (radioGroup.getCheckedRadioButtonId() == r_ntb.getId())
role = "Non Trade Buyer";
然后将它与将发送到哈希映射的参数一起传递:
@Override
protected Map<String, String> getParams() throws AuthFailureError //put all parameters that will be sent to hash map
Map<String, String> params = new HashMap<>();
params.put("cname", cname); //String is converted to final, because we'are using it in a class; refer to line 48-50
params.put("date", date);
params.put("lname", lname);
params.put("fname", fname);
params.put("mi", mi);
params.put("email", email);
params.put("city", city);
params.put("country", country);
params.put("tbuyer", role); // check code; String created from radio button
return params;
然后调用json的字符串请求,这样我们就知道注册成功与否了。但正如我所说,它返回 **Required fields are missing"
这些是我的 Buyer_Registration_Activity.java 代码:
import android.app.ProgressDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.example.smdojt.manilafame.R;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.Map;
public class Buyer_Registration_Activity extends AppCompatActivity implements View.OnClickListener
private EditText editTextCompanyName, editTextDate, editTextLastName, editTextFirstName, editTextMiddleInitial
, editTextCity, editTextCountry, editTextType, editTextEmail;
private Button buttonRegister;
private ProgressDialog progressDialog;
String role = "";
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_buyer_registration);
//this.setTitle("Buyer Registration");
Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
setSupportActionBar(myToolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
myToolbar.setNavigationIcon(R.drawable.ic_arrow_back_black_24dp);
editTextCompanyName = (EditText) findViewById(R.id.editTextCompanyName);
editTextDate = (EditText) findViewById(R.id.editTextDate);
editTextLastName = (EditText) findViewById(R.id.editTextLastName);
editTextFirstName = (EditText) findViewById(R.id.editTextFirstName);
editTextMiddleInitial = (EditText) findViewById(R.id.editTextMiddleInitial);
editTextCity = (EditText) findViewById(R.id.editTextCity);
editTextType = (EditText) findViewById(R.id.editTextType);
editTextCountry = (EditText) findViewById(R.id.editTextCountry);
editTextEmail = (EditText) findViewById(R.id.editTextEmail);
buttonRegister = (Button) findViewById(R.id.buttonRegister);
progressDialog = new ProgressDialog(this);
buttonRegister.setOnClickListener(this);
private void registerUser()
final String cname = editTextCompanyName.getText().toString().trim();
final String date = editTextDate.getText().toString().trim();
final String lname = editTextLastName.getText().toString().trim();
final String fname = editTextFirstName.getText().toString().trim();
final String mi = editTextMiddleInitial.getText().toString().trim();
final String email = editTextEmail.getText().toString().trim();
final String city = editTextCity.getText().toString().trim();
final String country = editTextCountry.getText().toString().trim();
final String tbuyer = editTextType.getText().toString().trim();
final RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
final RadioButton r_tb = (RadioButton) findViewById(R.id.radio_tb);
final RadioButton r_ntb = (RadioButton) findViewById(R.id.radio_ntb);
if (radioGroup.getCheckedRadioButtonId() == r_tb.getId())
role = "Trade Buyer";
else if (radioGroup.getCheckedRadioButtonId() == r_ntb.getId())
role = "Non Trade Buyer";
progressDialog.setMessage("Registering User...");
progressDialog.show();
StringRequest stringRequest = new StringRequest(Request.Method.POST,
com.example.smdojt.manilafame.sql_demo_2.Constants.URL_REGISTER,
new Response.Listener<String>()
@Override
public void onResponse(String response) //If there are no ERROR this method will be executed
progressDialog.dismiss();
//we will get the json object
try
JSONObject jsonObject = new JSONObject(response); //create json object
Toast.makeText(getApplicationContext(), jsonObject.getString("message"), Toast.LENGTH_LONG).show();
catch (JSONException e)
e.printStackTrace();
,
new Response.ErrorListener()
@Override
public void onErrorResponse(VolleyError error) // Else, this method will be executed
progressDialog.hide();
Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_LONG).show();
)
@Override
protected Map<String, String> getParams() throws AuthFailureError //put all parameters that will be sent to hash map
Map<String, String> params = new HashMap<>();
params.put("cname", cname); //String is converted to final, because we'are using it in a class; refer to line 48-50
params.put("date", date);
params.put("lname", lname);
params.put("fname", fname);
params.put("mi", mi);
params.put("email", email);
params.put("city", city);
params.put("country", country);
params.put("tbuyer", role); // check code; String created from radio button
//params.put("tbuyer", tbuyer);
return params;
;
//add stringRequest (Line 55)
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
@Override
public void onClick(View view)
if (view==buttonRegister)
registerUser();
这些是我的 DBOperations.php 代码(Crud 操作声明)
<?php
//manage all php and db operations
class DbOperations
private $con;
function __construct()
require_once dirname(__FILE__).'/DbConnect.php'; //import dbconnect to dboperations
$db = new DbConnect(); //create db connect object
$this->con = $db->connect();
//CRUD Operations below
//Create-----------------------------------------------------------------------------------------------
function registerBuyer($cname, $date, $lname, $fname, $mi, $email, $country, $city, $tbuyer)
//$password = md5($password); //encrypt password
$stmt = $this->con->prepare("INSERT INTO `mfmis_buyers`.`buyers` (`id`, `cname`, `date`, `lname`, `fname`, `mi`, `email`, `country`, `city`, `tbuyer`) VALUES (NULL, ?, ?, ?, ?, ?, ?, ?, ?, ?);"); //statement
//find actual parameters /bind param
$stmt->bind_param("sssssssss", $cname, $date, $lname, $fname, $mi, $email, $country, $city, $tbuyer); //bind to query
//execute query below
if ($stmt->execute()) //as soon as this line is called, data will be inserted into DB
return true;
else
return false;
//-----------------------------------------------------------------------------------------------------
?>
这些是我的 registerBuyer.php 代码(我的所有错误语句都来自其中,包括 “REQUIRED FIELDS ARE MISSING”)
<?php
require_once '../includes/DbOperations.php';
$response = array();
if($_SERVER['REQUEST_METHOD']=='POST')
if(
isset($_POST['cname']) and
isset($_POST['date']) and
isset($_POST['lname']) and
isset($_POST['fname']) and
isset($_POST['mi']) and
isset($_POST['email']) and
isset($_POST['country']) and
isset($_POST['city']) and
isset($_POST['tbuyer']))
//operate the data further
$db = new DbOperations();
if($db->registerBuyer(
$_POST['cname'],
$_POST['date'],
$_POST['lname'],
$_POST['fname'],
$_POST['mi'],
$_POST['email'],
$_POST['country'],
$_POST['city'],
$_POST['tbuyer']
))
$response['error'] = false;
$response['message'] = "Buyer Registered Successfully";
else
$response['error'] = true;
$response['message'] = "Some error occured try again";
else
$response['error'] = true;
$response['message'] = "Required fields are missing";
else
$response['error'] = true;
$response['message'] ="Invalid request";
echo json_encode($response);
?>
【问题讨论】:
请在 $response 中包含 $_POST 数据,如下所示:$response['post'] = $_POST;
以查看您在服务器端得到了什么并告诉我
@ConstantinGALBENU 所有$response
?所以在响应消息的每一行中,只需添加$response['post'] = $_POST;
?
只有在$response['message'] = "Required fields are missing";
之后才能调试这个;找到解决方案后,您将删除该语句
@ConstantinGALBENU 它不返回任何内容,只返回来自 StringRequest 的空白吐司
@ConstantinGALBENU 我为 .java 添加了另一个代码
【参考方案1】:
因此,基于role
字符串,它甚至不会推送单个数据,因为最初的连接甚至都不正确。
所以我上面提到的代码是有效的,唯一的障碍是我的 Constants.java。确保您的本地或在线连接已设置并与 xampp 服务器一致。
public class Constants
public static final String ROOT_URL = "http://192.168.15.186/MFMIS/register/";
public static final String URL_REGISTER = ROOT_URL+"registerBuyer.php";
确保您的String ROOT_URL
和URL_REGISTER
或任何其他本地和在线连接正确无误。否则,如果问题仍然存在,那么您的活动或 xampp 端有问题。
【讨论】:
以上是关于Android单选按钮值无法存储在phpmysql中:“必填字段缺失”的主要内容,如果未能解决你的问题,请参考以下文章