Invalid parameter number on PDO Prepared Statement

前端 未结 3 593
情话喂你
情话喂你 2021-01-14 10:08

I\'m working with a sequence of queries created with PDO class, in some case, my queries needs the same parameter. I\'ve created an array used in a foreach statement which s

3条回答
  •  深忆病人
    2021-01-14 10:36

    To elaborate on BD answer you're missing the following lines of code:

    $statement->bindParam (':address', trim($address), PDO::PARAM_STR);
    $statement->bindParam (':phone', trim($phone), PDO::PARAM_STR);
    $statement->bindParam (':email', trim($email), PDO::PARAM_STR);
    

    Plus, something seems to be wrong with your foreach loop, I think this is what you want:

    $sql = "UPDATE users_table SET city = :address, phone = :phone, email = :email, admin_id = :admin_id, admin_name = :admin_name";
    $statement = $connection->prepare($sql);
    
    $statement->bindParam(':admin_id', trim($admin_id), PDO::PARAM_INT);
    $statement->bindParam(':admin_name', trim($admin_name), PDO::PARAM_STR);
    
    foreach ($full_data as $value)
    {
        $statement->bindParam(':address', trim($value['address']), PDO::PARAM_STR);
        $statement->bindParam(':phone', trim($value['phone']), PDO::PARAM_STR);
        $statement->bindParam(':email', trim($value['email']), PDO::PARAM_STR);
    
        $ok = $statement->execute();
        $num = $statement->rowCount();
    }
    

提交回复
热议问题