【问题标题】:PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number: parameter was not defined on line 60PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number: parameter was not defined on line 60
【发布时间】:2018-04-22 05:55:44
【问题描述】:

我正在努力找出我的代码中的问题,我在这个平台上阅读了一些类似的问题,但没有一个帮助我解决问题。我已经指出错误消息中列出了哪一行(警告: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;
        }
    ?> 

【问题讨论】:

  • :condition 替换为:prodCondition
  • 哇!!太感谢了!有效。我很困惑,我认为 VALUES 应该是保存表单数据的变量,没有 $。

标签: php mysql sql-insert bindvalue


【解决方案1】:

检查您的列绑定占位符,列“prodCondition”具有绑定参数占位符“:condition”,但在您的 bindValue 函数中,您将其绑定到占位符“:prodCondition”,这是正确的列名,但不存在占位符。为了便于阅读,最好保持列名和占位符一致。

所以查询位应该是:

        //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)';

【讨论】:

    猜你喜欢
    • 2023-03-18
    • 2015-07-07
    • 1970-01-01
    • 1970-01-01
    • 2013-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-08
    相关资源
    最近更新 更多