【发布时间】:2013-10-13 11:22:51
【问题描述】:
编辑:我意识到我的问题是我试图将具有数字字符串的 $user_id 插入到列 user_id 中,该列是 int 类型。我将字符串转换为整数。我还是不太明白虽然有错误,但那是因为时间限制。我会在其他时间回复它。
我正在尝试在http://www.php-login.net 提供的 php 登录系统的高级版本之上构建一个网站。在脚本的 Registration 类中,有一个向数据库添加新用户的函数,该代码运行良好:
$query_new_user_insert = $this->db_connection->prepare('INSERT INTO users (user_name, user_password_hash, user_email, user_activation_hash, user_registration_ip, user_registration_datetime) VALUES(:user_name, :user_password_hash, :user_email, :user_activation_hash, :user_registration_ip, now())');
$query_new_user_insert->bindValue(':user_name', $user_name, PDO::PARAM_STR);
$query_new_user_insert->bindValue(':user_password_hash', $user_password_hash, PDO::PARAM_STR);
$query_new_user_insert->bindValue(':user_email', $user_email, PDO::PARAM_STR);
$query_new_user_insert->bindValue(':user_activation_hash', $user_activation_hash, PDO::PARAM_STR);
$query_new_user_insert->bindValue(':user_registration_ip', $_SERVER['REMOTE_ADDR'], PDO::PARAM_STR);
$query_new_user_insert->execute();
// id of new user
$user_id = $this->db_connection->lastInsertId();
if ($query_new_user_insert) {
// send a verification email
if ($this->sendVerificationEmail($user_id, $user_email, $user_activation_hash)) {
// when mail has been send successfully
$this->messages[] = $this->lang['Verification mail sent'];
$this->registration_successful = true;
} else {
// delete this users account immediately, as we could not send a verification email
$query_delete_user = $this->db_connection->prepare('DELETE FROM users WHERE user_id=:user_id');
$query_delete_user->bindValue(':user_id', $user_id, PDO::PARAM_INT);
$query_delete_user->execute();
$this->errors[] = $this->lang['Verification mail error'];
}
} else {
$this->errors[] = $this->lang['Registration failed'];
}
我尝试使用以下代码将该代码复制到我网站的另一部分:
public function addNewTag($postContent) {
if ($this->databaseConnection()) {
$query_add_tag = $this->db_connection->prepare('INSERT INTO tags (tag_name, tag_short_name, tag_id, color, user_id, creation_time)
VALUES(:tag_name, :tag_short_name, :tag_id, :color, :user_id, :creation_time)');
$query_add_tag->bindValue(':tag_name', $postContent['tag_name'], PDO::PARAM_STR);
$query_add_tag->bindValue(':tag_short_name', $postContent['tag_short_name'], PDO::PARAM_STR);
$query_add_tag->bindValue(':tag_id', rand(100000000000, 999999999999), PDO::PARAM_STR);
$query_add_tag->bindValue(':color', $postContent['color'], PDO::PARAM_STR);
$query_add_tag->bindValue(':user_id', $user_id, PDO::PARAM_STR);
$query_add_tag->bindValue(':creation_time', time(), PDO::PARAM_STR);
$query_add_tag->execute();
if ($query_add_tag) {
return true;
} else {
return false;
}
} else {
return false;
}
}
使用 $_POST 作为 $postContent 的变量调用函数
$content->addNewTag($_POST);
当我这样做时,该函数返回 true ("1"),但是当我检查我的数据库内容时,没有添加任何内容。这里可能是什么问题?我对 mySQL 的使用不是很好,所以这可能是我脚本中其他地方的明显错误。
【问题讨论】:
-
$query_add_tag始终是一个 PDOStatement 对象。它不会成为执行失败的虚假值,因此您无法测试if ($query_add_tag) -
看看PDO error handling manual。选择一个报错方式,默认是静默失败,可能就是你看到的那样。
-
如果你在函数开始时执行 $this->databaseConnection 的 var_dump 会产生什么输出?
-
我使用的登录脚本似乎对注册做同样的事情。他们做错了吗?或者我错过了什么......我会将他们的其余代码添加到我的原始帖子中。 (我是新来的。)
-
@AYM 是的,这是错误的代码。该变量始终包含 PDO 语句对象,该对象始终为真值。如果有任何原因导致原始代码失败(例如连接不正确的数据库或错误的权限),它将无法按预期运行并始终返回 true。