0
我有一个移动应用程序,允许技术人员进行登记,我要防止重复电子邮件地址和手机号码,我在我的PHP文件写了这个代码Android应用:如何避免重复的电子邮件和电话号码,当人们登记
<?php
require "config.php";
$name = $_POST['name'];
$password = $_POST['userpass'];
$emailadd = $_POST['emailadd'];
$phone = $_POST['phone'];
$category = $_POST['category'];
$token = $_POST['token'];
$username = stripslashes($username);
$password = stripslashes($password);
$sql_get_email= "SELECT * FROM technician where emailadd ='$emailadd';";
$result1 = mysqli_query($db, $sql_get_email);
$row = mysqli_fetch_array($result1,MYSQLI_ASSOC);
$count = mysqli_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row
if($count >0) {
echo "Email already exists in our database";
die();
}
else {
$mysql_get_phone = "SELECT * FROM technician where mobile ='$phone';";
$result1 = mysqli_query($db, $sql_get_email);
$row = mysqli_fetch_array($result1,MYSQLI_ASSOC);
$count = mysqli_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row
if($count >0) {
echo "Pnone number already exists in our database";
die();
}
else {
$sql = "INSERT INTO technician (name, emailadd, password, mobile, category, fcm_key)
VALUES ('$name', '$emailadd', '$password','$phone', '$category','$token')";
if ($db->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $db->error;
}
}}
$db->close();
?>
,我有这个摘自我BackgroundTask.java检查登记
@Override
protected void onPostExecute(String result) {
if (result.equals("Email already exists in our database")) {
final Dialog dialog = new Dialog(ctx);
dialog.setContentView(R.layout.dialog_second);
dialog.setTitle("ERROR");
dialog.setCancelable(true);
//set up text
TextView text = (TextView) dialog.findViewById(R.id.TextView01);
text.setText(result);
//set up button
Button button = (Button) dialog.findViewById(R.id.btnOk);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
//now that the dialog is set up, it's time to show it
dialog.show();
} else if (result.equals("Pnone number already exists in our database")) {
final Dialog dialog = new Dialog(ctx);
dialog.setContentView(R.layout.dialog_second);
dialog.setTitle("ERROR");
dialog.setCancelable(true);
//set up text
TextView text = (TextView) dialog.findViewById(R.id.TextView01);
text.setText(result);
//set up button
Button button = (Button) dialog.findViewById(R.id.btnOk);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
//now that the dialog is set up, it's time to show it
dialog.show();
}else
if (result.equals("Registration Success...")) {
Toast.makeText(ctx, "Registration Successful......Please login", Toast.LENGTH_LONG).show();
Intent intent = new Intent(ctx, Login.class);
ctx.startActivity(intent);
我发现登记始终是即使有重复录入成功的结果。
的config.php
<?php
define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'proartisan_dbadmin');
define('DB_PASSWORD', 'YYYYYYYYY');
define('DB_DATABASE', 'XXXXXXXXX');
$db = mysqli_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);
?>
简单的方法创建一个PHP文件,检查电子邮件和电话是否存在,当用户输入数据发送到PHP并检查是否存在,获得回应如果回答是没有注册如果没有注册,如果你需要我可以创建一个详细的答案 –
重复计数时$ count的结果是什么,何时不重复 – Thorny84
您的代码容易受到[** SQL注入**](https://en.wikipedia.org/wiki/SQL_injection)攻击的影响。您应该通过[** mysqli **](https://secure.php.net/manual/en/mysqli.prepare.php)或[** PDO **](https ://secure.php.net/manual/en/pdo.prepared-statements.php)驱动程序。 [**这篇文章**](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php)有一些很好的例子。 –