gpt4 book ai didi

php - 注册页面脚本: Fatal error

转载 作者:行者123 更新时间:2023-11-29 23:27:43 24 4
gpt4 key购买 nike

我刚刚为一个简单的注册页面编写了一个脚本,当我尝试使用它时,我得到了

Fatal error: Call to a member function query() on a non-object  on line 60

第 60 行是

$result = $mysqli->query("SELECT username from users WHERE username = '{$username}' LIMIT 1");

这是完整的脚本:

<html>
<head>
<title> Register </title>
</head>

<center>
<font color="green">
<body background=background.png no-repeat center center fixed;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
>
<h1> Register</h1>

<?php
require_once("db_const.php");
if (!isset($_POST['submit'])) {
?>



<form action=" <?=$_SERVER['PHP_SELF']?>" method="post">
Username: <input type="text" name="username" /><br />
Password: <input type="password" name="password" /><br />
Age: <select name="Age">
<option value="12-13"> 12-13 </option>
<option value="14-15"> 14-15 </option>
<option value="16-17"> 16-17 </option>
<option value="18+"> 18+ </option>
</select>
<br />
Gender: <select name="Gender">
<option value="male"> male </option>
<option value="female"> female </option>
</select>
<br />


<input type="submit" name="submit" value="register" />
</form>
</font>
</center>
<?php
} else {
$mysqli = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_AGE, DB_Gender);
if ($mysqli->connect_errno) {
echo "<p>MySQL error no {$mysqli->connect_errno} : {mysqli->connect_error}</p>";
exit();
}
}

# prepare data for insertion
$username = $_POST['username'];
$password =$_POST['password'];
$age =$_POST['Age'];
$gender =$_POST['Gender'];

$exists = 0;
$result = $mysqli->query("SELECT username from users WHERE username = '{$username}' LIMIT 1");
if ($result->num_rows == 1) {
$exists = 1;
}

if ($exists == 1) echo "<p> Username already exists!</p>";
else {
$sql = "INSERT INTO 'users' ('id', 'username', 'password', 'age', 'gender')
VALUES (NULL, '{$username}', '{$password}', '{$age}'. '{$gender}')";

if ($mysql->query($sql)) {
//echo "New Record has id" .$mysql->insert_id;
echo "<p>Registered successfully!</p>";
} else{
echo "MySQL error no {mysql->errno} : {$mysql->error}</p>";
exit();
}
}



?>
</body>
</html>

请原谅我的笨拙。提前致谢!

最佳答案

替换下面的行

if ($mysql->query($sql)) {

if ($mysqli->query($sql)) {

关于php - 注册页面脚本: Fatal error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26828905/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com