PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number: parameter was not defined on line 60

2.2k views Asked by At

I am struggling to figure out what is the problem in my code, I had read some similar questions on this platform, but none helped me to solve the problem. I have indicated what line is listed in the error message (Warning: PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number: parameter was not defined in C:\xampp\htdocs\Website system\sale.php on line 60)

<?php
        //link files
        require ('config.php'); //database connection

        //get user input from the form
        if (isset($_POST['formSubmit'])) {
            //product details
            $brand = checkData($_POST['brand']);
            $model = checkData($_POST['model']);
            $serial = checkData($_POST['serialnumber']);
            $yearModel = checkData($_POST['yearmodel']);
            $productType = checkData($_POST['type']);
            $condition = checkData($_POST['condition']);
            //supplier details
            $supplierId = checkData($_POST['supplierid']);
            $supInvoice = checkData($_POST['supplierinvoice']);
            $supPrice = checkData($_POST['supplierprice']);
            //customer details
            $custId = checkData($_POST['customerid']);
            $custInvoice = ($_POST['custinvoice']);
            $custPrice = checkData($_POST['custprice']);
            $purchaseDate = checkData($_POST['purchasedate']);

            require_once ('config.php'); //database connection
            global $dbselect;
            //SQL - add data to database
            $qry1 = 'INSERT INTO product
                    (brand, model, serial, yearmodel, type, prodCondition, purchDate, supplierId, supInvoice, supPrice, custId, custInvoice, custPrice)
                    VALUES
                    (:brand, :model, :serial, :yearModel, :productType, :condition, :purchaseDate, :supplierId, :supInvoice, :supPrice, :custId, :custInvoice, :custPrice)';
            //execute query
            $statement = $dbselect->prepare($qry1);
            $statement->bindValue(':brand', $brand);
            $statement->bindValue(':model', $model);
            $statement->bindValue(':serial', $serial);
            $statement->bindValue(':yearModel', $yearModel);
            $statement->bindValue(':productType', $productType);
            $statement->bindValue(':prodCondition', $condition);
            $statement->bindValue(':purchaseDate', $purchaseDate);
            $statement->bindValue(':supplierId', $supplierId);
            $statement->bindValue(':supInvoice', $supInvoice);
            $statement->bindValue(':supPrice', $supPrice);
            $statement->bindValue(':custId', $custId);
            $statement->bindValue(':custInvoice', $custInvoice);
            $statement->bindValue(':custPrice', $custPrice);

            if ($statement->execute())  { //////<-PROBLEM ON THIS LINE//////
                echo 'New Sale Added Successfully!';
            } else {
                echo 'Not successfull.';
            }
            $statement->closeCursor();
        }

        //validate data 
        function checkData($data) {
            $data = trim($data);
            $data = stripslashes($data);
            $data = htmlspecialchars($data);
            return $data;
        }
    ?> 
1

There are 1 answers

0
jcbdrn On

Check your column binding placeholders, the column "prodCondition" has a binded parameter placeholder of ":condition" but in your bindValue function you are binding it to placeholder ":prodCondition" which is the correct column name but does not exist as a placeholder. Is best for readability sake to keep the column names and placeholders consistent.

So the query bit should read:

        //SQL - add data to database
        $qry1 = 'INSERT INTO product
                (brand, model, serial, yearmodel, type, prodCondition, purchDate, supplierId, supInvoice, supPrice, custId, custInvoice, custPrice)
                VALUES
                (:brand, :model, :serial, :yearModel, :productType, :prodCondition, :purchaseDate, :supplierId, :supInvoice, :supPrice, :custId, :custInvoice, :custPrice)';