mysql_query在成功时返回false

时间:2011-12-23 09:16:00

标签: php mysql

我在使用Windows 7,WAMP 2.2,MySQL 5.5.16,Apache 2.2.21和& Sons的开发环境中处理的表单上遇到问题。 PHP 5.3.8。每次执行查询时都会返回错误:

  

“。您的SQL语法有错误;请查看手册   对应于您的MySQL服务器版本,以便使用正确的语法   'INERT IGNORE INTO用户附近(用户名,hashed_pa​​ssword,   电子邮件)VALUES('用户名',''在第1行“

我决定将我的问题放在顶部,以便在查看我的代码时更好地参考,也就是如果您打算提供帮助,我在下面列出了每个源代码和文件名,如果您需要更多信息请告诉我,我会尽快提交。非常感谢你们。

问题#1:database.php包含我在这种形式中引用的唯一对象类MySQLDataBase(),当我尝试提交表单数据时,它将其插入到数据库中,但是mysql_query函数返回false而不是true这应该是它应该做的。我一直在研究这个问题,并且无法弄清楚我的语法在哪里不好,也没有任何其他可能出错的东西。任何帮助将非常感谢和任何提示。我是一名中等水平的PHP程序员,还有很多需要学习的东西。我被告知来这里寻求建议。

问题2:如果你注意在database.php中注意,在类MySQLDatabase下,有一个名为query()的实例函数。当我尝试在user.php中使用静态函数register_user时,它表示MySQL查询失败:没有选择数据库,当它显然被选中时,因为构造函数在声明它的datbase.php文件中运行。因此,如果我写User :: register_user($ username,$ password,$ email),它将返回“MySQL Query Failed:No Database Selected”错误。如果有人能够优雅地帮助我,我会非常感激。

这是导致问题的表单register.php:

<?php
require_once("../../includes/initialize.php");

f(isset($_POST['register'])){
$username = $_POST['username'];
$password = $_POST['password'];
$email = $_POST['email'];
$message = "";
$errors = 0;

if(empty($username)){
    $errors++;
    $message = "Please enter your username." . "<br />";
} if (empty($password)){
    $errors++;
    $message .= "Please enter your password." . "<br /";
} if (empty($email)){
    $errors++;
    $message .= "Please enter your email address." . "<br />";
} if (strlen($username) > 15 || strlen($password) > 15 || strlen($email) > 32 ){
    $errors++;
    $message .= "You entered too many characters for your username/password/email";
} if ($errors > 0){
    echo $message;
}


if($errors == 0){

  $username =  $database->escape_value(trim(htmlentities($_POST['username'])));
  $password =  $database->escape_value(trim(htmlentities($_POST['password'])));
  $email    =  $database->escape_value(trim(htmlentities($_POST['email'])));
  $hashed_password = hash('whirlpool', $password);

  $sql  = "INSER INTO users(username, hashed_password, email)";
  $sql .= "VALUES('{$username}','{$hashed_password}','{$email}')";    
  $resource = mysql_query($sql);

  if( $resource ){
      //log_action('Registered', "{$found_user->username} registered.");
      echo "User successfully registered " . "<br />" . "<a href = \"login.php\">Login Now!</a>";
  } else {
      echo "Registration failed please try again later." . mysql_error();
  }

} // end if($errors == 0)





}//end if(isset($_POST['resiter']))

?>
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Resister</title>
</head>

<body>
<div>
<p>Please enter your registration information:</p><br />
<form id = "loginForm" action = "register.php" method = "POST" /><br />
<input type = "username" name = "username" value = "" maxlength = "15" /><br />
<input type = "password" name = "password" value = "" maxlength = "15" /><br />
<input type = "email" name = "email" value = "" maxlength = "32" /><br />
<input type = "submit" name = "register" value = "Register!" /><br />
</form>
<a href = "login.php">Login Now!</a>
</div>
</body>
</html>


<?php

$database->close_connection();

?>

这是database.php,它包含在register.php中,但仅用于打开连接,选择数据库并关闭连接:

    <?php

    require(LIB_PATH.DS."config.php");

    class MySQLDatabase{

        private $connection;
        public $last_query;
        private $magic_quotes_active;
        private $real_escape_string_exists;

      function __contruct(){
       $this->open_connection();
        $this->magic_quotes_active = get_magic_quotes_gpc();
        $this->real_escape_string_exists = function_exists("mysql_real_escape_string");
      }

      public function open_connection(){
          $this->connection = mysql_connect(DB_SERVER,DB_USER,_DB_PASS);
            if(!$this->connection){
                die("There was an error connecting to the database: " . mysql_error());
            } 
            $db_select = mysql_select_db(DB_NAME, $this->connection);
            if(!$db_select){
                die("There was an error selecting the database");
            }  
      }

      public function close_connection() {
           if(isset($this->connection)){
               mysql_close($this->connection);
               unset($this->connection);
           }
      }

      public function query($sql="") {
         $this->last_query = $sql;
         $result = mysql_query($sql);
         $this->confirm_query($result);
         return $result;
      }

      public function escape_value( $value ) {
        if($this->real_escape_string_exists){
           if($this->magic_quotes_active){
               $value = stripslashes( $value );
           }
           $value = mysql_real_escape_string ( $value );
        } else {
            if (!$this->magic_quotes_active){
                $value = addslashes ( $value );
            }
        }
        return $value;  
      }

      public function fetch_array( $result_set ){
          return mysql_fetch_array( $result_set );
      }

      public function num_rows( $result_set ){
          return mysql_num_rows( $result_set );
      }

      public function insert_id(){
          return mysql_insert_id($this->connection);
      }

      public function affected_rows(){
          return mysql_affected_rows($this->connection);
      }

      private function confirm_query( $result ){
          if ( !$result ){
              die ("MySQL Query failed: " . mysql_error());
          }
      }

    }

    $database = new MySQLDatabase();

    ?>

这是user.php,我遇到了query()函数问题:         

    require_once(LIB_PATH.DS."database.php");

    class User extends DatabaseObject {

      protected static $table_name="users";
      public $id;
      public $username;
      public $user;
      public $password;
      public $email;
      private $hashed_password;
      public $first_name;
      public $last_name;
      public $value;

      public static function authenticate($username="", $password=""){
       global $database;
       $username = $database->escape_value(htmlentities($username));
       $password = $database->escape_value(htmlentities($password));
       $hashed_password = hash('whirlpool', $password);

       $sql  = "SELECT * FROM users";
       $sql .= "WHERE username = '{$username}' ";
       $sql .= "AND hashed_password = '{$hashed_password}' ";
       $sql .= "LIMIT 1";
       $result_array = parent::find_by_sql($sql);
        return !empty($result_array) ? array_shift($result_array) : false;
      }

      public function register_user($username="", $password="", $email="") {
          global $database;
          $username = $database->escape_value(htmlentities($username));
          $password = $database->escape_value(htmlentities($password));
          $hashed_password = hash('whirlpool', $password);
          $email = $database->escape_value(htmlentities($email));

          $sql = "INSERT INTO users(username, hashed_password, email)";
          $sql .= " VALUES ('{$username}', '{$hashed_password}', '{$email}')";

          $database->query($sql);
      }

      public function full_name() {
          if(isset($this->first_name) && isset($this->last_name)) {
              return $this->first_name . " " . $this->last_name;
          } else {
              return "";
          }
      }

      public static function user_exists($user="") {
          global $database;
          $user = $database->escape_value($user);

          $sql  = "SELECT * FROM users";
          $sql .= "WHERE username = {$username}";

          $result_set = parent::find_by_sql($sql);
          if( $database->num_rows($result_set) >=1 ) {
              return true;
          }

      }


    }

    ?>

2 个答案:

答案 0 :(得分:3)

".You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INERT IGNORE INTO users(username, hashed_password, email)VALUES('username','' at line 1 "

您只需将INERT更改为INSERT

即可

答案 1 :(得分:0)

您的插入查询中似乎有拼写错误。将INSER INTO更改为INSERT INTO并尝试。