gpt4 book ai didi

php - 通过 PHP 的 "insert into"mySQL 查询似乎有效,但未添加任何内容

转载 作者:可可西里 更新时间:2023-11-01 07:09:46 24 4
gpt4 key购买 nike

编辑:我意识到我的问题是我试图插入 $user_id,它有一个带数字的字符串“到 user_id 列,它是 int 类型。我把字符串转换成一个整数。我还是不太明白虽然出了错误,但那是因为时间限制。我会在其他时间再回来。

我正在尝试在 http://www.php-login.net 提供的高级 php 登录系统之上建立一个网站。 .在脚本的注册类中,有一个将新用户添加到数据库的函数,该代码运行良好:

$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 不是很好,所以这可能是我脚本中其他地方的明显错误。

最佳答案

您是否在 autocommit=0 中运行?如果是这样,请设置 autocommit=1 或在您的函数末尾添加提交。应该是这样的:

$query_add_tag->commit();

关于php - 通过 PHP 的 "insert into"mySQL 查询似乎有效,但未添加任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19215483/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com