我想創建一個帳戶註冊頁面,當我添加一個系統來檢查數據庫並確保沒有多個具有相同用戶名的行時,我得到一個500錯誤。爲什麼我一直在用PHP獲取500錯誤?
下面是工作代碼:
<?php
if(isset ($_POST['submit']))
{
include('connection.php');
$username = $_POST['username'];
$password = $_POST['password'];
$email = $_POST['email'];
if(empty($username) || empty($email) || empty($password))
{
echo 'Please check the required fields.';
}
elseif(!filter_var($email,FILTER_VALIDATE_EMAIL))
{
echo 'Please enter a correct email address.';
}
else
{
$password = md5($password);
$sql = mysql_query("INSERT INTO users (email,username,password) VALUES ('$email','$username','$password')") or die(mysql_error());
if($sql)
{
echo 'Successfully submitted.';
}
}
}
?>
下面是給了我一個500錯誤代碼:
<?php
error_reporting(E_ALL)
if(isset ($_POST['submit'])) {
include('connection.php');
$username = $_POST['username'];
$password = $_POST['password'];
$email = $_POST['email'];
$fetch = mysql_query("SELECT * FROM users WHERE username = '$email'") or die(mysql_error();
$num_rows = mysql_num_rows($fetch);
if(empty($username) || empty($email) || empty($password)) {
echo 'Please check the required fields.';
}
elseif(!filter_var($email,FILTER_VALIDATE_EMAIL)) {
echo 'Please enter a correct email address.';
}
elseif($num_rows >= 1);
{
echo 'This username is taken.';
else
}
$password = md5($password);
$sql = mysql_query("INSERT INTO users (email,username,password) VALUES ('$email','$username','$password')") or die(mysql_error());
if($sql)
{
echo 'Successfully submitted.';
}
}
}
?>
如果您有權訪問錯誤日誌文件,請查看 – Utkanos 2012-07-07 22:10:02
您的代碼易受SQL注入攻擊。請看一下mysql_real_escape_string,或者考慮PDO。 – 2012-07-07 22:12:09
如果你已經在你的本地PC上安裝了php-cgi,那麼你至少可以用'php -l'來檢查你的代碼是否有錯誤。我花了60年的時間找到和@Jocelyn相同的錯誤 – TerryE 2012-07-07 22:18:48