foreach ($data_acceptable as $key => $value)
{
if($key != 'submit' && $value != 'submit')
{
$fields_array[] = $key;
$values_array[] = $value;
$placeholder_type .= "s";
$placeholder_num .= "?,";
$complete_array_info .= '$data_acceptable'."['".$key."'], ";
}
}
// Builds strings from arrays
$fields = implode(", ", $fields_array);
// Adds ' ' around column names , example = 'user_name'
$values = "'".implode("', '", $values_array)."'";
// Removes last , example = ?,?,?
$placeholder_num = substr($placeholder_num, 0, -1);
$complete_array_info = substr($complete_array_info, 0, -2);
echo $complete_array_info."<br /><br />"; // outputs: $data_acceptable['email'], $data_acceptable['user_name'], $data_acceptable['password']
echo $placeholder_type."<br /><br />"; // outputs: sss
$query = "INSERT INTO users (".$fields.") VALUES (".$placeholder_num.")";
echo $query;
echo "<br />".$values."<br />";
$stmt = mysqli_prepare($dbc, $query);
mysqli_stmt_bind_param($stmt, $placeholder_type, $complete_array_info);
mysqli_stmt_execute($stmt);
$affected_rows = mysqli_stmt_affected_rows($stmt);
if($affected_rows == 1){
echo 'User Entered';
mysqli_stmt_close($stmt);
mysqli_close($dbc);
} else {
echo 'Error Occurred<br>';
echo mysqli_error();
mysqli_stmt_close($stmt);
mysqli_close($dbc);
}
我想要完成的是构建一个字符串($complete_array_info
)来存储mysqli_stmt_bind_param($stmt, $placeholder_type, $complete_array_info);
我得到的错误是我{s}}中存储的sss定义了3个字符串变量来传递$placeholder_type
(?,?,?)但是$placeholder_num
($ data_acceptable [&#39; email&#39;],$ data_acceptable [&#39; user_name&#39;],$ data_acceptable [&#39;密码&#39;])并未执行所需的正确参数。如果我将$complete_array_info
的内容粘贴到其中,那么一切正常。
我对php和MySQL比较新,所以也许还有一个完全不同的功能我不知道但是这一切的最终目标是拥有自己的个人功能我可以重用来创建一个准备好的语句,用于通过占位符将任意数量的列插入到我的数据库中。
非常感谢任何帮助。