Я подозреваю, что у меня большая проблема с кодом.
Он добавляет только первые из $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.";
}
Так что вместо этого мне нужно закончить другие отправки, а затем перенаправить на целевую страницу.