【问题标题】:Getting "SQLSTATE[HY093]: Invalid parameter number: parameter was not defined"获取“SQLSTATE [HY093]:无效参数号:未定义参数”
【发布时间】:2014-04-13 22:27:33
【问题描述】:

我已经在一个接一个的线程中搜索了这个错误,每个人的错误从概念上归结为参数丢失或拼写错误。我可能已经花了大约 4-5 个小时试图找到解决方案。我专门为这个问题做了一个帐户。

这是错误:

错误:SQLSTATE[HY093]:参数号无效:参数未定义

不管有什么问题,我的教授们也很困惑。无论如何,这里有一些我的代码的sn-ps:

注意:我将“location”设置为“todo-description”的内容,因为我还没有使用位置框格式化我的 HTML 页面。

解决方案:“到期日”中的连字符是非法字符。

    $data = array(
        'todo-name' => array('name' => 'Todo Item Name','value' => '', 'errors' =>array()),
        'todo-list' => array('name' => 'Todo List', 'value' => '', 'errors' => array()),
        'todo-due-date' => array('name' => 'Due Date', 'value' => '','errors' => array()),
        'todo-location' => array('name' => 'Location', 'value' => '','errors' => array()),
        'todo-priority' => array('name' => 'Priority', 'value' => '','errors' => array()),
        'todo-description' => array('name' => 'Description', 'value' => '','errors' => array()) 
        );

    if($total_errors == 0)
{
    $date = DATETIME::createFromFormat('m/d/Y', $data['todo-due-date']['value']);
    $date = $date->format('Y-m-d');
    $data['todo-due-date']['value'] = $date;

    $query = "insert into todo_item(name, due_date, list, priority, location, description)
        values(:name, :due-date, :list, :priority, :location, :description);";

    $statement = $db->prepare($query);
    try
    {
        $statement->execute(array(
                'name' => $data['todo-name']['value'],
                'due-date' => $data['todo-due-date']['value'],
                'list' => $data['todo-list']['value'],
                'priority' => $data['todo-priority']['value'],
                'location' => $data['todo-description']['value'],
                'description' => $data['todo-description']['value']
                ));

        if($statement)                  
        {
            $position = $db->lastInsertId();
            header("Location: item.php?id=$position");
        }
    }
    catch(Exception $ex)
    {
        die("Could not execute query: {$ex->getMessage()}");
    }
}
else
{
    die('error');
}

【问题讨论】:

  • 我想它仍然可以工作,但我建议坚持正确的类名大小写 - 这是对细节的关注的一部分,有助于消除一般问题。因此,我会将DATETIME 更改为DateTime
  • 参数名称中是否允许使用连字符?
  • 啊哈,好地方@popovits,几乎可以肯定是due-date。应该用下划线重命名。
  • :) 自己无法测试,所以不想回答。
  • OP,你的header() 后面也需要紧跟exit,否则你可能会在客户端有机会处理重定向请求之前执行 PHP。

标签: php mysql sql database


【解决方案1】:

根据PDO valid characters for placeholders(https://stackoverflow.com/a/5810058/689579)的回答

http://lxr.php.net/xref/PHP_5_3/ext/pdo/pdo_sql_parser.re#49

有效参数字符

BINDCHR = [:][a-zA-Z0-9_]+;

所以你的hypenated参数'due-date'是无效的

【讨论】:

  • 我知道另一双眼睛可以解决问题。谢谢。
【解决方案2】:

你忘记了什么。看看吧:

$statement->execute(array(
    ':name' => $data['todo-name']['value'],
    ':due-date' => $data['todo-due-date']['value'],
    ':list' => $data['todo-list']['value'],
    ':priority' => $data['todo-priority']['value'],
    ':location' => $data['todo-description']['value'],
    ':description' => $data['todo-description']['value']
));

明白了吗? ;)

【讨论】:

  • 我不知道这是否是问题所在 - 显然在执行语句中 the colon prefix is not necessary (不过我不是反对者)。
  • @Marcel 不要成为 SA,只是告诉他/她出了什么问题。
猜你喜欢
  • 2012-06-13
  • 2012-04-15
  • 2020-03-18
  • 2016-11-28
  • 2016-12-15
相关资源
最近更新 更多