注册页面脚本:致命错误


Registration page script: Fatal error

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

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)) {

相关文章: