【问题标题】:mysqli multiple insert not submitting all lines from arraymysqli多次插入未提交数组中的所有行
【发布时间】:2019-02-23 00:50:54
【问题描述】:

我怀疑我的代码存在很大问题。 它只添加$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();
}

【问题讨论】:

    标签: php mysqli bulkinsert


    【解决方案1】:

    我发现了错误,第一次提交后更改了 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.";
            }
    

    因此,我需要它来完成其他提交,然后重定向到登录页面。

    【讨论】:

      猜你喜欢
      • 2015-06-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-30
      相关资源
      最近更新 更多