【发布时间】:2015-08-08 14:31:06
【问题描述】:
所以我为我的页面创建注册,单击“提交”按钮后出现此错误...我创建了 mysql 表,但我不知道为什么会显示此错误... 错误:
警告:mysqli_connect(): (HY000/2013): 与 MySQL 的连接丢失 服务器在“读取初始通信数据包”,系统错误:95 /home/u771616840/public_html/FreeDay/PHP 中的“不支持操作” 表格 2/configdb.php 在第 2 行
数据库错误
注册码:
<?php
session_start();
include('configdb.php');
if(isset($_POST['submit']))
{
//whether the username is blank
if($_POST['username'] == '')
{
$_SESSION['error']['username'] = "User Name is required.";
}
//whether the email is blank
if($_POST['email'] == '')
{
$_SESSION['error']['email'] = "E-mail is required.";
}
else
{
//whether the email format is correct
if(preg_match("/^([a-zA-Z0-9])+([a-zA-Z0-9._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9._-]+)+$/", $_POST['email']))
{
//if it has the correct format whether the email has already exist
$email= $_POST['email'];
$sql1 = "SELECT * FROM user WHERE email = '$email'";
$result1 = mysqli_query($mysqli,$sql1) or die(mysqli_error());
if (mysqli_num_rows($result1) > 0)
{
$_SESSION['error']['email'] = "This Email is already used.";
}
}
else
{
//this error will set if the email format is not correct
$_SESSION['error']['email'] = "Your email is not valid.";
}
}
//whether the password is blank
if($_POST['password'] == '')
{
$_SESSION['error']['password'] = "Password is required.";
}
//if the error exist, we will go to registration form
if(isset($_SESSION['error']))
{
header("Location: index.php");
exit;
}
else
{
$username = $_POST['username'];
$email = $_POST['email'];
$password = $_POST['password'];
$com_code = md5(uniqid(rand()));
$sql2 = "INSERT INTO user (username, email, password, com_code) VALUES ('$username', '$email', '$password', '$com_code')";
$result2 = mysqli_query($mysqli,$sql2) or die(mysqli_error());
if($result2)
{
$to = $email;
$subject = "Confirmation from TutsforWeb to $username";
$header = "TutsforWeb: Confirmation from TutsforWeb";
$message = "Please click the link below to verify and activate your account. rn";
$message .= "http://www.yourname.com/confirm.php?passkey=$com_code";
$sentmail = mail($to,$subject,$message,$header);
if($sentmail)
{
echo "Your Confirmation link Has Been Sent To Your Email Address.";
}
else
{
echo "Cannot send Confirmation link to your e-mail address";
}
}
}
}
?>
配置数据库文件:
<?php
$mysqli=mysqli_connect('localhost','dbusername','dbpassword','databasename') or die("Database Error");
?>
感谢帮助:)
【问题讨论】:
-
这对你没有帮助
or die("Database Error")这对你有帮助or die(mysqli_error($mysqli)) -
一旦您按照@Fred-ii- 更改了代码。然后尝试将
'localhost'更改为'127.0.0.1' -
好吧,伙计们,我按照你说的做了,现在它显示 2 个错误...第一-
Warning: mysqli_connect(): (HY000/2003): Can't connect to MySQL server on '127.0.0.1' (111 "Connection refused") in /home/u771616840/public_html/FreeDay/PHP Form 2/configdb.php on line 2第二:Warning: mysqli_error() expects parameter 1 to be mysqli, boolean given in /home/u771616840/public_html/FreeDay/PHP Form 2/configdb.php on line 2 -
是你在本地运行的 mysql 守护进程
-
你应该阅读SQL注入,这段代码很容易受到攻击。