在我们注册时自动创建一个随机代码作为用户ID。在我们保存个人资料信息的Android应用程序中我不能用户ID不同。所以如何管理所有活动中的用户ID和片段保持相同的用户ID,直到注销帮助我............................... ................ **
注册代码:
<?php
session_start();
if($_SERVER['REQUEST_METHOD']=='POST'){
include_once("db_connect.php");
$username = $_POST['user_name'];
$useremail = $_POST['user_email'];
$usermobile = $_POST['user_mobile'];
$password = $_POST['password'];
function randomstring($len) {
$string = "";
$chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
for($i=0;$i<$len;$i++)
$string.=substr($chars,rand(0,strlen($chars)),1);
return $string;
}
$rndm_code=randomstring(5);
$CheckSQL = "SELECT * FROM user WHERE user_email='$useremail'";
$check = mysqli_fetch_array(mysqli_query($con,$CheckSQL));
if(isset($check)){
echo 'Email Already Exist';
}
else{
$Sql_Query = "insert into user (user_id,user_name,user_email,user_mobile,password) values ('$rndm_code','$username','$useremail','$usermobile','$password')";
if (mysqli_query($con,$Sql_Query)){
echo 'User Registration Successfully';
}
else
{
echo 'Try Again';
}
}
}
mysqli_close($con);
?>
Android代码:
private void registerUser() {
isConnectingToInternet();
final String username = signupname.getText().toString().trim();
final String email = signupemail.getText().toString().trim();
final String phone = signupphone.getText().toString().trim();
final String password = signuppassword.getText().toString().trim();
pDialog = new ProgressDialog(SignupActivity.this);
pDialog.setMessage("Signing Up.. Please wait...");
pDialog.setCancelable(false);
pDialog.show();
StringRequest stringRequest = new StringRequest(Request.Method.POST, Config.URL_REGISTER,
new Response.Listener<String>() {
@Override
public void onResponse(String ServerResponse) {
// Hiding the progress dialog after all task complete.
pDialog.dismiss();
// Showing response message coming from server.
Toast.makeText(SignupActivity.this, ServerResponse, Toast.LENGTH_LONG).show();
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
// Hiding the progress dialog after all task complete.
pDialog.dismiss();
// Showing error message if something goes wrong.
Toast.makeText(SignupActivity.this,"Check Internet connection", Toast.LENGTH_LONG).show();
}
}) {
@Override
protected Map<String, String> getParams() {
// Creating Map String Params.
Map<String, String> params = new HashMap<String, String>();
// Adding All values to Params.
params.put("user_name",username);
params.put("user_email",email);
params.put("user_mobile", phone);
params.put("password",password);
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(SignupActivity.this);
requestQueue.add(stringRequest);
}**
答案 0 :(得分:1)
除响应文本外,您的服务器响应还需要包含ID。通常,这是通过将响应格式化为JSON来完成的。
而不仅仅是&#34;用户注册成功&#34;,PHP脚本的响应可能是
zip END header not found
您可以在PHP中使用json_encode从数组中为您创建此格式。 代码看起来像这样
{
"message":"User Registration Successfully",
"userId":<your_user_id_goes_here>
}
最好在发送响应后退出,这样你的JSON deos就会因为你不小心在某个地方稍后回复某些东西而无法使用。
在Android应用程序中,您将从JSON获取数据 - 我在android中没有经验,但我认为您可以通过使其成为JsonObjectRequest而不是StringRequest,然后显示serverResponse.getString(& #34; message&#34;)并将serverResponse.getString(&#34; userId&#34;)保存为变量中的id。
答案 1 :(得分:0)
<?php
session_start();
if($_SERVER['REQUEST_METHOD']=='POST'){
include_once("db_connect.php");
$username = $_POST['user_name'];
$useremail = $_POST['user_email'];
$usermobile = $_POST['user_mobile'];
$password = $_POST['password'];
function randomstring($len) {
$string = "";
$chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
for($i=0;$i<$len;$i++)
$string.=substr($chars,rand(0,strlen($chars)),1);
return $string;
}
$rndm_code=randomstring(5);
$checkUser = "SELECT * FROM user WHERE user_email='$useremail' OR user_mobile='$usermobile'";
$userStatus = mysqli_fetch_array(mysqli_query($con,$checkUser));
if(isset($userStatus)){
die(json_encode(array("success"=>0,"message"=>"Email or Mobile no already exist!!")));
}
else
{
$sql =$con->prepare( "INSERT INTO user(user_id,user_name,user_email,user_mobile,password) VALUES ('$rndm_code','$username', '$useremail', '$usermobile','$password')");
$sql->bind_param("ssss",$rndm_code, $username, $useremail, $usermobile,$password);
if($sql->execute())
{
echo(json_encode(array("success"=>1,"message"=>"Account created successfully!! Please Login")));
}
else
{
die("Something Error".$sql."<br>".mysqli_error($con));
}
}
}else{
die(json_encode(array("success"=>0,"message"=>"Empty Request Parameters..!!")));
}
mysqli_close($con);
?>