PHP函数(添加记录)无法正常工作

时间:2015-08-30 21:11:58

标签: php mysql

我正在开设一个用户添加表单的网站。以下函数是addrecord()。当admin用户创建新用户时,此函数会在SQL表中添加行。但是,每次添加新用户时,我都会在第一个else语句中错误地显示错误消息"用户名/密码未添加到联系人"检查表时,访问级别和密码字段包含数据,但我无法使用哈希密码登录。任何人都可以帮忙,这段代码有什么问题?

谢谢, Sixxdog

public function addRecord() {  

    // Verify the fields
    if ($this->_verifyInput()) {
        // prepare for the encrypted password
        $password = trim($_POST['password1']);

        // Get the Database connection
        $connection = Database::getConnection();

        // Prepare the data 
        $query = "INSERT INTO contacts(first_name, last_name, position, email, phone) 
        VALUES ('" . Database::prep($this->first_name) . "',
        '" . Database::prep($this->last_name) . "',
        '" . Database::prep($this->position) . "',
        '" . Database::prep($this->email) . "',
        '" . Database::prep($this->phone) . "')";
        // Run the MySQL statement 
        if ($connection->query($query)) { // this inserts the row
            // update with the user name and password now that you know the id
            $query = "UPDATE contacts 
            SET user_name = '" . Database::prep($this->user_name) . "', 
            password = '" . hash_hmac('sha512',
              $password . '!hi#HUde9' . mysql_insert_id(), 
              SITE_KEY) ."',
            access = '" . Database::prep($this->access) . "'";
            if ($connection->query($query)) { // this updates the row
              $return = array('', 'Contact Record successfully added.', '');   
              // add success message
              return $return;
            } else {
              // send fail message 
                $return = array('', 'User name/password not added to contact.', '');
                return $return;
            }

        } else {
            // send fail message and return to contactmaint
            $return = array('contactmaint', 'No Contact Record Added. Unable to create record.', '0');
            return $return;
        }
    } else {
        // send fail message and return to contactmaint
        $return = array('contactmaint', 'No Contact Record Added. Missing required information
        or problem with user name or password.', '0');
        return $return;
    }

}

1 个答案:

答案 0 :(得分:0)

更新语句中没有WHERE子句。也许user_name列上有唯一索引?