gpt4 book ai didi

java - Android studio 应用程序未将数据保存到数据库并发送确认电子邮件

转载 作者:太空宇宙 更新时间:2023-11-04 10:52:48 25 4
gpt4 key购买 nike

我正在使用 android studio 为一个简单的应用程序创建一个登录注册系统。我在将电子邮件数据保存到数据库时遇到问题,但除了电子邮件之外,其他所有内容都已成功保存到数据库。此外,当我进一步向刚刚创建帐户的用户发送确认电子邮件时,该电子邮件从未发送出去。我找不到导调用子邮件未保存到数据库的原因。如何解决这个问题?)我希望将电子邮件保存到数据库中,并且 php 向用户发送一封确认电子邮件。谢谢。

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.os.AsyncTask;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import android.widget.EditText;
import android.widget.Toast;
import android.content.Context;
import java.io.OutputStream;
import java.net.MalformedURLException;

public class Register extends AppCompatActivity {

EditText name, password, confirm, first, email;
String Name, Password, Confirm, First, Email;
Context ctx=this;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.register);
name = (EditText) findViewById(R.id.register_name);
password = (EditText) findViewById(R.id.register_password);
confirm = (EditText) findViewById(R.id.register_confirm);
first = (EditText) findViewById(R.id.register_first);
email = (EditText) findViewById(R.id.register_email);
}

public void register_register(View v){
Name = name.getText().toString();
Password = password.getText().toString();
Confirm = confirm.getText().toString();
First = first.getText().toString();
Email = email.getText().toString();
BackGround b = new BackGround();
b.execute(Name, Password, Confirm, First, Email);
}

class BackGround extends AsyncTask<String, String, String>{

@Override
protected String doInBackground(String... params) {
String name = params[0];
String password = params[1];
String confirm = params[2];
String first = params[3];
String email = params[4];
String data="";
int tmp;

try {
URL url = new URL("php link");
String urlParams = "name="+name+"&password="+password+"&confirm="+confirm+"&first="+first+"&email"+email;

HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setDoOutput(true);
OutputStream os = httpURLConnection.getOutputStream();
os.write(urlParams.getBytes());
os.flush();
os.close();
InputStream is = httpURLConnection.getInputStream();
while((tmp=is.read())!=-1){
data+= (char)tmp;
}
is.close();
httpURLConnection.disconnect();

return data;

} catch (MalformedURLException e) {
e.printStackTrace();
return "Exception: "+e.getMessage();
} catch (IOException e) {
e.printStackTrace();
return "Exception: "+e.getMessage();
}
}

@Override
protected void onPostExecute(String s) {
if(s.equals("")){
s="Data saved successfully.";
}
Toast.makeText(ctx, s, Toast.LENGTH_LONG).show();
}
}
}

>//这是将数据保存到数据库的php代码

$name= mysqli_real_escape_string($con,$_POST['name']);  
$password= mysqli_real_escape_string($con,$_POST['password']);
$confirm= mysqli_real_escape_string($con,$_POST['confirm']);
$first= mysqli_real_escape_string($con,$_POST['first']);
$email= mysqli_real_escape_string($con,$_POST['email']);
$hash = md5(rand(0,1000));
$mysql_get_users = mysqli_query("SELECT * FROM userInfo where name = '$name'");

if (empty($name)) {
echo "Name is required\n";
}

else if (mysqli_num_rows($mysql_get_users) > 0){
echo "Username already exists";
}

else if (empty($password)){
echo "Password is required\n";
}

else if (empty($confirm)){
echo "Please confirmed the password\n";
}

else if (empty($first)){
echo "First name is required\n";
}
else if (!preg_match("/^[a-zA-Z'-]+$/",$first)){
echo "First name should only contains letters";
}

else if ($password != $confirm){
echo "Password does not match confirmation";
}

else{
$sql = "INSERT INTO userInfo (name, password, first, email, hash) VALUES ('$name', '$password', '$first', '$email', '$hash')";
if(!mysqli_query($con, $sql)){
echo "Unable to save the data to the database.";
}
else{
echo "Your account has been made, a verification email has been sent to you.";
$to = $email;
$subject = 'Signup| Verification';
$message = '

Thank you for signing up!
Your account has been created.
Please click this link to activate your account:

$headers = 'From:noreply@app.com' . "\r\n";
mail($to, $subject, $message, $headers);
}
}
}

mysqli_close($con);
?>

最佳答案

我认为这条线

$mysql_get_users = mysqli_query("SELECT * FROM....

应该是这样的

$mysql_get_users = mysqli_query($con, "SELECT * FROM.....

除此之外,如果您发送正确的信息,PHP 代码应该可以正常工作。

我建议尝试进行一些调试以找出问题所在:

  • 使用 Postman 等直接调用 PHP 代码并查看它显示的内容
  • 打开 PHP 的所有通知/警告/错误,以查看代码中的任何潜在问题
  • 对于插入,mysqli_query() 函数返回 truefalse,具体取决于插入是否成功。您应该检查该值以确保其为 true
  • 在 PHP 脚本中,将所有输入写入文件,以确保 Java 代码按照您期望的方式编写和发送它。
  • 关于发送电子邮件部分:您需要一个邮件服务器才能使该部分工作。这取决于 PHP 脚本运行的操作系统。

应用上述内容应该可以让您很好地了解问题可能出在哪里。原则上,在这种情况下,请进行调试并确保每个部分的行为符合您的要求。单独检查它们。

当某件事不起作用时,要么它坏了,要么你的假设之一是错误的。所以一定要检查你的假设。

PS:顺便说一句,您的代码在结构、逻辑、最佳实践、安全漏洞等方面存在多个问题。这不是你的问题的一部分,但我建议在将其发布到生产系统之前更多地了解其中的一些内容并对其进行改进。

关于java - Android studio 应用程序未将数据保存到数据库并发送确认电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47623083/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com