无法将$ _POST数组保存到数据库中

时间:2022-07-01 12:33:01

I have a form that has multiple inputs of the same name, resulting in an array. I want to pass this $_POST array to a function which will process and save the array into a database.

我有一个具有多个同名输入的表单,从而产生一个数组。我想将这个$ _POST数组传递给一个函数,该函数将处理并将数组保存到数据库中。

I've done this before with no problems without using a function, but now that I want to contain it all in a nice neat function call it won't do it and I'm not sure why?

我之前没有使用过函数就没有问题,但是现在我想把它包含在一个很好的整齐函数调用中,它不会这样做而且我不确定为什么?

The input/post variable in question is named option[] and is passed to the function as $_POST['option']. Here is the function:

有问题的输入/后置变量名为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\">","","");
}

This is the error I'm getting in my log:

这是我在日志中收到的错误:

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

1 个解决方案

#1


2  

Based on the comment thread above, there are several things you should try.

根据上面的评论主题,你应该尝试几件事。

First, always enable error reporting when debugging an application. To do this in your script, add:

首先,在调试应用程序时始终启用错误报告。要在脚本中执行此操作,请添加:

error_reporting(E_ALL);

to the top of your script.

到你的脚本的顶部。

Second, ensure that $options contains the data you expect. Somewhere in your code, add the following line:

其次,确保$ options包含您期望的数据。在代码中的某处,添加以下行:

var_dump($options);

This will show you the contents of $options. If it does not contain the values you expect, check your submission process.

这将显示$ options的内容。如果它不包含您期望的值,请检查您的提交过程。

Finally, if $options contains the expected data, check your table structure to ensure that your query matches and inserts the correct values.

最后,如果$ options包含预期数据,请检查表结构以确保查询匹配并插入正确的值。

EDIT: After you posted the MySQL error, I cross-checked the MySQL Reserved Words list. The word 'option' is on the list. As such, the query is failing because the word is not recognized as a column name. Try surrounding the column name with backticks:

编辑:发布MySQL错误后,我交叉检查MySQL保留字列表。 “选项”一词在列表中。因此,查询失败,因为该单词未被识别为列名。尝试用反引号围绕列名称:

$query2 = $db->prepare("INSERT INTO $table2(`variant`, `option`)... 

#1


2  

Based on the comment thread above, there are several things you should try.

根据上面的评论主题,你应该尝试几件事。

First, always enable error reporting when debugging an application. To do this in your script, add:

首先,在调试应用程序时始终启用错误报告。要在脚本中执行此操作,请添加:

error_reporting(E_ALL);

to the top of your script.

到你的脚本的顶部。

Second, ensure that $options contains the data you expect. Somewhere in your code, add the following line:

其次,确保$ options包含您期望的数据。在代码中的某处,添加以下行:

var_dump($options);

This will show you the contents of $options. If it does not contain the values you expect, check your submission process.

这将显示$ options的内容。如果它不包含您期望的值,请检查您的提交过程。

Finally, if $options contains the expected data, check your table structure to ensure that your query matches and inserts the correct values.

最后,如果$ options包含预期数据,请检查表结构以确保查询匹配并插入正确的值。

EDIT: After you posted the MySQL error, I cross-checked the MySQL Reserved Words list. The word 'option' is on the list. As such, the query is failing because the word is not recognized as a column name. Try surrounding the column name with backticks:

编辑:发布MySQL错误后,我交叉检查MySQL保留字列表。 “选项”一词在列表中。因此,查询失败,因为该单词未被识别为列名。尝试用反引号围绕列名称:

$query2 = $db->prepare("INSERT INTO $table2(`variant`, `option`)...