我很新的PHP & stackoverflow。 只是想通過電子郵件複製檢查做一個註冊表單。 嘗試添加新用戶時工作正常,但問題在於嘗試更新現有用戶時。 「它說,電子郵件已經存在的」我怎樣才能解決這個問題,即如果現有用戶試圖更新自己的信息,將不檢查重複 下面是我的代碼:php註冊驗證錯誤。
case 'users':
if(isset($_POST['submitted']) == 1) {
$first = mysqli_real_escape_string($dbc, $_POST['first']);
$last = mysqli_real_escape_string($dbc, $_POST['last']);
$email = mysqli_real_escape_string($dbc, $_POST['email']);
if($_POST['password'] == '' OR $_POST['password2'] == '' OR $_POST['password'] != $_POST['password2']) {
$message = '<p class="alert alert-danger">Both password field mus be filled & matching</p>';
} if(strlen($_POST['password'])<6 OR strlen($_POST['password'])>20) {
$message = '<p class="alert alert-danger">Password must be 6 to 20 characters</p>';
}
if ($first =='' OR $last =='') {
$message = '<p class="alert alert-danger">How should we address you ?</p>';
}
$q = "SELECT * FROM users WHERE email = '$email'";
$r = mysqli_query($dbc,$q) or die(mysqli_error());
if (mysqli_num_rows($r) > 0)
{
$message = '<p class="alert alert-danger">This Email is already used.</p>';
}
if (empty($email) OR !filter_var($email, FILTER_VALIDATE_EMAIL)) {
$message = $message.'<p class="alert alert-danger">Please provide a valid email address </p>';
}
if(isset($_POST['id']) != '') {
$action = 'updated';
$q = "UPDATE users SET first = '$first', last = '$last', email = '$_POST[email]', password = md5(SHA1('$_POST[password]')), status = $_POST[status] WHERE id = $_GET[id]";
$r = mysqli_query($dbc, $q);
} else {
$action = 'added';
$q = "INSERT INTO users (first, last, email, password, status) VALUES ('$first', '$last', '$_POST[email]', md5(SHA1('$_POST[password]')), '$_POST[status]')";
if($message == '') {
$r = mysqli_query($dbc, $q);
}
}
if ($message == '') {
$message = '<p class="alert alert-success">User was '.$action.'!</p>';
}
}
if (isset($_GET['id'])){ $opened = data_user($dbc, $_GET['id']); }
break;
您可能應該使用不同的路線來更新細節,並且保持原樣。另外,只有在用戶登錄後才允許用戶更新。 –