I am using android studio to create a login sign up system for a simple app. I have problem saving the email data to the database, but other than email, everything else is saved successfully to the database. Also, when I take a further step to send confirmation email to user who just created an account, the email never sent out. I cannot find what's causing the email not being saved to the database. how to fix this problem?) I would like to have the email saved to the data base and the php send a confirmation email to the user. Thank you.
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();
}
}
}
> //this is the php code that saves the data to the database
$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);
?>
I think this line
$mysql_get_users = mysqli_query("SELECT * FROM....
should be something like
$mysql_get_users = mysqli_query($con, "SELECT * FROM.....
Other than that, the PHP code should work just fine, if you send it the correct information.
What I recommend is to try to do a bit of debugging to figure out where the problem is:
mysqli_query()
function returns true
or false
, depending if it succeeded or not. You should check that value to make sure it's true
Applying the items above should give you a good idea where the issue might be. In principle, in such situations, do the debugging and make sure each part behaves like you want. Check them individually.
When something doesn't work, either it's broken or one of your assumptions is wrong. So always check your assumptions.
PS: As a side-note, your code has multiple issues in terms of structure, logic, best practices, security-holes and so on. It's not part of your question, but I suggest learning more about some of this stuff and improving it before releasing it in a production system.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.