如何将php多维数组数据存储到各自领域的数据库中

mlmc2os5  于 2021-06-19  发布在  Mysql
关注(0)|答案(3)|浏览(420)

这个问题在这里已经有答案了

在mysqli中插入许多值的最佳方法是什么(4个答案)
两年前关门了。
我有多数组数据,比如“cars name&cars modal”
车名与车型匹配。这两个列在数据库中是不同的(cars\u name,cars\u model)。我要将此数组中的数据存储到其字段中的数据库中
输出:

  1. Array
  2. (
  3. [car_name] => Array
  4. (
  5. [0] => Honda
  6. [1] => Ford Mustang
  7. [2] => Volvo
  8. )
  9. [car_modal] => Array
  10. (
  11. [0] => 2015
  12. [1] => 2016
  13. [2] => 2014
  14. )
  15. )

我想使用“mysql”将数组值存储到每行的单个列中。为此,我喜欢这样的查询,但它显示错误。

  1. $sql = "INSERT INTO cars_data (cars_name,cars_modal)
  2. VALUES ($cars_name,$cars_modal)";

什么都没发生。但错误是这样的。。。

注意:e:\xampp\htdocs\car\u records\modal\u data.php中的数组到字符串转换45行**错误:插入cars\u数据(cars\u name,cars\u model)

值(数组,数组)
“字段列表”中的未知列“array”
问题是如何解决它。请帮帮我

vltsax25

vltsax251#

您可以在一行中插入多个元素,只需以正确的格式插入即可:
插入x(列)值(x1)、(x2)、(x3)

  1. $string = "";
  2. foreach($cars_name as $car){
  3. $string .= "(" . $car . "), ";
  4. }
  5. $sql = "INSERT INTO car_data (cars_name) VALUES $string";

请注意,您不应该接受用户输入而不进行消毒。

tmb3ates

tmb3ates2#

要使用一个语句和mysqli准备的语句(代码中的注解)。。。

  1. $cars_name = ["Honda", "Volvo"];
  2. // Create an entry for each name
  3. $params = str_repeat("(?),", count($cars_name));
  4. // Build a bind for a list of strings
  5. $binds = str_repeat("s", count($cars_name));
  6. // Add the params to the insert (remove the last ,)
  7. $sql = "INSERT INTO car_data (cars_name)
  8. VALUES ".rtrim($params, ",");
  9. $insert = $conn->prepare ( $sql );
  10. // Bind the parameters, using ... is the argument unpacking operator
  11. $insert->bind_param($binds, ...$cars_name);
  12. // Execute the SQL
  13. $insert->execute();

更新:
如果数组中有两个数据项,则可以将上面的内容调整为。。。

  1. // Source data - ensure that the two sets of data have the same number of entries
  2. $car_data = [ 'cars_name' => ["Honda", "Volvo"],
  3. 'cars_modal' => [ '2015', '2016' ]];
  4. $car_count = count($car_data['cars_name']);
  5. // Create an entry for each name (2 binds per entry)
  6. $params = str_repeat("(?,?),", $car_count);
  7. // Build a bind for a list of strings
  8. $binds = str_repeat("ss", $car_count);
  9. // Reformat data for binding (needs to be a single list of the data
  10. // with cars_name followed by cars_modal for each entry)
  11. $merged_data = [];
  12. foreach ( $car_data['cars_name'] as $key => $name ) {
  13. $merged_data[] = $name;
  14. $merged_data[] = $car_data['cars_modal'][$key];
  15. }
  16. // Add the params to the insert (remove the last ,)
  17. $sql = "INSERT INTO car_data (cars_name,car_model)
  18. VALUES ".rtrim($params, ",");
  19. $insert = $conn->prepare ( $sql );
  20. // Bind the parameters, using ... is the argument unpacking operator
  21. $insert->bind_param($binds, ...$merged_data);
  22. // Execute the SQL
  23. $insert->execute();
展开查看全部
hmtdttj4

hmtdttj43#

当我想这样做时,我首先内爆这个数组,得到一个由(,)分隔的普通字符串,然后当我检索数据时,我再次内爆它们。

  1. $cars_name = implode(',', $_POST['cars_name']);

结果将是

  1. Honda,Volvo,Mercedes,Toyota,BMW

如果您想再次从数据库中获取数组,只需执行以下操作:

  1. $cars_array = explode(',', $databaseObject['cars']);

结果将与您的第一个数组相同。

相关问题