Home > OS >  PHP PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number: number of bound variables do
PHP PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number: number of bound variables do

Time:09-23

I am trying to build my Restful API from my database, however, I made the Request class and it includes the UpdateRequest Function in the Req.php file:

<?php
    class Requests{

        // Connection
        private $conn;

        // Table
        private $db_table = "Requests";

        // Columns
        public $R_id;
        public $F_Name;
        public $L_NAme;
        public $Mobile_No;
        public $Email;
        public $Website;
        public $Services_Type;
        public $Services_List;
        public $Contact_time;
        public $Request_Describtion;
      

        // Db connection
        public function __construct($db){
            $this->conn = $db;
        }

        // GET ALL
        public function getRequests(){
            $sqlQuery = "SELECT * FROM " . $this->db_table . "";
            $stmt = $this->conn->prepare($sqlQuery);
            $stmt->execute();
            return $stmt;
        }

        // CREATE
        public function createRequest(){
            $sqlQuery = "INSERT INTO
                        ". $this->db_table ."
                    SET
                        F_Name = :F_Name, 
                        L_NAme = :L_NAme, 
                        Mobile_No = :Mobile_No, 
                        Email = :Email, 
                        Website = :Website,
                        Services_Type = :Services_Type, 
                        Services_List = :Services_List, 
                        Contact_time = :Contact_time, 
                        Request_Describtion = :Request_Describtion";
        
            $stmt = $this->conn->prepare($sqlQuery);
        
            // sanitize
            $this->F_Name=htmlspecialchars(strip_tags($this->F_Name));
            $this->L_NAme=htmlspecialchars(strip_tags($this->L_NAme));
            $this->Mobile_No=htmlspecialchars(strip_tags($this->Mobile_No));
            $this->Email=htmlspecialchars(strip_tags($this->Email));
            $this->Website=htmlspecialchars(strip_tags($this->Website));
            $this->Services_Type=htmlspecialchars(strip_tags($this->Services_Type));
            $this->Services_List=htmlspecialchars(strip_tags($this->Services_List));
            $this->Contact_time=htmlspecialchars(strip_tags($this->Contact_time));
            $this->Request_Describtion=htmlspecialchars(strip_tags($this->Request_Describtion));
        
            // bind data
            $stmt->bindParam(":F_Name", $this->F_Name);
            $stmt->bindParam(":L_NAme", $this->L_NAme);
            $stmt->bindParam(":Mobile_No", $this->Mobile_No);
            $stmt->bindParam(":Email", $this->Email);
            $stmt->bindParam(":Website", $this->Website);
            $stmt->bindParam(":Services_Type", $this->Services_Type);
            $stmt->bindParam(":Services_List", $this->Services_List);
            $stmt->bindParam(":Contact_time", $this->Contact_time);
            $stmt->bindParam(":Request_Describtion", $this->Request_Describtion);
        
            if($stmt->execute()){
               return true;
            }
            return false;
        }

        // READ single
        public function getSingleRequest(){
            $sqlQuery = "SELECT
                        *
                      FROM
                        ". $this->db_table ."
                    WHERE 
                       R_id = ?
                    LIMIT 0,1";

            $stmt = $this->conn->prepare($sqlQuery);

            $stmt->bindParam(1, $this->R_id);

            $stmt->execute();

            $dataRow = $stmt->fetch(PDO::FETCH_ASSOC);
            
            $this->F_Name = $dataRow['F_Name'];
            $this->L_NAme = $dataRow['L_NAme'];
            $this->Mobile_No = $dataRow['Mobile_No'];
            $this->Email = $dataRow['Email'];
            $this->Website = $dataRow['Website'];
            $this->Services_Type = $dataRow['Services_Type'];
            $this->Services_List = $dataRow['Services_List'];
            $this->Contact_time = $dataRow['Contact_time'];
            $this->Request_Describtion = $dataRow['Request_Describtion'];
        }        

        // UPDATE
        public function updateRequest(){
            $sqlQuery = "UPDATE
                        ". $this->db_table ."
                    SET
                        F_Name = :F_Name, 
                        L_NAme = :L_NAme, 
                        Mobile_No = :Mobile_No, 
                        Email = :Email, 
                        Website = :Website,
                        Services_Type = :Services_Type, 
                        Services_List = :Services_List, 
                        Contact_time = :Contact_time, 
                        Request_Describtion = :Request_Describtion
                    WHERE 
                        R_id = :R_id";
        
            $stmt = $this->conn->prepare($sqlQuery);
        
            $this->F_Name=htmlspecialchars(strip_tags($this->F_Name));
            $this->L_NAme=htmlspecialchars(strip_tags($this->L_NAme));
            $this->Mobile_No=htmlspecialchars(strip_tags($this->Mobile_No));
            $this->Email=htmlspecialchars(strip_tags($this->Email));
            $this->Website=htmlspecialchars(strip_tags($this->Website));
            $this->Services_Type=htmlspecialchars(strip_tags($this->Services_Type));
            $this->Services_List=htmlspecialchars(strip_tags($this->Services_List));
            $this->Contact_time=htmlspecialchars(strip_tags($this->Contact_time));
            $this->Request_Describtion=htmlspecialchars(strip_tags($this->Request_Describtion));
            
            
        
            // bind data
            $stmt->bindParam(":F_Name", $this->F_Name);
            $stmt->bindParam(":L_NAme", $this->L_NAme);
            $stmt->bindParam(":Mobile_No", $this->Mobile_No);
            $stmt->bindParam(":Email", $this->Email);
            $stmt->bindParam(":Website", $this->Website);
            $stmt->bindParam(":Services_Type", $this->Services_Type);
            $stmt->bindParam(":Services_List", $this->Services_List);
            $stmt->bindParam(":Contact_time", $this->Contact_time);
            $stmt->bindParam(":Request_Describtion", $this->Request_Describtion);
        
        
        
            if($stmt->execute()){
               return true;
            }
            return false;
        }

        // DELETE
        function deleteRequest(){
            $sqlQuery = "DELETE FROM " . $this->db_table . " WHERE R_id = ?";
            $stmt = $this->conn->prepare($sqlQuery);
        
            $this->R_id=htmlspecialchars(strip_tags($this->R_id));
        
            $stmt->bindParam(1, $this->R_id);
        
            if($stmt->execute()){
                return true;
            }
            return false;
        }

    }
?>

Then, I was created the update.php file to make the post request to update which calls the updateRequest() function from the previous file, this is it:

<?php
    header("Access-Control-Allow-Origin: *");
    header("Content-Type: application/json; charset=UTF-8");
    header("Access-Control-Allow-Methods: POST");
    header("Access-Control-Max-Age: 3600");
    header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
    
    include_once '/home/intlogy/api.intlogy.com/Try/TryConnectionConfig.php';
    include_once '/home/intlogy/api.intlogy.com/Try/Req.php';
    
    $database = new Database();
    $db = $database->getConnection();
    
    $item = new Requests($db);
    
    $data = json_decode(file_get_contents("php://input"));
    
    $item->R_id = $data->R_id;
    
    // Requests values
    $item->F_Name = $data->F_Name;
    $item->L_NAme = $data->L_NAme;
    $item->Mobile_No = $data->Mobile_No;
    $item->Email = $data->Email;
    $item->Website = $data->Website;
    $item->Services_Type = $data->Services_Type;
    $item->Services_List = $data->Services_List;
    $item->Contact_time = $data->Contact_time;
    $item->Request_Describtion = $data->Request_Describtion;

    
    
    if($item->updateRequest()){
        echo json_encode("Request data updated.");
    } else{
        echo json_encode("Request's data could not be updated");
    }
?>

How ever, when I tested it on the postman, the request sent properly, but the out put was:

"Request's data could not be updated"

and when I went to the error log, it shows that:

[Thu Sep 23 07:05:41.445698 2021] [:error] [pid 22651:tid 139905204188928] [client 51.223.108.141:33630] PHP Warning: PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens in /home/intlogy/api.intlogy.com/Try/Req.php on line 154

I tried to fix the error in the Req.php file put I didn't get it, is there anyone who can help?

CodePudding user response:

In updateRequest(), you need to bind the :R_id parameter:

            $stmt->bindParam(":R_id", $this->R_id);
  • Related