字符串中的元素数量与绑定变量的数量不匹配


Number of elements in string doesn't match number of bind variables

// prepare and bind

,你好

当我试图通过准备好的语句执行我的查询时,我收到以下错误:

Number of elements in string doesn't match number of bind variables     

下面的代码:

$track = $con->prepare("INSERT INTO resources_record (name,email,stage,format,topic,max_cost,mentor,total_cost,duration)
              VALUES ('?', '?', '?', '?', '?', '?', '?', '?', '?')");
    $track->bind_param($fullName, $email, $stage, $format, $topic, $cost, $mentor, $price, $duration);
    // Execute
    $track->execute();
    $track->close();

这里有两个明显的错误:

  • 不要在预编译语句的占位符周围加上引号
  • bind_param()的第一个参数应该是标识后续参数的数据类型的字符串- PHP Docs

$track = $con->prepare("INSERT INTO resources_record (name,email,stage,format,topic,max_cost,mentor,total_cost,duration)
          VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)");
$track->bind_param('sssssssss', $fullName, $email, $stage, $format, $topic, $cost, $mentor, $price, $duration);

包含9个s的字符串,因为您有9个参数,并且都是字符串(s)值