成功插入PDO后重定向功能

时间:2016-10-11 19:01:06

标签: php redirect pdo

简而言之,我正在寻找一种在成功插入PDO后进行重定向的方法。这是我到目前为止所拥有的。

重定向功能

<?php
function redirect_to($new_location) {
      header("Location: " . $new_location);
      exit;
    }
?>

PDO INSERT

请注意,我在下面的示例中修剪了一些代码,以便于阅读。

try {
        $sql = "INSERT INTO location_info (`department`, `participant`, `activity`, `location`, `rec_injuries`, `rec_injuries_timeframe`, `non_rec_injuries` )
        VALUES (:department, :participant, :activity, :location, :rec_injuries, :rec_injuries_timeframe, :non_rec_injuries, :non_rec_injuries_timeframe, '{$id}')";

        $stmt = $db->prepare($sql);

for($i = 0, $l = count($_POST["department_name"]); $i < $l; $i++) { 

    $loc_info = array(':department' => $_POST["department_name"][$i],
                        ':rec_injuries_timeframe' => $_POST["injury_time_frame"][$i],
                        ':non_rec_injuries' => $_POST["non_rec_injuries"][$i],
                        ':non_rec_injuries_timeframe' => $_POST["non_rec_injury_timeframe"][$i],
                        ':competitor' => $_POST["competitor"][$i],
                        ':cost_per_pair' => $_POST["cost_per_pair"][$i],
                        ':usage_rate' => $_POST["usage_rate"][$i],
                        ':leakage' => $_POST["leakage"][$i],
                        ':cost_of_productivity' => $_POST["cost_of_productivity"][$i],
                        ':non_rec_impact' => $_POST["non_rec_impact"][$i],
                        ':non_rec_sprain' => $_POST["non_rec_sprain"][$i],
                        ':non_rec_puncture' => $_POST["non_rec_puncture"][$i],
                        ':non_rec_dermatitis' => $_POST["non_rec_dermatitis"][$i],
                        ':non_rec_infection' => $_POST["non_rec_infection"][$i],
                        ':non_rec_burns' => $_POST["non_rec_burns"][$i],
                        ':non_rec_cuts' => $_POST["non_rec_cuts"][$i],
                        ':rec_impact' => $_POST["impact"][$i],
                        ':rec_sprain' => $_POST["sprain"][$i],
                        ':rec_puncture' => $_POST["puncture"][$i],
                        ':rec_dermatitis' => $_POST["dermatitis"][$i],
                        ':rec_infection' => $_POST["infection"][$i],
                        ':rec_burns' => $_POST["burns"][$i],
                        ':rec_cuts' => $_POST["cuts"][$i],
                        ':condition' => $_POST["condition"][$i] );

$stmt->execute($loc_info);
}
 if ($stmt->execute()) {
        redirect_to($_SERVER["DOCUMENT_ROOT"]."/testing/tim/results.php"); 

}
}
 catch (Exception $e) {
        $error = $e->getMessage();
        print "<b>error:</b> " . $error;
    }   

您会看到我使用if ($stmt->execute()) { redirect_to($_SERVER["DOCUMENT_ROOT"]."/testing/tim/results.php");

进行重定向的if语句

我哪里错了?

2 个答案:

答案 0 :(得分:2)

  

我哪里错了?

当您添加大量无用的代码时。

这里是您需要的完整代码(保存修剪后的数组):

$sql = "INSERT INTO location_info (`department`, `participant`, `activity`, `location`, `rec_injuries`, `rec_injuries_timeframe`, `non_rec_injuries` )
    VALUES (:department, :participant, :activity, :location, :rec_injuries, :rec_injuries_timeframe, :non_rec_injuries, :non_rec_injuries_timeframe, '{$id}')";

$stmt = $db->prepare($sql);
for($i = 0, $l = count($_POST["department_name"]); $i < $l; $i++) { 
    $loc_info = array(
        ':department' => $_POST["department_name"][$i],
        ':condition' => $_POST["condition"][$i]
    );
    $stmt->execute($loc_info);
}
redirect_to("/testing/tim/results.php"); 

这就是全部。

如果所有执行都成功执行,此代码将重定向。

答案 1 :(得分:0)

这里有一点结构问题......

如果您想要执行不确定数量的查询,那么在所有查询成功执行后重定向,则需要跟踪所有语句执行和错误。

如果你想在第一次有错误时抛出错误并停止插入,那么你只需检查执行函数的返回结果,如果失败就抛出错误:

$sql = "INSERT INTO location_info (`department`, `participant`, `activity`, `location`, `rec_injuries`, `rec_injuries_timeframe`, `non_rec_injuries` )
    VALUES (:department, :participant, :activity, :location, :rec_injuries, :rec_injuries_timeframe, :non_rec_injuries, :non_rec_injuries_timeframe, '{$id}')";

$stmt = $db->prepare($sql);
$errors = array();

for($i = 0, $l = count($_POST["department_name"]); $i < $l; $i++) { 

    $loc_info = array(':department' => $_POST["department_name"][$i],
                    ':rec_injuries_timeframe' => $_POST["injury_time_frame"][$i],
                    ':non_rec_injuries' => $_POST["non_rec_injuries"][$i],
                    ':non_rec_injuries_timeframe' => $_POST["non_rec_injury_timeframe"][$i],
                    ':competitor' => $_POST["competitor"][$i],
                    ':cost_per_pair' => $_POST["cost_per_pair"][$i],
                    ':usage_rate' => $_POST["usage_rate"][$i],
                    ':leakage' => $_POST["leakage"][$i],
                    ':cost_of_productivity' => $_POST["cost_of_productivity"][$i],
                    ':non_rec_impact' => $_POST["non_rec_impact"][$i],
                    ':non_rec_sprain' => $_POST["non_rec_sprain"][$i],
                    ':non_rec_puncture' => $_POST["non_rec_puncture"][$i],
                    ':non_rec_dermatitis' => $_POST["non_rec_dermatitis"][$i],
                    ':non_rec_infection' => $_POST["non_rec_infection"][$i],
                    ':non_rec_burns' => $_POST["non_rec_burns"][$i],
                    ':non_rec_cuts' => $_POST["non_rec_cuts"][$i],
                    ':rec_impact' => $_POST["impact"][$i],
                    ':rec_sprain' => $_POST["sprain"][$i],
                    ':rec_puncture' => $_POST["puncture"][$i],
                    ':rec_dermatitis' => $_POST["dermatitis"][$i],
                    ':rec_infection' => $_POST["infection"][$i],
                    ':rec_burns' => $_POST["burns"][$i],
                    ':rec_cuts' => $_POST["cuts"][$i],
                    ':condition' => $_POST["condition"][$i] );

    if(!$stmt->execute($loc_info)){
        $errors[] = $e->getMessage();

        // un-comment if you want to stop on error:
        // print "<b>error:</b> " . $e->getMessage();
        // die();
    };
}

if(count($errors)){
    foreach($errors as $error){
        print "<b>error:</b> " . $e->getMessage()."<br/>";
    }
} else {
    redirect_to($_SERVER["DOCUMENT_ROOT"]."/testing/tim/results.php");
}