我似乎收到的错误信息是类型定义字符串中的元素数量与绑定变量的数量不匹配。
<?php
function addToTable($data){
$columns = array_keys($data);
$keys = implode(", ", $columns);
$info = array_values($data);
$values = implode(", ", $info);
$preholder = [];
foreach($data as $count){
array_push($preholder,"?");
}
$placeholder = implode(", ", $preholder);
$sql = "INSERT INTO guest ($keys) VALUES ($placeholder)";
$types = [];
foreach($data as $x=>$y){
if(is_numeric($y)){
array_push($types,"i");
}
elseif(gettype($y) == "string"){
array_push($types,"s");
}
}
$types = implode("",$types);
$statement = mysqli_prepare($conn, $sql);
mysqli_stmt_bind_param($statement, '$types', $values);
$results = mysqli_stmt_execute($statement);
mysqli_close($conn);
return $results;
}
?>
我能想到的唯一可能的事情是在bind函数中实际上需要在$ type之后的多个值而不是具有值列表的变量。我希望保持这种工作方式,不必每次都手动更改它。