1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[SQL] Uncaught PDOException: SQLSTATE[HY093]: Invalid parameter number: number of bound...

Discussão em 'Outras Linguagens' iniciado por Stack, Novembro 5, 2024 às 14:52.

  1. Stack

    Stack Membro Participativo

    I'm having difficulties in my Category, CategoryModel class regarding the error reported above, I looked and searched the code several times and didn't see the error, here's my Category which looks like this:

    Categories.php = Controller

    Here is the code for this:

    <?php

    /**
    * Data and Communication Control with Category model
    * It is conventional having the same name 'like' a table, example "Categories"
    * in plural, with entity being in singular as "Category"
    *
    * @author Alessandro Fraga Gomes
    * @copyright 2021-2024 Php7 Alex
    * @version 1.1.1
    */


    class Categories extends Controller
    {

    public function __construct()
    {
    $this->postModel = $this->model('Post');
    $this->userModel = $this->model('User');
    $this->categoryModel = $this->model('Category');
    }

    // public function index($id)
    public function index($url_site)
    {
    // $post = $this->categoryModel->readPostsByCategory($id);
    $post = $this->categoryModel->readPostsByCategory($url_site);

    if ($post == null) {
    Session::msg('category', 'No post to show in this category');
    }

    $admin = $this->userModel->readAdmin();
    $categories = $this->categoryModel->readCategories();
    $category = $this->categoryModel->readCategoryByUrl($url_site);

    $data = [
    'posts' => $post,
    'categories' => $categories,
    'category' => $category,
    'admin' => $admin
    ];

    $this->view('categories/index', $data);
    }
    }



    I believe the problem is not in the class above, just mentioning it just to have a clue, there seems to be a problem with this line return $this->db->result()->id; in the categoryId Method.... but I don't know what it is, if there is another error in the code I am not seeing it

    Category.php = Category Model

    <?php

    /*
    * Data and Communication Control with category model
    * Responsible for database communication
    * It is conventional having the same name 'like' an entity (SQL language)
    *
    * @author Alessandro Fraga Gomes
    * @copyright 2021-2024 Php7 Alex
    * @version 1.1.1
    */

    class Category
    {
    private $db;
    private $table = 'categories';

    public function __construct()
    {
    // db connection's class instance
    $this->db = new Db();
    }

    public function readCategories()
    {
    $this->db->query("SELECT * FROM {$this->table} ORDER BY title ASC");
    return $this->db->results();
    }

    // public function readPostsByCategory($id)
    public function readPostsByCategory($url_site)
    {

    $cat = $this->categoryId($url_site);

    $this->db->query("SELECT
    p.id as postId,
    p.url_site as postUrl,
    p.cover as postCover,
    p.title as postTitle,
    p.txt as postTxt,
    p.created_at as postRegisterDate,
    c.title as categTitle,
    c.descr as categDescr,
    u.username as postAuthor

    FROM posts AS p
    INNER JOIN categories AS c
    ON p.category_id = c.id
    INNER JOIN users AS u
    ON p.user_id = u.id
    WHERE p.category_id = $cat
    ORDER BY p.id DESC
    ");

    // $this->db->bind('id', $id);
    $this->db->bind('url_site', $url_site);

    return $this->db->results();
    }


    private function categoryId($url_site) {

    var_dump($url_site);
    $this->db->query("SELECT id FROM {$this->table} WHERE url_site = :url_site");
    $this->db->bind('url_site', $url_site);

    var_dump($this->db->result()->id);
    return $this->db->result()->id;
    }


    // saving category at db
    public function save($data)
    {

    $data['url_site'] = $this->titleCheck($data['title']);

    $this->db->query("INSERT INTO {$this->table} (url_site, title, descr) VALUES :)url_site, :title, :descr)");

    $this->db->bind("url_site", $data['url_site']);
    $this->db->bind("title", $data['title']);
    $this->db->bind("descr", $data['descr']);

    if ($this->db->exec()) :
    return true;
    else :
    return false;
    endif;
    }

    public function update($data)
    {

    $data['url_site'] = $this->titleCheck($data['title'], $data['id']);

    $this->db->query("UPDATE {$this->table} SET url_site = :url_site, title = :title, descr = :descr, updated_at = NOW() WHERE id = :id");

    $this->db->bind("id", $data['id']);
    $this->db->bind("url_site", $data['url_site']);
    $this->db->bind("title", $data['title']);
    $this->db->bind("descr", $data['descr']);


    if ($this->db->exec()) :
    return true;
    else :
    return false;
    endif;
    }

    public function readCategoryById($id)
    {
    $this->db->query("SELECT * FROM {$this->table} WHERE id = :id");
    $this->db->bind('id', $id);

    return $this->db->result();
    }

    public function readCategoryByUrl($url_site)
    {

    $this->visitCount($url_site);

    $this->db->query("SELECT * FROM {$this->table} WHERE url_site = :url_site");
    $this->db->bind('url_site', $url_site);

    return $this->db->result();
    }

    public function delete($id)
    {
    // var_dump($id);

    $this->db->query("DELETE FROM {$this->table} WHERE id = :id");

    $this->db->bind("id", $id);

    if ($this->db->exec()) :
    return true;
    else :
    return false;
    endif;
    }


    // does category has posts
    public function postsCheck($id)
    {
    $this->db->query("SELECT * FROM posts WHERE category_id = :id");
    $this->db->bind("id", $id);

    if ($this->db->result()) :
    return true;
    else :
    return false;
    endif;
    }


    public function titleCheck($title, $id = null){

    $sql = (!empty($id) ? "id != {$id} AND" : "");

    $this->db->query("SELECT * FROM {$this->table} WHERE {$sql} title = :t");
    $this->db->bind('t', $title);

    if($this->db->result()) :
    // return Url::friendlyUrl($title).'-'.uniqid();
    return Url::friendlyUrl($title).'-'.date('d-m-Y-h_i-s', time());
    else:
    return Url::friendlyUrl($title);
    endif;
    }

    private function visitCount($url_site)
    {
    $this->db->query("UPDATE {$this->table} SET visits = visits + 1, last_visit = NOW() WHERE url_site = :u");

    $this->db->bind("u", $url_site);

    if ($this->db->exec()) :
    return true;
    else :
    return false;
    endif;
    }

    }


    The views are like this in code for the main page:

    <div class="sideBar">
    <h4>Categories</h4>
    <ul>
    <?php foreach ($data['categories'] as $category) : ?>
    <li>
    <a href="<?= URL . '/categories/' . $category->url_site ?>" data-toggle="tooltip" title=" <?= $category->title ?>">
    <?= $category->title ?>
    </a>
    </li>
    <?php endforeach ?>
    </ul>
    </div>



    SideBar in the page, and selecting category and browsing where it goes at the end of the page localhost/C1_B/categories/games

    And for the adminpage:

    <li class="list-inline-item">
    <!-- This '/controller/' may be the exact same name/string to work -->
    <a class="font-weight-bold" href="<?= URL . '/categories/' . $category->id ?>" data-toggle="tooltip" title="Show Category <?= $category->title ?>">
    <i class="btn btn-outline-success border-0 fas fa-eye"></i>
    </a>
    </li>


    Admin list for Categories and selecting category and browsing where it goes at the end of the page localhost/C1_B/categories/2

    The errors are bellow

    [Uncaught PDOException: SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens in] (https://i.sstatic.net/UApd0GED.png)

    Uncaught PDOException: SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens in and Attempt to read "id" on bool

    Sorry to bother with a very long question for a starter, but I need this help, I tried everything I know already. Thank you all.

    I tried a lot of changings in the id property, converting to string, but nothing I tried worked, even looking if the code was really correct, I do know where the error is (maybe), but I am not being able to fix it for a long time. Tested the readPostsByCategory SQL at phpMyAdmin and it worked well.... so.... I really don't know where exactly the error is, and how to fix it anymore.

    Continue reading...

Compartilhe esta Página