我有一个具有多个同名输入的表单,从而生成一个数组。我想将此$_POST
数组传递给一个函数,该函数将处理该数组并将其保存到数据库中。
我之前在不使用函数的情况下没有任何问题,但现在我想将它全部包含在一个漂亮的整洁函数调用中,它不会这样做,我不知道为什么?
有问题的输入/后变量被命名option[]
并作为$_POST['option']
. 这是功能:
function newVariant($name, $option) {
global $db;
global $table_prefix;
$table = $table_prefix . "variants";
$query = $db->prepare("INSERT INTO $table(name) VALUES(:name)");
$query->bindParam(":name", $name);
if (!$query->execute()) {
die(showMessage("Error!","There has been a problem saving your variant. Please try again or contact technical support if the problem persists.",""));
}
$variant_id = $db->lastInsertId('id');
for($i = 0; $i < count($option); $i++) {
if($option[$i] != "") {
$table2 = $table_prefix . "variant_items";
$query2 = $db->prepare("INSERT INTO $table2(variant, option) VALUES(:variant, :option)");
$query2->bindParam(":variant", $variant_id);
$query2->bindParam(":option", $option[$i]);
if (!$query2->execute()) {
die(showMessage("Error!","There has been a problem saving your variant. Please try again or contact technical support if the problem persists.",""));
}
}
}
$redirect = renderLink("/beyond/?act=admin&sub=variants", "true");
showMessage("Saving variant...<META HTTP-EQUIV=\"Refresh\" Content=\"1; URL=$redirect\">","","");
}
这是我在日志中遇到的错误:
PHP Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'option) VALUES(?, ?)' at line 1' in /Users/adampcollings/Sites/Hot Mint Development/beyond/php/functions/product.php:131
Stack trace:
#0 /Users/adampcollings/Sites/Hot Mint Development/beyond/php/functions/product.php(131): PDO->prepare('INSERT INTO bb_...')
#1 /Users/adampcollings/Sites/Hot Mint Development/beyond/html/admin/new-variant.php(5): newVariant('Test', Array)
#2 /Users/adampcollings/Sites/Hot Mint Development/beyond/php/html.php(19): include('/Users/adampcol...')
#3 /Users/adampcollings/Sites/Hot Mint Development/beyond/html/admin.php(10): subElement('admin', 'new-variant')
#4 /Users/adampcollings/Sites/Hot Mint Development/beyond/php/html.php(8): include('/Users/adampcol...')
#5 /Users/adampcollings/Sites/Hot Mint Development/beyond/index.php(14): siteElement('a in /Users/adampcollings/Sites/Hot Mint Development/beyond/php/functions/product.php on line 131