關鍵代碼:
//查詢郵箱是否重復
$user_email = $_POST["user_email"];
$sql = "select count(*) from users where user_email='$user_email'";
$result = mysql_query($sql,$con);
$row = mysql_fetch_row($result);
if ( $row[0] > 0)
{
echo $row[0];
echo "<br/>郵箱已注冊過,請直接登陸或找回密碼";}
else {
//----增加新的用戶--begin
--------問題更新-------
使用如下代碼,然後注冊時,輸入數據庫已經存在的郵箱,依然會注冊成功。
這是不正常的,請問問題出現在哪裡?
經過我的定位,好像“查詢郵箱是否重復”的代碼沒有生效,請各位幫忙看下?
//查詢郵箱是否重復
$user_email = $_POST["user_email"];
$sql = "select count(*) from users where user_email='$user_email'";
if (mysql_query($sql,$con)=='1')
{echo "exist";}
else {
//----增加新的用戶--begin
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<style>.error {color: red;}</style>
</head>
<body>
<?php
//檢查(1)郵箱、密碼、昵稱是否為空;(2)郵箱格式是否正確。
global $emailErr,$passErr,$nicknameErr;
$emailErr = $passErr = $nicknameErr = "";
//$user_email = $user_pass = $user_nickname = "";
if (empty($_POST["user_email"])) {$emailErr = "不可為空";}
else
{
if (!preg_match("/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/",$_POST["user_email"])) {$emailErr = "email格式不正確";}
else {$emailErr="ok";}
}
if (empty($_POST["user_pass"])) {$passErr = "不可為空";} else {$passErr = "ok";}
if (empty($_POST["user_nickname"])) {$nicknameErr = "不可為空";} else {$nicknameErr = "ok";}
?>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<p>注冊一個新用戶^_^</p>
郵箱:<input type="text" name="user_email"><span class="error">* <?php echo $emailErr;?></span><br><br>
密碼:<input type="text" name="user_pass"><span class="error">* <?php echo $passErr;?></span><br><br>
昵稱:<input type="text" name="user_nickname"><span class="error">* <?php echo $nicknameErr;?></span><br><br>
<input type="submit" name ='submit' value="注冊">
</form>
<?php
//判斷是否需要連接數據庫
if ( isset($_POST["submit"]) && $emailErr=="ok" && $passErr == "ok" && $nicknameErr == "ok" )
{
//連接數據庫 Kilo,20150909
$con = mysql_connect(SAE_MYSQL_HOST_M.":".SAE_MYSQL_PORT,SAE_MYSQL_USER,SAE_MYSQL_PASS);
$con or die ('Could not connect: ' . mysql_error());
//選擇數據庫
mysql_select_db(SAE_MYSQL_DB,$con);
//查詢郵箱是否重復
$user_email = $_POST["user_email"];
$sql = "select count(*) from users where user_email='$user_email'";
if (mysql_query($sql,$con)=='1')
{echo "exist";}
else {
//----增加新的用戶--begin
//獲取當前時間戮
$unix_timestamp=time();
//插入用戶注冊數據
$sql="INSERT INTO users
(user_email,user_pass,user_nickname,user_reg_ip,user_reg_date,user_last_log_ip,user_last_log_date,user_coins,user_rmb,user_email_status)
VALUES
('$_POST[user_email]','$_POST[user_pass]','$_POST[user_nickname]','$_SERVER[REMOTE_ADDR]','$unix_timestamp','$_SERVER[REMOTE_ADDR]','$unix_timestamp','0','0','0')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
else
{
//echo '<br />';
echo "注冊成功!".'<br/><br/>';
echo "很高興遇見你,".$_POST["user_nickname"].'<br/>';
$url='/login.php';
echo "<a href=$url>點此登陸吧</a>";
//斷開數據庫連接
mysql_close($con);
}
}
//----增加新的用戶--end
} else echo "<br/>:( 失敗了,請修改~";
?>
</body>
</html>