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)有一些很好的例子。 –