草庐IT

php - SQLSTATE[HY093] : pdo statement during insert into mysql db

coder 2023-10-23 原文

这是我实现插入数据库的php代码:

<?php

require_once "includes/db_data_inc.php";

try 
{
    $DBH = new PDO("mysql:host=$db_host;dbname=$db_name",$db_user,$db_pass);

    $DBH->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    $cathy = new patient($_POST['name'],
                         $_POST['surname'],
                         $_POST['address'],
                         $_POST['birth-place'],
                         $_POST['province'],
                         $_POST['dt'],
                         $_POST['gender'],
                         $_POST['select']);

    $STH = $DBH->prepare("INSERT INTO users (name, 
                                            surname, 
                                            address, 
                                            birth_place,
                                            province,
                                            dt,
                                            sex,
                                            case) value (:name
                                                        :surname,
                                                        :address,
                                                        :birth_place,
                                                        :province,
                                                        :dt,
                                                        :sex,
                                                        :case)");

    $STH->execute((array)$cathy);

}
catch (PDOException $pdoe) 
{
    error_log($pdoe->getMessage());
    die("An error was encountered!");
}

?>

这是 db_data_inc.php 存储 db_info 和我创建对象 patient 的地方

    $db_host = 'localhost';

$db_name = 'main_db';

$db_user = 'root';

$db_pass = 'root';

/* Create an object patient */

class patient
{
    public $name;
    public $surname;
    public $address;
    public $birth_place;
    public $province;
    public $birth_date;
    public $sex;
    public $case;

    function __construct($nm,$sur,$addr,$bp,$pr,$bd,$sx,$cs)
    {
        $this->name = $nm;
        $this->surname = $sur;
        $this->address = $addr;
        $this->birth_place = $bp;
        $this->province = $pr;
        $this->birth_date = $bd;
        $this->sex = $sx;
        $this->case = $cs;
    }

}

我收到这个错误:

[10-Feb-2012 21:14:29] SQLSTATE[HY093]: Invalid parameter number: parameter was not defined

但我没有意识到原因...为什么会出现此错误?有人可以帮助我吗?哪里错了?

最佳答案

在您的查询中,您使用 :dt 作为占位符,但在类构造函数中,您使用 $this->birth_date

转换后,这将创建一个索引为“birth_date” 的数组,该数组与命名参数 “dt” 不匹配:选择一个或另一个。

关于php - SQLSTATE[HY093] : pdo statement during insert into mysql db,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9234425/

有关php - SQLSTATE[HY093] : pdo statement during insert into mysql db的更多相关文章

  1. ruby-on-rails - Railstutorial : db:populate vs. 工厂女孩 - 2

    在railstutorial中,作者为什么选择使用这个(代码list10.25):http://ruby.railstutorial.org/chapters/updating-showing-and-deleting-usersnamespace:dbdodesc"Filldatabasewithsampledata"task:populate=>:environmentdoRake::Task['db:reset'].invokeUser.create!(:name=>"ExampleUser",:email=>"example@railstutorial.org",:passwo

  2. ruby-on-rails - 撤消 "rails generate scaffold"后是否需要撤消 "db:migrate"? - 2

    我是RoR的新手,我正在学习MichaelHartl的教程(所以请随意更正我在您认为合适的地方使用的术语)。在第2章中,我通过运行以下行创建了一个Users表:$railsgeneratescaffoldUsername:stringemail:string$bundleexecrakedb:migrate然后,我运行了下面的代码来尝试创建一个Microposts表(但是,我拼错了没有“r”的Micropost!)...$railsgeneratescaffoldMiropostcontent:stringuser_id:integer$bundleexecrakedb:migrate

  3. ruby-on-rails - 这个 C 和 PHP 程序员如何学习 Ruby 和 Rails? - 2

    按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visitthehelpcenter指导。关闭9年前。我来自C、php和bash背景,很容易学习,因为它们都有相同的C结构,我可以将其与我已经知道的联系起来。然后2年前我学了Python并且学得很好,Python对我来说比Ruby更容易学。然后从去年开始,我一直在尝试学习Ruby,然后是Rails,我承认,直到现在我还是学不会,讽刺的是那些打着简单易学的烙印,但是对于我这样一个老练的程序员来说,我只是无法将它

  4. ruby-on-rails - 模型范围正在破坏 rake db :migrate - rails 3. 2.3 postgres 9.1.3 - 2

    我在新的Rails应用程序(3.2.3)中运行迁移时遇到了问题。我们正在使用postrgres9.1.3和-pg(0.13.2)-当我运行rakedb:create,然后运行​​rakedb:migrate,我得到->1.9.3-p194(master)rakedb:migrate--trace**Invokedb:migrate(first_time)**Invokeenvironment(first_time)**Executeenvironmentrakeaborted!PG::Error:ERROR:relation"roles"doesnotexistLINE4:WHEREa

  5. ruby-on-rails - 奇数 rake db :migrate output - 2

    为什么rakedb:migrate运行Executedb:schema:dump我的输出全都搞砸了(显示SQL)。看起来像这样:ActiveRecord::SchemaMigrationLoad(0.5ms)SELECT"schema_migrations".*FROM"schema_migrations"(3.7ms)SELECTt2.oid::regclass::textASto_table,a1.attnameAScolumn,a2.attnameASprimary_key,c.connameASname,c.confupdtypeASon_update,c.confdeltyp

  6. ruby-on-rails - Rails 还是 Sinatra? PHP程序员入门学习哪个好? - 2

    按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visitthehelpcenter指导。关闭10年前。我使用PHP的时间太长了,对它感到厌倦了。我也想学习一门新语言。我一直在使用Ruby并且喜欢它。我必须在Rails和Sinatra之间做出选择,那么您会推荐哪一个?Sinatra真的不能用来构建复杂的应用程序,它只能用于简单的应用程序吗?

  7. ruby - Rails 3 db :migrate 的未定义方法 `visitor' - 2

    我在Rails3中进行数据库迁移时遇到异常。undefinedmethod`visitor'for#编辑请查看解决方案here.在我的项目中没有出现字符串visitor所以我很困惑。这是完整的转储:$rakedb:migrate--trace**Invokedb:migrate(first_time)**Invokeenvironment(first_time)**Executeenvironment**Invokedb:load_config(first_time)**Invokerails_env(first_time)**Executerails_env**Executedb:l

  8. ruby-on-rails - rake 数据库 :migrate not working on my postgres db - 2

    好的,所以我有一个Rails应用程序,我试图在postgres数据库上运行迁移,我通常使用mysql,一切都很好,但是当我运行命令时,我得到了这个rakedb:migrate(in/Users/tamer/Sites/my_app)/Users/tamer/.rvm/gems/ruby-1.9.2-p290@my_app/gems/activerecord-3.0.3/lib/active_record/connection_adapters/postgresql_adapter.rb:487:[BUG]Segmentationfaultruby1.9.2p290(2011-07-09

  9. ruby-on-rails - 运行 rake db :seed multiple times without creating duplicate records? - 2

    这个问题在这里已经有了答案:appendingtorakedb:seedinrailsandrunningitwithoutduplicatingdata(9个回答)关闭9年前。我想更改种子文件中的一些代码,以便在我多次运行种子命令时它不会创建重复记录。有什么方法可以从我的种子文件中修改下面的代码,这样就可以了吗?除非我弄错了,否则find_or_create_by方法在这里似乎不起作用。data_file=Rails.root.join('db/data/data.csv')CSV.foreach(data_file)do|row|TownHealthRecord.create(ci

  10. ruby-on-rails - rake db :migrate doesn't work after ruby 2. 2.2 到 2.2.3 通过 RVM 升级 - 2

    我最近使用RVM从Ruby2.2.2升级到2.2.3。这搞砸了我的开发环境中的一些事情,但由于有用的错误消息,到目前为止我可以处理它。现在我想向我的数据库添加一些迁移,但遇到了这个错误:$rakedb:migrate/Users/howard/.rvm/gems/ruby-2.2.3/bin/ruby_executable_hooks:15:in`eval':/Users/howard/.rvm/rubies/ruby-2.2.3/bin/rake:4:syntaxerror,unexpectedtSTRING_BEG,expectingkeyword_door'{'or'('(Syn

随机推荐