草庐IT

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

coder 2023-10-06 原文

编辑:我意识到我的问题是我试图插入 $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/

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

  1. ruby-on-rails - rails : "missing partial" when calling 'render' in RSpec test - 2

    我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou

  2. ruby - 我需要将 Bundler 本身添加到 Gemfile 中吗? - 2

    当我使用Bundler时,是否需要在我的Gemfile中将其列为依赖项?毕竟,我的代码中有些地方需要它。例如,当我进行Bundler设置时:require"bundler/setup" 最佳答案 没有。您可以尝试,但首先您必须用鞋带将自己抬离地面。 关于ruby-我需要将Bundler本身添加到Gemfile中吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/4758609/

  3. ruby-on-rails - 由于 "wkhtmltopdf",PDFKIT 显然无法正常工作 - 2

    我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-

  4. ruby - ECONNRESET (Whois::ConnectionError) - 尝试在 Ruby 中查询 Whois 时出错 - 2

    我正在用Ruby编写一个简单的程序来检查域列表是否被占用。基本上它循环遍历列表,并使用以下函数进行检查。require'rubygems'require'whois'defcheck_domain(domain)c=Whois::Client.newc.query("google.com").available?end程序不断出错(即使我在google.com中进行硬编码),并打印以下消息。鉴于该程序非常简单,我已经没有什么想法了-有什么建议吗?/Library/Ruby/Gems/1.8/gems/whois-2.0.2/lib/whois/server/adapters/base.

  5. ruby - 如何将脚本文件的末尾读取为数据文件(Perl 或任何其他语言) - 2

    我正在寻找执行以下操作的正确语法(在Perl、Shell或Ruby中):#variabletoaccessthedatalinesappendedasafileEND_OF_SCRIPT_MARKERrawdatastartshereanditcontinues. 最佳答案 Perl用__DATA__做这个:#!/usr/bin/perlusestrict;usewarnings;while(){print;}__DATA__Texttoprintgoeshere 关于ruby-如何将脚

  6. ruby - 通过 rvm 升级 ruby​​gems 的问题 - 2

    尝试通过RVM将RubyGems升级到版本1.8.10并出现此错误:$rvmrubygemslatestRemovingoldRubygemsfiles...Installingrubygems-1.8.10forruby-1.9.2-p180...ERROR:Errorrunning'GEM_PATH="/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/ruby-1.9.2-p180@global:/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/rub

  7. ruby - 检查 "command"的输出应该包含 NilClass 的意外崩溃 - 2

    为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar

  8. ruby - 将 Bootstrap Less 添加到 Sinatra - 2

    我有一个ModularSinatra应用程序,我正在尝试将Bootstrap添加到应用程序中。get'/bootstrap/application.css'doless:"bootstrap/bootstrap"end我在views/bootstrap中有所有less文件,包括bootstrap.less。我收到这个错误:Less::ParseErrorat/bootstrap/application.css'reset.less'wasn'tfound.Bootstrap.less的第一行是://CSSReset@import"reset.less";我尝试了所有不同的路径格式,但它

  9. ruby - 通过 erb 模板输出 ruby​​ 数组 - 2

    我正在使用puppet为ruby​​程序提供一组常量。我需要提供一组主机名,我的程序将对其进行迭代。在我之前使用的bash脚本中,我只是将它作为一个puppet变量hosts=>"host1,host2"我将其提供给bash脚本作为HOSTS=显然这对ruby​​不太适用——我需要它的格式hosts=["host1","host2"]自从phosts和putsmy_array.inspect提供输出["host1","host2"]我希望使用其中之一。不幸的是,我终其一生都无法弄清楚如何让它发挥作用。我尝试了以下各项:我发现某处他们指出我需要在函数调用前放置“function_”……这

  10. ruby - 续集在添加关联时访问many_to_many连接表 - 2

    我正在使用Sequel构建一个愿望list系统。我有一个wishlists和itemstable和一个items_wishlists连接表(该名称是续集选择的名称)。items_wishlists表还有一个用于facebookid的额外列(因此我可以存储opengraph操作),这是一个NOTNULL列。我还有Wishlist和Item具有续集many_to_many关联的模型已建立。Wishlist类也有:selectmany_to_many关联的选项设置为select:[:items.*,:items_wishlists__facebook_action_id].有没有一种方法可以

随机推荐