這個腳本工作3天前。但現在在每一次註冊時,它都會給我錯誤,說明Usernamen未定義。密碼未定義且電子郵件未定義。註冊錯誤 - 在註冊腳本變量不isset
看來,$的用戶名,密碼$ $和電子郵件變量沒有isset。
這是從我的error_log:
PHP的警告:mysql_real_escape_string()[function.mysql實時逃逸字符串]:拒絕訪問用戶 '根' @ 'localhost' 的(使用密碼:NO)在/registerScr.php在線52 PHP的警告:mysql_real_escape_string()[function.mysql實時逃逸字符串]:到服務器的連接不能在/registerScr.php在線52
在52行成立我有我有mysql_real_escape_string函數。我搜索關於解決方案的stackoverflow,但沒有解決我的問題。
代碼是:
<?
/* Include Files *********************/
session_start();
include("db.php"); //Database connection
include("lScr.php"); // check if I am connected
/*************************************/
?>
<!DOCTYPE html>
<html>
................
<?
function protect($string){
$string = mysql_real_escape_string($string);
$string = strip_tags($string);
$string = addslashes($string);
return $string;
}
echo " <br>
<form class=\"form-horizontal\" action=\"\" method=\"post\">
<fieldset>
<div class=\"control-group\">
<label class=\"control-label\" for=\"input01\">Username </label>
<div class=\"controls\">
<input type=\"text\" class=\"input-xlarge\" id=\"input01\" name=\"username\" maxlength=\"30\" placeholder=\"Your username\">
</div>
</div>
<div class=\"control-group\">
<label class=\"control-label\" for=\"input01\">Password </label>
<div class=\"controls\">
<input type=\"password\" class=\"input-xlarge\" id=\"input01\" name=\"password\" maxlength=\"30\" placeholder=\"Your password\">
</div>
</div>
<div class=\"control-group\">
<label class=\"control-label\" for=\"input01\">Confirm Password </label>
<div class=\"controls\">
<input type=\"password\" class=\"input-xlarge\" id=\"input01\" name=\"passconf\" maxlength=\"30\" placeholder=\"Your password,again!\">
</div>
</div>
<div class=\"control-group\">
<label class=\"control-label\" for=\"input01\">E-mail </label>
<div class=\"controls\">
<input type=\"text\" class=\"input-xlarge\" id=\"input01\" name=\"email\" placeholder=\"Your e-mail\">
</div>
</div>
<div class=\"control-group\">
<label class=\"control-label\" for=\"input01\">Enter Captcha </label>
<div class=\"controls\">";
require_once('recaptchalib.php');
$publickey = "examplekey******"; // you got this from the signup page
echo recaptcha_get_html($publickey);
echo"
</div>
</div>
<br/><div class=\"control-group\">
<div class=\"controls\">
<input type=\"submit\" class=\"btn btn-primary\" name=\"submit\" value=\"Register\">
</div>
</div>
</fieldset>
</form>";
?>
</div>
<div class="span4">
<? if (isset($_POST['submit'])) {
$username = protect($_POST['username']);
$password = protect($_POST['password']);
$confirm = protect($_POST['passconf']);
$email = protect($_POST['email']);
$errors = array();
if(!$username){
$errors[] = "Username is not defined!";
}
if(!$password){
$errors[] = "Password is not defined!";
}
if($password){
if(!$confirm){
$errors[] = "Confirmation password is not defined!";
}
}
if(!$email){
$errors[] = "E-mail is not defined!";
}
if($username){
$aValid = array('-', '_');
if(!ctype_alnum(str_replace($aValid, '', $username))){
$errors[] = "Username can only contain numbers, letters and symbols like \"-\" or \"_\"!";
}
$range = range(1,32);
if(!in_array(strlen($username),$range)){
$errors[] = "Username must be between 1 and 32 characters!";
}
}
if($password && $confirm){
if($password != $confirm){
$errors[] = "Passwords do not match!";
}
}
if($email){
$checkemail = "/^[a-z0-9]+([_\\.-][a-z0-9]+)*@([a-z0-9]+([\.-][a-z0-9]+)*)+\\.[a-z]{2,}$/i";
if(!preg_match($checkemail, $email)){
$errors[] = "E-mail is not valid, must be [email protected]!";
}
}
if($username){
$sql = "SELECT * FROM `users` WHERE `username`='".$username."'";
$res = mysql_query($sql) or die(mysql_error());
if(mysql_num_rows($res) > 0){
$errors[] = "The username you supplied is already in use!";
}
}
if($email){
$sql2 = "SELECT * FROM `users` WHERE `email`='".$email."'";
$res2 = mysql_query($sql2) or die(mysql_error());
if(mysql_num_rows($res2) > 0){
$errors[] = "The e-mail address you supplied is already in use of another user!";
}
}
require_once('recaptchalib.php');
$privatekey = "examplekey*****";
$resp = recaptcha_check_answer ($privatekey,
$_SERVER["REMOTE_ADDR"],
$_POST["recaptcha_challenge_field"],
$_POST["recaptcha_response_field"]);
if (!$resp->is_valid) {
// What happens when the CAPTCHA was entered incorrectly
$errors[] = "Wrong Captcha. Please re-write Captcha!";
}
if(count($errors) > 0){
foreach($errors AS $error){
echo "<div class=\"alert alert-block alert-error fade in\">
<h4 class=\"alert-heading\"><li>$error</li></h4></div>";
}
}else {
$sql4 = "INSERT INTO `users`
(`username`,`password`,`email`)
VALUES ('".$username."','".$password."','".$email."')";
$res4 = mysql_query($sql4) or die(mysql_error());
echo "Registed";
}
}
?>
db.php中代碼:
$conn = mysql_connect($dbhost, $dbuser, $dbpassword);
if (!$conn)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("db_example", $conn);
我已經對數據庫進行連接。看看:include(「db.php」); //數據庫連接 –
檢查數據庫連接是否正確。該錯誤是由於分貝連接 – user2092317
失敗它被連接,如果它不能連接它應該是模頭由於這種機能的研究中db.php中引起 - >模(「無法連接:」。mysql_error()); –