php - mysqli多次插入未提交数组中的所有行
问题描述
我怀疑我的代码有一个大问题。它只添加$species
,$weight
和的第一个$length
。但如果$_Post中有多个值。它也应该从我的表单中提交它们。我很难看到我哪里出错了。我希望有人能指出我正确的方向吗?
<?php
require 'config.php';
$teamid = $_POST['teamid'];
$species = $_POST['species']; // Can be multiple values depending on how many lines added from form
$weight = $_POST['weight']; // Can be multiple values depending on how many lines added from form
$length = $_POST['length']; // Can be multiple values depending on how many lines added from form
// count($species),($weight),($length) - Should always be the same length
// Processing form data when form is submitted
if($_SERVER["REQUEST_METHOD"] == "POST"){
// Prepare an insert statement
$sql = "INSERT INTO indvejninger ( teamid, artid, vaegt, laengde) VALUES (?, ?, ?, ?)";
if($stmt = $mysqli->prepare($sql)){
foreach ($species as $key => $value) {
// Bind variables to the prepared statement as parameters
$stmt->bind_param("ssss", $_POST['teamid'], $param_species, $param_weight, $param_length);
$param_species = $species[$key];
$param_weight = $weight[$key];
$param_length = $length[$key];
// Attempt to execute the prepared statement
if($stmt->execute()){
// Records created successfully. Redirect to landing page
header("location: index.php?limit=");
exit();
} else{
echo "Something went wrong. Please try again later.";
}
}
// Close statement
$stmt->close();
}
// Close connection
$mysqli->close();
}
解决方案
我发现了错误,第一次提交后它更改了 url。所以它不会完成其他提交。
// Attempt to execute the prepared statement
if($stmt->execute()){
// Records created successfully. Redirect to landing page
header("location: index.php?limit=");
exit();
} else{
echo "Something went wrong. Please try again later.";
}
因此,我需要它来完成其他提交,然后重定向到登录页面。
推荐阅读
- python - PermissionError: [Errno 13] Permission denied: 'ffmpeg' Open AI GYM
- jestjs - “路由器”类型上不存在属性“navigateByData”
- bash - 重命名目录中的文件以在破折号前后插入空格
- reactjs - 在“连接(登录)”的上下文中找不到“商店”
- java - 可能的选择数量有限?
- fortran - 如何修复英特尔视觉 Fortran 中的“错误 LNK2019:libifcoremdd.lib(for_main.obj)”
- python - Sklearn 线性回归给我不准确的准确读数?
- tensorflow - Keras 中的权重和可训练变量是否相同?
- c# - 如何使每个二进制代码前面都有一个('),后面有一个(',)?前任。'01011011',
- android - 我可以将 Apk 文件转换回 android studio 项目吗?