PHP 使用数据库时出现问题
PHP problem using database
$DB_HOST = "localhost";
$DB_USER = "root";
$DB_PASSWORD = "";
$DB_NAME = "practice";
$conn = new mysqli($DB_HOST,$DB_USER,$DB_PASSWORD,$DB_NAME);
if ($conn->connect_errno) {
printf("Connect failed: %s\n", $conn->connect_error);
exit();
}else{
//printf("connected!!!");
}
function NewUser() {
$fullname = $_POST['name'];
$userName = $_POST['user'];
$email = $_POST['email'];
$password = $_POST['pass'];
$query = "INSERT INTO websiteusers (fullname,userName,email,pass) VALUES ('$fullname','$userName','$email','$password')";
$data = $conn->query($query);
if($data) {
echo "YOUR REGISTRATION IS COMPLETED...";
}else{
echo "something wrong!!!";
}
}
function SignUp() {
if(!empty($_POST['user'])){
$query1 = "SELECT userName,email"; (line-44) //The Problem is in this line!!
$data1 = $conn->query($query1);
if($data1) {
NewUser();
} else {
echo "SORRY...YOU ARE ALREADY REGISTERED USER...";
}
}
}
if(!isset($_POST['submit'])) {
$result = $conn->query("SELECT * FROM websiteusers");
while ($row = $result->fetch_assoc()) {
printf ("%s (%s)\n", $row["userName"], $row["email"]);
}
//SignUp();
}else{
SignUp();
}
?>
这是我的 PHP 注册码,但每当我尝试注册时都会给我这个错误:
Notice: Undefined variable: conn inm F:\Software\xampp\htdocs\LAB3\connectivity-sign-up.php on line 44
Fatal error: Call to a member function query() on null in
F:\Software\xampp\htdocs\LAB3\connectivity-sign-up.php on line 44
如何解决这个问题?任何人都可以。
这是错误的部分。你应该使用 Whosebug。
根据代码的外观,您可能应该再研究一下 PHP 和 MySQL,然后再回过头来。
回答你的问题,是因为$conn
在函数中是无效引用,(function SignUp() {
)你必须使用$GLOBALS["conn"]
。 (http://php.net/manual/en/reserved.variables.globals.php)
这个$query1 = "SELECT userName,email";
是行不通的。您没有在查询中提供 table 名称。
正如菲尔在评论中所说,你直接使用POST数据打开了一个巨大的SQL注入孔。
$DB_HOST = "localhost";
$DB_USER = "root";
$DB_PASSWORD = "";
$DB_NAME = "practice";
$conn = new mysqli($DB_HOST,$DB_USER,$DB_PASSWORD,$DB_NAME);
if ($conn->connect_errno) {
printf("Connect failed: %s\n", $conn->connect_error);
exit();
}else{
//printf("connected!!!");
}
function NewUser() {
$fullname = $_POST['name'];
$userName = $_POST['user'];
$email = $_POST['email'];
$password = $_POST['pass'];
$query = "INSERT INTO websiteusers (fullname,userName,email,pass) VALUES ('$fullname','$userName','$email','$password')";
$data = $conn->query($query);
if($data) {
echo "YOUR REGISTRATION IS COMPLETED...";
}else{
echo "something wrong!!!";
}
}
function SignUp() {
if(!empty($_POST['user'])){
$query1 = "SELECT userName,email"; (line-44) //The Problem is in this line!!
$data1 = $conn->query($query1);
if($data1) {
NewUser();
} else {
echo "SORRY...YOU ARE ALREADY REGISTERED USER...";
}
}
}
if(!isset($_POST['submit'])) {
$result = $conn->query("SELECT * FROM websiteusers");
while ($row = $result->fetch_assoc()) {
printf ("%s (%s)\n", $row["userName"], $row["email"]);
}
//SignUp();
}else{
SignUp();
}
?>
这是我的 PHP 注册码,但每当我尝试注册时都会给我这个错误:
Notice: Undefined variable: conn inm F:\Software\xampp\htdocs\LAB3\connectivity-sign-up.php on line 44
Fatal error: Call to a member function query() on null in
F:\Software\xampp\htdocs\LAB3\connectivity-sign-up.php on line 44
如何解决这个问题?任何人都可以。
这是错误的部分。你应该使用 Whosebug。
根据代码的外观,您可能应该再研究一下 PHP 和 MySQL,然后再回过头来。
回答你的问题,是因为
$conn
在函数中是无效引用,(function SignUp() {
)你必须使用$GLOBALS["conn"]
。 (http://php.net/manual/en/reserved.variables.globals.php)这个
$query1 = "SELECT userName,email";
是行不通的。您没有在查询中提供 table 名称。正如菲尔在评论中所说,你直接使用POST数据打开了一个巨大的SQL注入孔。