$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代码,但每当我尝试注册时都会给我这个错误:
注意:未定义的变量:第44行的conn inm F:\ Software \ xampp \ htdocs \ LAB3 \ connectivity-sign-up.php
致命错误:在null中调用成员函数query()
第44行的F:\ Software \ xampp \ htdocs \ LAB3 \ connectivity-sign-up.php
如何解决这个问题?有人请。
答案 0 :(得分:2)
这是错误的部分。您应该使用StackOverflow。
根据代码的外观,您可能需要更多地研究PHP和MySQL,然后再回过头来。
要回答您的问题,因为$conn
在函数中是无效的引用,(function SignUp() {
)您必须使用$GLOBALS["conn"]
。 (http://php.net/manual/en/reserved.variables.globals.php)
此$query1 = "SELECT userName,email";
无效。您不在查询中提供表名。
正如Phil在评论中所说,你直接使用POST数据打开了一个巨大的SQL注入漏洞。