2016-07-22 2 views
0

일단 데이터가 데이터베이스에 게시되면 사용자가 새 페이지로 리디렉션하고 방금 삽입 한 레코드의 last_id 결과를 표시하는 포스트 PHP 페이지가 있습니다.게시물 이후 리다이렉트하지 않음

데이터 게시가 제대로되었는지 확인할 수는 있지만 리디렉션이 작동하지 않습니다. 그냥 앉아서 아주 슬퍼 보입니다. 그것은 한 번 일했지만 지금은 다시 작동하지 않을 수 있습니다.

질문에 조각은 다음과 같습니다

if ($conn->query($sql) === TRUE) { 
     $last_id = $conn->insert_id; 
      echo ' <head> <meta http-equiv="refresh" content="1;url=http://vmcnucmed.cvm.umn.edu/patientinjection/view_inj_info.php?id=' . $last_id;'"></head> 
      Patient Added Successfully. <br /><br />Database ID: ' . $last_id;'<br>'; 
} 

전체의 스크립트입니다 :

<title>Post Patient Injection Information</title> 
<?php 
$erors = array();      // set an empty array that will contains the errors 

// Check for form submission 
if (isset($_POST['patientID']) && isset($_POST['lastname']) && isset($_POST['patientname'])&& isset($_POST['dose'])) { 
    // remove tags and whitespace from the beginning and end of form data 
    $_POST = array_map("strip_tags", $_POST); 
    $_POST = array_map("trim", $_POST); 

    // chech if all form fields are filled in correctly 
    // (minimum number of characters in "patientID") 
    if (strlen($_POST['patientID'])<3) $erors[] = 'Patient ID must contain at least 3 characters.'; 


    // if no errors ($error array empty) 
    if(count($erors)<1) { 
    // connect to the "tests" database 
    $conn = new mysqli('somehost', 'someuser', 'somepassword', 'somedatabase'); 

    // check connection 
    if (mysqli_connect_errno()) { 
     exit('Connect failed: '. mysqli_connect_error()); 
    } 

    // store the values in an Array, escaping special characters for use in the SQL statement 
    $adds['pkgnumberID'] = $conn->real_escape_string($_POST['pkgnumberID']); // package number ID 
    $adds['rxnumber'] = $conn->real_escape_string($_POST['rxnumber']); // patient prescription number 
    $adds['patientID'] = $conn->real_escape_string($_POST['patientID']); // Patient ID/MRN number 
    $adds['lastname'] = $conn->real_escape_string($_POST['lastname']); // patient last name 
    $adds['patientname'] = $conn->real_escape_string($_POST['patientname']); // patient name 
    $adds['isotope'] = $conn->real_escape_string($_POST['isotope']); // isotope injected 
    $adds['radiopharmaceutical'] = $conn->real_escape_string($_POST['radiopharmaceutical']); // radiopharmaceutical injected 
    $adds['dose'] = $conn->real_escape_string($_POST['dose']); // dose injected 
    $adds['datetimestated'] = $conn->real_escape_string($_POST['datetimestated']); // date/time stated activity 
    $adds['datetimeadmin'] = $conn->real_escape_string($_POST['datetimeadmin']); // date/time administered 
    $adds['adminby'] = $conn->real_escape_string($_POST['adminby']); // administered by 

    // sql query for INSERT INTO tbl_patientdoseinformation 
    $sql = "INSERT INTO `tbl_patientdoseinformation` (`pkgnumberID`,`rxnumber`,`patientID`,`lastname`,`patientname`,`isotope`,`radiopharmaceutical`,`dose`,`datetimestated`,`datetimeadmin`,`adminby`) 
    VALUES ('". $adds['pkgnumberID']. "','". $adds['rxnumber']. "','". $adds['patientID']. "','". strtoupper($adds['lastname']). "','". strtoupper($adds['patientname']). "','". $adds['isotope']. "','". $adds['radiopharmaceutical']. "','". $adds['dose']. "','". $adds['datetimestated']. "','". $adds['datetimeadmin']. "','". $adds['adminby']. "')"; 

    // Performs the $sql query on the server to insert the values 
    if ($conn->query($sql) === TRUE) { 
    $last_id = $conn->insert_id; 
     echo ' <head> <meta http-equiv="refresh" content="1;url=http://vmcnucmed.cvm.umn.edu/patientinjection/view_inj_info.php?id=' . $last_id;'"></head> 
     Patient Added Successfully. <br /><br />Database ID: ' . $last_id;'<br>'; 
    } 
    else { 
     echo 'Error: '. $conn->error; 
    } 

    $conn->close(); 
    } 
    else { 
    // else, if errors, it adds them in string format and print it 
    echo implode('<br />', $erors); 
    } 
} 
else { 
    echo 'No data from form'; 
} 
?> 

답변

1

게시 후 리디렉션을위한 모범 사례는 위치 헤더를 보내는 것입니다. wi 대신 HTML 메타 태그로 리디렉션의 302 상태 코드를 토륨 :

header(
    'Location: http://vmcnucmed.cvm.umn.edu/patientinjection/view_inj_info.php?id=' . $last_id, 
    TRUE, // rewrite existing Location header 
    302 // set status code 
); 

302 코드 페이지를 다시로드로 대신 POST 요청을 GET 브라우저를 강제 할 것이다.

+0

예, 완벽하게 작동했습니다. 고마워요! –

1

난 당신이 응답 헤더에 리디렉션을 반환 할 경우 더 나은 결과를 얻을 수있을 거라 생각 이렇게 ...

header('Location: http://vmcnucmed.cvm.umn.edu/patientinjection/view_inj_info.php?id=' . $last_id); 
관련 문제