SQLSTATE[HY093]: 绑定变量的数量与中的标记数量不匹配
SQLSTATE[HY093]: number of bound variables does not match number of tokens in
我在这段代码中的目标是为用户创建应用后面的部分,我创建了一个隐藏按钮来获取确切的 ID post,我正在获取 ID 但是当我绑定并执行我收到此警告,我已经检查了其他几个与我类似的问题,但他们似乎并没有解决我的确切问题。我究竟要绑定什么才能获取具有特定 ID
的 post 的信息 这是函数:
public function Index(){
if(isset($_POST['submit'])){
$id = $_POST['applylater_id'];// I got this from a button the user is to click
// INSERT INTO MYSQL
$this->query('INSERT INTO apply_later(id, title, body, link, user_id, user_name) SELECT id, title, body, link, user_id, user_name FROM shares WHERE id = :id');
$this->bind(':title', 'title');
$this->bind(':body', 'body');
$this->bind(':link', 'link');
$this->bind(':user_id', 1);
$this->bind(':user_name', 'user_name');
$this->bind(':id', $id);
$this->execute();
//Verify
if($this->lastInsertId()){
//Redireect
Messages::setMsg('Successfully Added to your apply Later section', 'successMsg');
header( "refresh:2; url=http://localhost/phpsandbox/phptutorials/website2/shares/applylater" );
}
}
//
$this->query('SELECT * FROM shares ORDER by id desc ');
$rows = $this->resultSet();
return $rows;
}
//这是我在上面调用的绑定和执行函数
public function query($query){
$this->stmt = $this->dbh->prepare($query);
}
public function bind ($param, $value, $type =null) {
if(is_null($type)){
switch(true) {
case is_int($value):
$type = PDO::PARAM_INT;
break;
case is_bool($value):
$type = PDO::PARAM_BOOL;
break;
case is_null($value):
$type = PDO::PARAM_NULL;
break;
default:
$type = PDO::PARAM_STR;
}
}
$this->stmt->bindValue($param, $value, $type);
}
public function execute(){
return $this->stmt->execute();
}
现在这是我使用的主要形式,但它可以获取 ID 但不在屏幕上显示它
<?php foreach ($viewmodel as $item) : ?>
<div class="well">
<div class="text-center">
<img src="http://localhost/phpsandbox/phptutorials/website2/assets/image/job.png" class="img-circle" alt="Cinque Terre" width="50" height="50">
<h3> <?php echo $item['title']; ?></h3>
<small> Username: <?php echo $item['user_name']; ?></small>
<br />
<small><?php echo $item['create_date']; ?></small>
</div>
<hr />
<p><?php echo $item['body']; ?></p>
<br />
<div class="text-center">
<a href="<?php echo $item['link']; ?>" target="_blank" class="btn btn-info">Apply Now</a>
<br />
</div>
<hr />
<form method="post" action="<?php $_SERVER['PHP_SELF']; ?>" >
// 当你点击提交时,我想得到这个确切的 post 的 ID,并将数据发送到另一个 table,这样我就可以在不同的页面中显示这个确切的。
<input type="hidden" name="applylater_id" value="<?php echo $item['id']; ?>">
<input class="btn btn-primary" type="submit" name="submit" value="Apply Later">
</form>
这是我的tables
</div>[enter image description here][1]
只将值绑定到有占位符的预准备语句。
$this->query('INSERT INTO apply_later(id, title, body, link, user_id, user_name)
SELECT id, title, body, link, user_id, user_name FROM shares WHERE id = :id');
// only 1 placeholder-------------------------------------------^^^
删除这些行,因为它们未在准备好的语句中表示:
$this->bind(':title', 'title');
$this->bind(':body', 'body');
$this->bind(':link', 'link');
$this->bind(':user_id', 1);
$this->bind(':user_name', 'user_name');
只需绑定:id
:
$this->bind(':id', $_POST['applylater_id']);
我在这段代码中的目标是为用户创建应用后面的部分,我创建了一个隐藏按钮来获取确切的 ID post,我正在获取 ID 但是当我绑定并执行我收到此警告,我已经检查了其他几个与我类似的问题,但他们似乎并没有解决我的确切问题。我究竟要绑定什么才能获取具有特定 ID
的 post 的信息 这是函数:
public function Index(){
if(isset($_POST['submit'])){
$id = $_POST['applylater_id'];// I got this from a button the user is to click
// INSERT INTO MYSQL
$this->query('INSERT INTO apply_later(id, title, body, link, user_id, user_name) SELECT id, title, body, link, user_id, user_name FROM shares WHERE id = :id');
$this->bind(':title', 'title');
$this->bind(':body', 'body');
$this->bind(':link', 'link');
$this->bind(':user_id', 1);
$this->bind(':user_name', 'user_name');
$this->bind(':id', $id);
$this->execute();
//Verify
if($this->lastInsertId()){
//Redireect
Messages::setMsg('Successfully Added to your apply Later section', 'successMsg');
header( "refresh:2; url=http://localhost/phpsandbox/phptutorials/website2/shares/applylater" );
}
}
//
$this->query('SELECT * FROM shares ORDER by id desc ');
$rows = $this->resultSet();
return $rows;
}
//这是我在上面调用的绑定和执行函数
public function query($query){
$this->stmt = $this->dbh->prepare($query);
}
public function bind ($param, $value, $type =null) {
if(is_null($type)){
switch(true) {
case is_int($value):
$type = PDO::PARAM_INT;
break;
case is_bool($value):
$type = PDO::PARAM_BOOL;
break;
case is_null($value):
$type = PDO::PARAM_NULL;
break;
default:
$type = PDO::PARAM_STR;
}
}
$this->stmt->bindValue($param, $value, $type);
}
public function execute(){
return $this->stmt->execute();
}
现在这是我使用的主要形式,但它可以获取 ID 但不在屏幕上显示它
<?php foreach ($viewmodel as $item) : ?>
<div class="well">
<div class="text-center">
<img src="http://localhost/phpsandbox/phptutorials/website2/assets/image/job.png" class="img-circle" alt="Cinque Terre" width="50" height="50">
<h3> <?php echo $item['title']; ?></h3>
<small> Username: <?php echo $item['user_name']; ?></small>
<br />
<small><?php echo $item['create_date']; ?></small>
</div>
<hr />
<p><?php echo $item['body']; ?></p>
<br />
<div class="text-center">
<a href="<?php echo $item['link']; ?>" target="_blank" class="btn btn-info">Apply Now</a>
<br />
</div>
<hr />
<form method="post" action="<?php $_SERVER['PHP_SELF']; ?>" >
// 当你点击提交时,我想得到这个确切的 post 的 ID,并将数据发送到另一个 table,这样我就可以在不同的页面中显示这个确切的。
<input type="hidden" name="applylater_id" value="<?php echo $item['id']; ?>">
<input class="btn btn-primary" type="submit" name="submit" value="Apply Later">
</form>
这是我的tables
</div>[enter image description here][1]
只将值绑定到有占位符的预准备语句。
$this->query('INSERT INTO apply_later(id, title, body, link, user_id, user_name)
SELECT id, title, body, link, user_id, user_name FROM shares WHERE id = :id');
// only 1 placeholder-------------------------------------------^^^
删除这些行,因为它们未在准备好的语句中表示:
$this->bind(':title', 'title');
$this->bind(':body', 'body');
$this->bind(':link', 'link');
$this->bind(':user_id', 1);
$this->bind(':user_name', 'user_name');
只需绑定:id
:
$this->bind(':id', $_POST['applylater_id']);