编辑:我意识到我的问题是我试图插入$ 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'];
}
Run Code Online (Sandbox Code Playgroud)
我尝试使用以下代码将该代码复制到我的网站的另一部分:
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;
}
}
Run Code Online (Sandbox Code Playgroud)
使用$ _POST作为$ postContent的变量调用该函数
$content->addNewTag($_POST);
Run Code Online (Sandbox Code Playgroud)
当我这样做时,该函数返回true("1"),但是当我检查我的数据库内容时,没有添加任何内容.这可能是什么问题?我的mySQL不是很好,所以也许这在我的脚本中的其他地方是一个明显的错误.
小智 0
你跑进来吗autocommit=0
?autocommit=1
如果是这样,请在函数末尾设置或添加提交。应该是这样的:
$query_add_tag->commit();
Run Code Online (Sandbox Code Playgroud)