2016-10-11 3 views
0

간략하게 말하면 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 문을 가지고 당신이 볼 수?

+0

하지 마십시오 'redirect_to'는 PHP에서의 일이라고 생각합니다. 'header ('Location : '.'new/path/file.php ');' – nerdlyist

+0

질문을 좀 더 명확하게하고 싶을 수도 있습니다 .... AFTER all 진술이 실행 되었습니까? 모든 명령문을 작성한 다음 대량으로 실행하고 모두 성공한 후에 리디렉션하려고합니까? – Zach

+0

은'$ stmt-> execute()'가 두 번 호출 되는가? 아마도 두 번째로 기대했던 것과 같은 쿼리를 실행하지 않을 것입니다. 그리고'$ loc_info'의 값 할당 위의 블록에 무엇이 있습니까? if 문 위에 닫는 중괄호가 있습니다. 그래서 위에 무엇이 있는지 모른다면 우리가 알기가 어렵습니다. –

답변

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"); 

이 전부입니다 :

여기 당신이 필요로하는 전체 코드 (트리밍 배열 저장)를 간다.

이 코드는 모든 실행이 성공적으로 실행되면 리디렉션됩니다. 당신은 여기에 구조 문제의 비트

+0

예, 감사합니다! 나는 그것을 생각할 줄 알았다. –

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"); 
} 
+0

코드가 1입니다. 쓸모가 없습니다. 2. 어쨌든 작동하지 않습니다. $ e-> getMessage(); 너는 생각대로 작동하지 않는다. 사실 PHP는 여러분이 말한 모든 것을 할 수 있습니다 - 여분의 코드 라인없이 오류를 추적하고 성공 여부를 확인하십시오. 제가 게시 한 코드를 확인하십시오. –

+0

@YourCommonSense : 2) 맞습니다.'$ e-> getMessage();'대신'$ stmt-> errorInfo()'를 사용해야합니다. "그 $ e-> getMessage(); thingy는 생각하는대로 작동하지 않습니다."실제로 생각대로 작동합니다 ... 저는 여기에서 사용하려고하지 않았습니다. 1)이 코드는 쓸모가 없으며 OP가 의도 한대로 적절하지 않을 수 있습니다. 0) LPT : 그런 거시기하지 마십시오. – Zach