MySQLi预处理语句变量数与参数数不匹配

时间:2019-10-08 03:05:10

标签: php mysqli

我正在使用MySQLi写我认为是相当基本的准备陈述。语句如下(其中$connectionMySQLi实例):

    $venue = $_POST['venue'];
    $description = $_POST['description'];

    if (isset($_POST['add'])) {
       $query = $connection->prepare("INSERT INTO events (date, time, showTime, venue, description) VALUES ('$date', '$time', '$showtime', '?', '?')");
       $query->bind_param('ss', $venue, $description);
    }
    elseif (isset($_POST['edit'])) {
       $query = $connection->prepare("UPDATE events SET date = '$date', time = '$time', showTime = '$showtime', venue = '?', description = '?' WHERE id ='$id'");
       $query->bind_param('ss', $venue, $description);
    }

    $query->execute();

此操作失败,log: Number of variables doesn't match the number of parameters in a prepared statement中的以下内容。

我不是数学天才,但是语句中有两个占位符,bind_param中有两个变量:)我也已经确认类型正确:如果我var_dump $venue$description,我得到strings。有人可以帮我看看我的错误在哪里吗?

谢谢!

1 个答案:

答案 0 :(得分:1)

在此不做过多说明,请不要在查询中将“直接值”与值持有者(?)混合使用。而是一次将它们全部绑定。请尝试以下操作:

myTime: Date = new Date();
myDate = new Date();

events: string[] = [];

addEvent(type: string, event: MatDatepickerInputEvent<Date>) {
  this.events.push(`${event.value}`);
}

constructor(private formBuilder: FormBuilder,
  private agentservice: AgentService,
  private service: AuthService,
  private snackBar: MatSnackBar,
  public dialogRef: MatDialogRef<ScheduleComponent>,
  @Inject(MAT_DIALOG_DATA) public data: DialogData) {
    this.form = this.formBuilder.group({
      myDate: new FormControl(),
      myTime: new FormControl(),
      agentId: new FormControl()
    });

    this.form.controls.myDate.patchValue(this.form.value.myDate);
    this.form.controls.myTime.patchValue(this.form.value.myTime);
    this.form.controls.agentId.patchValue(data.agentId);

  }

  onSubmit() {
     console.log(this.form.value);
  }