如何解决此错误致命错误:未捕获异常' PDOException'消息' SQLSTATE [42000]:语法错误或访问冲突:

时间:2017-12-09 09:30:02

标签: php pdo

我使用OOP PHP创建购物车。当我通过HTML表单从数据库返回单个产品信息数据以执行编辑操作时, 我遇到这种类型的错误

"致命错误:未捕获的异常' PDOException' with message' SQLSTATE [42000]:语法错误或访问冲突:1064 SQL语法中有错误;查看与您的MariaDB服务器版本对应的手册,以获得在''附近使用的正确语法。在第1行'在C:\ xampp \ htdocs \ onlineShop \ src \ Product.php:100堆栈跟踪:#0 C:\ xampp \ htdocs \ onlineShop \ src \ Product.php(100):PDO->查询(' SELECT * FROM t ...')#1 C:\ xampp \ htdocs \ onlineShop \ views \ Product \ editProduct.php(18):App \ Product-> view()#2 {main}引入第100行" C:\ xampp \ htdocs \ onlineShop \ src \ Product.php;

我的editProduct文件代码

<?php
  require_once("../../vendor/autoload.php");
  use App\Product;

  $product  = new Product();
  $product->setData($_GET);
  $allData = $product->view();



?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Book Title Create Form</title>
</head>
<body>
<div class="container" align="center" id="body">
    <h3>Add Product Information</h3>
</div>
<div align="center" class="container">

    <form action="store.php" method="post" enctype="multipart/form-data" class="form-horizontal">
        <table class="table table-bordered table-responsive">
            <tr>
                <td><label class="control-label"> Product Name:</label></td>
                <td> <input type="text" class="form-control" name="productName" value=""<?php echo $allData->productName;?>""></td>
            </tr>

            <tr>
                <td><label class="control-label">Category :</label></td>
                <td><select name="categoryID" class="form-control">
                        <?php
                        $category = new App\Category();
                        $oneData= $category->index();
                        foreach($oneData as $singleData){

                            ?>
                            <option
                                <?php if($allData->categoryID==$singleData->categoryID){?>
                                      selected ="selected"
                           <?php } ?>
                                value="<?php echo $singleData->categoryID; ?>"><?php echo $singleData->categoryName;?></option>
                        <?php }?>
                    </select>
                </td>
            </tr>

            <tr>
                <td><label class="control-label">Brand :</label> </td>
                <td>
                    <select name="brandID" class="form-control">
                        <?php
                        $brand = new App\Brand();
                        $allData= $brand->index();
                        foreach($allData as $singleData){
                            ?>
                            <option
                                <?php if($allData->brandID==$singleData->brandID){?>
                                    selected ="selected"
                                <?php } ?> value="<?php echo $singleData->brandID;?>"><?php echo $singleData->brandName;?></option>
                        <?php } ?>
                    </select>
                </td>
            </tr>
            <tr>
                <td><label class="control-label">Description:</label></td>
                <td>
                    <textarea name="description" value="<?php echo $allData->description;?>" class="form-control" ></textarea>
                </td>
            </tr>
            <tr>
                <td><label class="control-label">Price:</label></td>
                <td>
                    <input type="text" value="<?php echo $allData->price;?>" id="price" name="price" class="form-control">
                </td>
            </tr>
            <tr>
                <td><label class="control-label">Upload Image :</label> </td>
                <td>
                    <input class="input-group" type="file"  value="<?php echo $allData->image;?>" id="image" name="image" >
                </td>
            </tr>
            <tr>
                <td><label class="control-label">Product Type :</label></td>
                <td>
                    <select class="form-control" name="productType">
                        <option value="0">Featured</option>
                        <option value="1">General</option>
                    </select>
                </td>

            </tr>


        </table>

</div>
<div class="container">
    <input type="submit" value="EDIT" class="btn btn-success">
</div>

</form>
</body>
</html>
de here

我的查看方法类似于用于查看单个产品信息以执行编辑操作的方法。

                public function view()
            {

                $sql = "SELECT * FROM tbl_product WHERE productID=".$this->productID;
                $STH = $this->DBH->query($sql);
                $STH->setFetchMode(PDO::FETCH_OBJ);
                return $STH->fetch();
            }

请帮我解决这类错误。

2 个答案:

答案 0 :(得分:0)

请确保$ this-&gt; productID不是空的

更新您的功能代码,如下所示,请参阅:

public function view()
{

    if(!empty($this->productID)){
        $sql = "SELECT * FROM tbl_product WHERE productID=".$this->productID;
        $STH = $this->DBH->query($sql);
        $STH->setFetchMode(PDO::FETCH_OBJ);
        return $STH->fetch();       
    }
        return [];
}

答案 1 :(得分:0)

如果productID是迭代类型

 $sql = "SELECT * FROM `tbl_product` WHERE `produt_id`= ".$this->productID;

如果productID是VARCHAR OR TEXT类型: -

$sql = 'SELECT * FROM `tbl_product` WHERE `product_id` ="'.$this->productID.'"';