duangou1551 2014-05-18 04:12
浏览 51
已采纳

需要帮助查找PHP错误原因:参数号无效:参数未定义

I have not been able to find the cause of this error. I understand that it usually is caused by the number of params and values not matching during the execute() function. However, I have used var_dump and echo to verify repeatedly that my params and values match in number at every stage of this process. Can someone please show me where my code is wrong? Thanks!

First, here's my initial code:

$insert = array(
    array(  
      'fName' => 'Bob',   
      'mName' => 'C',  
      'lName' => 'Smith',  
      'suffix' => 'Jr'   
    ),  
    array(  
      'fName' => 'Tim',  
      'mName' => 'K',  
      'lName' => 'Jones',  
      'suffix' => 'Sr'  
    ),  
    array(  
      'fName' => 'Jim',  
      'mName' => 'P',  
      'lName' => 'Hampton',  
      'suffix' => 'III'  
    )  
);


$db = new Connect('clients');  
$db->insertMultiple($insert); 

Then, here is my relevent class functions:

public function insertMultiple($array)
{   
        foreach($array as $inner)
        {
            $fields = '(';
            $values = '(';

            foreach ($inner as $key => $value)
            {

                $fields .= $key . ',';
                $values .= ':' . $value . ',';
            }

            if (substr($fields, -1, 1) == ',')
            {
                $fields = substr($fields, 0, -1);
            }

            if (substr($values, -1, 1) == ',')
            {
                $values = substr($values, 0, -1);
            }

            $fields .= ')';
            $values .= ')';

            $sql = "INSERT INTO $this->name $fields VALUES $values";

            $this->query($sql);

            $this->bindValues($inner);

            try
            {

                $this->execute();
            }
            catch(PDOException $e)
            {
                $date = new DateTime(); 

                file_put_contents($this->file, trim($this->error = $e->getMessage()). ' ' . $date->format('Y-m-d H:i:s') . PHP_EOL,FILE_APPEND);
            }                   
        }
}

And the functions that this one calls are:

public function bindValues($array)
{   
    foreach($array as $param => $value)
    {
        $param = ':' . $param;

        $this->bind($param,$value); 
    }

}

and

public function bind($param, $value, $type = null)
{
    if (is_null($type)) 
    {
        switch (true) 
        {
            case is_int($value):
                $type = PDO::PARAM_INT;
                break;
            case is_bool($value):
                $type = PDO::PARAM_BOOL;
                break;
            case is_null($value):
                $type = PDO::PARAM_NULL;
                break;
            default:
                $type = PDO::PARAM_STR;
        }
    }
    $this->stmt->bindValue($param, $value, $type);
}

My query method:

public function query($query)
{
    $this->stmt = $this->dbh->prepare($query);
}

Someone please point out where I am going wrong! Thanks!

展开全部

  • 写回答

1条回答 默认 最新

  • dongwei1263 2014-05-18 05:21
    关注

    Looks like you need to change this:

        foreach ($inner as $key => $value)
            {
    
                $fields .= $key . ',';
                $values .= ':' . $value . ',';
            }
    

    To this:

        foreach ($inner as $key => $value)
            {
    
                $fields .= $key . ',';
                $values .= ':' . $key . ',';
            }
    

    The value is actually a place holder which is often identical to the key prefixed with :. If I read your code correctly, you are prefixing the actual value. Therefore your bind is not working as it is correctly looking for :key but you had the placeholder incorrectly defined as :value.

    It would explain the error as it cannot find the parameters you are looking for.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部