首先,如果我的标题不能很好地解释这种情况,我很抱歉认为这是一个好问题。
我试图从窗体中取出所有输入并将它们放入数据库中。如果这是可能的话,我现在并不是100%肯定的,但我想我会发布它以确保。代码评论,但我得到使用准备好的语句时的预定义语句
INSERT INTO items (title, description, category, size) VALUES (?, ?, ?, ?)
'ssss', $fields[0][1], $fields[1][1], $fields[2][1], $fields[3][1]
Warning: Wrong parameter count for mysqli_stmt::bind_param()
我猜这是因为我通过变量,而不是它的价值。但即使我确实通过了它的价值,不管它是否好代码对我来说都是值得怀疑的。
// $fields[x][0] is title of x, $fields[x][1] is value of x
$statement = 'INSERT INTO items (';
// Add all the field names
for ($i = 0; $i < count($fields) - 1; $i++) {
$statement .= $fields[$i][0] . ', ';
}
$statement = $statement . $fields[count($fields) - 1][0] . ') VALUES (';
// Add '?' for every field
for ($i = 0; $i < count($fields) - 1; $i++) {
$statement .= '?, ';
}
$statement = $statement . '?)';
echo $statement . '<br />';
// Add all the value types to the statement
$params = '\'';
for ($i = 0; $i < count($fields); $i++) {
if (gettype($fields[$i][1]) == 'integer') {
$params .= 'i';
} else {
$params .= 's';
}
}
$params .= '\', ';
// Put the values into the statement
for ($i = 0; $i < count($fields) - 1; $i++) {
$params .= '$fields[' . $i . '][1], ';
}
$i = count($fields) - 1;
$params .= '$fields[' . $i . '][1]';
echo $params;
// Put it all in the database
$db = mysqlConnect();
$stmt = $db->stmt_init();
$stmt->prepare($statement);
$stmt->bind_param($params);
$stmt->close();
$db->close();
我认为这将是我会得到的回应,所以我必须坚持定期查询。我确实知道将它作为参数传递,我只是认为可能有一种方法可以用它的值替换'$ params'(所以它正常工作,ish)。 – 2012-02-19 21:21:26