QLSTATE [HY093]:参数号无效:参数未定义

时间:2019-01-20 02:24:04

标签: php pdo

我很难找到为什么我的代码无法正常工作。我试图在互联网上查找,但似乎找不到该错误。

这是我的职能

public function AddNews($newsDate,$title,$content){

        try{
            $stmt = $this->db->prepare("INSERT INTO news(newsDate,title,content) VALUES (:newsDate,:$title,:$content)");

            $stmt->bindParam(":newsDate", $newsDate);
            $stmt->bindParam(":title", $title);
            $stmt->bindParam(":content", $content);

            $stmt->execute();

            return $stmt;

        }catch(PDOException $ex){
            echo $ex->getMessage();
        }
    }

和表单操作

/*---------DEVELOPMENT-----------*/
require_once '/database/database.php';

/*---------ENVIRONMENT-----------*/
// require_once 'database/database.php';

if(isset($_POST['btn-news-submit'])){

$newsDate = trim($_POST['newsDate']);
$title = trim($_POST['bodyContent']);
$content = trim($_POST['newsContent']);

if($user->AddNews($newsDate,$title,$content)){
    header("Location: admin-index.php?successfully-uploaded");
}
}

最后是我的html表单

<div class="news">
            <form action = "upload-news"  method="POST" enctype="multipart/form-data">
                <div class="form-group">
                        <input type="hidden" name="newsDate" id="newsDate" value="<?php echo date('Y-m-d H:i:s'); ?>" readonly="readonly">
                        <label for="bodyContent"><b>Title</b></label>
                        <textarea class="form-control" rows="1" id="bodyContent" name="bodyContent"></textarea>
                        <br>
                        <label for="exampleFormControlFile1">Content of News</label>
                        <textarea class="form-control" rows="5" id="newsContent" name="newsContent"></textarea>
                        <br />
                    <br>
                    <div class="btn-news">
                        <button type="submit" name="btn-news-submit" class="btn btn-primary">Post</button>  
                    </div>
                </div>
            </form>
        </div>

有人可以指出这里的错误在哪里。它说

  

SQLSTATE [HY093]:参数号无效:参数未定义

但是我检查了几次,所有的bindParam都匹配了

1 个答案:

答案 0 :(得分:0)

不要在此处的绑定句柄中使用美元符号:

"INSERT INTO news(newsDate,title,content) VALUES (:newsDate,:$title,:$content)"
                                                             ^       ^

只需使用像这样的纯字符串:

"INSERT INTO news(newsDate,title,content) VALUES (:newsDate,:title,:content)"