2012-03-16 2 views
0

나는 이것을 잘못 작성했다고 확신합니다. 나는 정보를 프로세스 페이지로 보내고 데이터베이스의 테이블에 행을 삽입하는 비트를 입력하는 간단한 양식을 가지고 있습니다. 내 문제는 삽입 부분이 잘 작동한다는 것입니다. 그러나 페이지 내의 템플릿 클래스 또는 다른 것이 표시됩니다. 그냥 빈 페이지. 그 말은 날 미치게했다.PHP 폼 업데이트 모범 사례

함수

 Public function UpdateReason($reason, $bundlereference) { 
     error_reporting(E_ALL^E_NOTICE); 
     $db_selected = mysql_select_db(DB_DATABASE_NAME, $this->conn); 

    if (!$db_selected) { 
    die("Can't use db : " . mysql_error()); 
     } 

    $_reason = mysql_real_escape_string($reason,$this->conn); 
    $_bundlereference = mysql_real_escape_string($bundlereference,$this->conn); 

    $sql = "UPDATE `ArchiveBundle` 
     SET `Issue` = '" . $_reason . "' 
     WHERE `BundleReference` = '" . $_bundlereference . "'"; 

    mysql_query($sql, $this->conn); 

    die(mysql_error()); 
    exit; 
    } 

형태

  <table> 
        <form method='post' action='addissue.php'> 
        <p>Reason: <input type='text' name='reason' /></p><br/> 
        <p><input type='hidden' name='bundlereference' id='Username' 
      value='" . $x['Reference'] . "' /></p> 
        <input type='submit' name ='add'/> 
       </form> 
      </table> 

프로세스 페이지도

<?php 

// First of all initialise the user and check for permissions 
require_once "/var/www/users/user.php"; 
$user = new CHUser(2); 


// Initialise the template 
require_once "/var/www/template/template.php"; 
$template = new CHTemplate(); 

// And create a cid object 
    require_once "/var/www/Testing/DisplayWIPOnLocation.php"; 
$BundleProgress= new CHWIPProgress(); 

    $reason = $_POST['reason']; 
    $reference = $_POST['bundlereference']; 



    $issue = $BundleProgress->UpdateReason($_POST['reason'],$_POST['bundlereference']); 
    Print "Your information has been successfully added to the database."; 


    $template->SetTag("content", $content); 
    echo $template->Display(); 


    ?> 

I이 궁금한 점이 있으시면 php의 양식에 대한 베스트 프랙티스가 무엇인지 배울 수 있습니다.

+0

'die (mysql_error()); exit;이 명령어는 PHP 실행을 완전히 멈추게 만들고, 적어도 첫 번째는 exit이고, exit는 어쨌든 die의 별칭입니다. – SirDarius

답변

6

출력되지 않는 이유는 다음 두 줄입니다. 이것은 스크립트를 제자리에서 죽이고 있습니다. 이걸 제거하십시오.

die(mysql_error()); 
exit; 

전반적으로 끔찍한 것은 아닙니다. 당신은 객체를 사용하고 있으며 db에 삽입하기 전에 사용자 입력을 소독하고 있습니다. 나는 몇 가지 제안을하고있다.

  • 프로덕션 스크립트에 die()을 사용하지 마십시오. 오류 및 예외를 올바르게 처리하십시오.
  • mysql을위한 prepared statement (PDO) 또는 저장 프로 시저를 살펴보십시오.
  • print "Your information has been added..." 대신 템플릿에 필드를 만들고 템플릿 클래스를 통해 그 값을 전달할 수 있습니까?
  • $reason$reference을 입력 한 다음 $_POST의 값을 전달하여 사용하지 않는 변수로 만듭니다. 어쩌면 이것은 단지 테스트 코드 때문일 수 있습니까?
  • 필요에 따라 업데이트 쿼리를 실행하기 전에 양식에서받은 데이터가 유효한지 확인하는 것이 좋습니다. $ reason 또는 $ bundlereference가 유효한 값이 아닌 경우 어떻게합니까?
+1

또 다른 제안 :'mysql_error()'의 반환 값을 출력하지 않는다면 방문자에게 도움이되지 않는 정보가 유출 될 가능성이 있습니다. – Lekensteyn