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

标签 php mysql

我正在开发一个网站,该网站将有一个用户添加表单。下面的函数是addrecord()。当管理员用户创建新用户时,此函数会在 SQL 表中添加行。但是,每次添加新用户时,我都会在第一个 else 语句中遇到错误消息“用户名/密码未添加到联系人”。当我检查表时,访问级别和密码字段有数据,但我无法使用散列密码登录。任何人都可以帮忙,这段代码有什么问题吗?

谢谢, 六号狗

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;
    }

}

最佳答案

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

关于PHP 函数(添加记录)无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32301193/

相关文章:

php - 使用 array_multisort 对多维数组进行排序,其中您不知道数组的维度

mysql - 正则表达式获取特定字符串第一次和第二次出现之间的字符

php - 为博客构建 MySQL 数据库以包含 "labels"或 "tags"

mysql - 为什么这个简短的 SQL 文件会给我错误?

php - 静态方法比非静态方法快吗?

php - 错误 '.$row["column_name_here"]。'编码

php - 如何从 phpmyadmin CURRENT_TIMESTAMP 获取小时和分钟

c# - 当返回行时,如何修复 reader.Read() 部分中的输入格式不正确?

php - 通过单击链接将数据更新到 mysql 数据库

php - PHP 命令行性能缓慢 - 这是正常现象还是我有安装问题?