"参数号无效"在PDO中

时间:2018-04-27 19:07:15

标签: php mysql pdo

我在PDO中做了一个准备好的声明。我收到了下面写的错误,而且我不知道我的错误在哪里。

  

错误!:SQLSTATE [HY093]:参数号无效:绑定变量数与令牌数不匹配

代码:

$qA=array();
$lastName=$_POST['cours'];

if(trim($_POST["nomActivity"]) != ""){
  $nomActivity= filter_var($_POST['nomActivity'], FILTER_SANITIZE_STRING);
  $qA[] = "a.title = :title";
}
if(trim($_POST["nPlace"]) != ""){
  $nPlace= filter_var($_POST['nPlace'], FILTER_SANITIZE_NUMBER_INT);
  $qA[] = "a.nPlace = :nPlaceA";
} 
if(isset($_POST['date'])){
  $date = $_POST['date'];
  $qA[] = "a.date = :date";
}

$stmtA = $con->prepare("UPDATE activity a SET " . implode(", ", $qA) . " where a.title= :lastN");

if(trim($_POST["nomActivity"]) != ""){
  $stmtA->bindParam(":title", $nomActivity);
} 
if(trim($_POST["nPlace"]) != ""){
  $stmtA->bindParam(":nPlaceA", $nPlace);
} 
if(trim($_POST["date"]) != ""){
  $stmtA->bindParam(":date", $date);
}
if(trim($_POST["cours"]) != ""){
  $stmtA->bindParam(":lastN", $lastName);
} 

$stmtA->execute();

1 个答案:

答案 0 :(得分:1)

好像你错过了适当的检查

  if(trim($_POST["nomActivity"]) != ""){
    $nomActivity= filter_var($_POST['nomActivity'], FILTER_SANITIZE_STRING);
    $qA[] = "a.title = :title";
  }
  if(trim($_POST["nPlace"]) != ""){
    $nPlace= filter_var($_POST['nPlace'], FILTER_SANITIZE_NUMBER_INT);
    $qA[] = "a.nPlace = :nPlaceA";
  } 
  if(isset($_POST['date'])){
    $date = $_POST['date'];
    $qA[] = "a.date = :date";
  }

  // this one
  if(isset($_POST['cours'])){
    $lastName=$_POST['cours'];
  } else {
    $lastName = 'your_default_value';
  }

你总是:lastN要分配

  $stmtA->bindParam(":lastN", $lastName);