简体   繁体   English

得到“ SQLSTATE [HY093]:无效的参数编号:参数未定义”

[英]Getting “SQLSTATE[HY093]: Invalid parameter number: parameter was not defined”

I have searched thread after thread with this error and everyone's error conceptually boils down to a parameter missing or misspelled. 我搜索了一个又一个错误的线程,每个人的错误从概念上可以归结为参数丢失或拼写错误。 I have probably spent collectively around 4-5 hours trying to find the solution. 我可能总共花费了大约4-5个小时来寻找解决方案。 I specifically made an account for this problem. 我专门为这个问题做了一个说明。

This is the error: 这是错误:

ERROR: SQLSTATE[HY093]: Invalid parameter number: parameter was not defined 错误:SQLSTATE [HY093]:无效的参数号:未定义参数

Whatever is wrong, my professors are stumped as well. 无论出什么问题,我的教授们也感到沮丧。 Anyway here are some snippets of my code: 无论如何,这是我的代码片段:

NOTE: I set "location" equal to "todo-description"'s content because I have not formatted my HTML page with a location box yet. 注意:我将“位置”设置为等于“待办事项说明”的内容,因为我尚未使用位置框格式化HTML页面。

SOLUTION: the hyphen in "due-date" is an illegal character. 解决方案:“到期日期”中的连字符是非法字符。

    $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');
}

According to the answer to PDO valid characters for placeholders ( https://stackoverflow.com/a/5810058/689579 ) 根据对PDO占位符有效字符的回答( https://stackoverflow.com/a/5810058/689579
and
http://lxr.php.net/xref/PHP_5_3/ext/pdo/pdo_sql_parser.re#49 http://lxr.php.net/xref/PHP_5_3/ext/pdo/pdo_sql_parser.re#49

Valid parameter chars 有效参数字符

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

So your hypenated parameter 'due-date' is invalid 因此,您输入的催交参数'due-date'无效

You 've forgotten something. 你忘记了什么。 Just have a look: 看看:

$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']
));

Got it? 得到它了? ;) ;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM