获取" SQLSTATE [HY093]:参数号无效:参数未定义"

时间:2014-04-13 22:27:34

标签: php mysql sql database

我在线程之后搜索了这个错误并且每个人的错误在概念上都归结为参数丢失或拼写错误。我可能花了大约4-5个小时试图找到解决方案。我特意说明了这个问题。

这是错误:

  

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

无论出了什么问题,我的教授们也很难过。无论如何,这里是我的代码的一些片段:

注意:我将“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');
}

2 个答案:

答案 0 :(得分:2)

根据PDO valid characters for placeholdershttps://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_]+;

因此,您的超级参数'due-date'无效

答案 1 :(得分:-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']
));

知道了吗? ;)