2011-11-02 4 views
1

제출 시스템을 설정 했으므로 중복 된 항목을 제출할 수 없습니다. 제출 된 경우 원본 레코드 및 파일 업로드가 유지됩니다 (덮어 쓰지 않음). 또한, 존재하는 경우 양식을 사용자에게 오류를 표시하고 싶습니다. 여기에 내 upload.php (HTML 양식 참조)가 있습니다.양식을 통한 파일 업로드 및 MySQL 레코드 덮어 쓰기 방지?

<?php 

//This is the directory where images will be saved 
$extension = explode(".", $_FILES['upload']['name']); 
$extension = $extension[count($extension)-1]; 
$target = "uploads/"; 
$target = $target . $_POST['snumber'] . "." . $extension; 

//This gets all the other information from the form and prevents SQL injection 
$fname=$_POST['fname']; 
$lname=$_POST['lname']; 
$upload=($_FILES['upload']['name']); 
$snumber=$_POST['snumber']; 
$grade=$_POST['grade']; 
$email=$_POST['email']; 

// Connects to your Database 
mysql_connect("localhost", "db_user", "password") or die(mysql_error()) ; 
mysql_select_db("db_name") or die(mysql_error()) ; 

//Writes the information to the database 
mysql_query("INSERT INTO `Table` VALUES ('$fname', '$lname', '$snumber', '$grade', '$email', '$target')") ; 

//Writes the upload to the server 
if(move_uploaded_file($_FILES['upload']['tmp_name'], $target)) 
{ 
//Tells you if its all ok 
echo "Your submission ". basename($_FILES['uploadedfile']['name']). " was successful and we have received your submission. Your result will be sent to $email "; 
} 
else { 

//Gives and error if its not 
echo "Sorry, there was a problem uploading your file."; 
} 
?> 

가 어떻게 이렇게 가겠어요 upload.php로?

편집 : 당신이 당신의 데이터베이스에서 대상을 저장하는 것처럼 아래에서 결합 제안,하지만 지금은

<?php 

//This is the directory where images will be saved 
$extension = explode(".", $_FILES['upload']['name']); 
$extension = $extension[count($extension)-1]; 
$target = "uploads/"; 
$target = $target . $_POST['snumber'] . "." . $extension; 

//This gets all the other information from the form and prevents SQL injection 
$fname=$_POST['fname']; 
$lname=$_POST['lname']; 
$upload=($_FILES['upload']['name']); 
$snumber=$_POST['snumber']; 
$grade=$_POST['grade']; 
$email=$_POST['email']; 

//Checks if submission already exists 
if(file_exists($target)) 
{ 
    echo "This submission already exists. Please check that you have entered all values correctly. If this is an error please contact support"; 
} 
else 
{ 
    //Now that file doesn't exist, move it. 
    move_uploaded_file($_FILES['upload']['tmp_name'], $target); 
    //MYSQL CONNECTION 
     mysql_connect("localhost", "db_user", "password") or die(mysql_error()) ; 
     mysql_select_db("db_name") or die(mysql_error()) ; 
    //MYSQL Entry 
     mysql_query("INSERT INTO Table (fname, lname, snumber, grade, email, target) VALUES ('".mysql_real_escape_string($fname)."', '".mysql_real_escape_string($lname)."', '".mysql_real_escape_string($snumber)."', '".mysql_real_escape_string($grade)."', '".mysql_real_escape_string($email)."', '".mysql_real_escape_string($target)."')") 

    echo "Your submission was successful and we have received your portfolio. Your marks will be sent out to $email."; 
} 
?> 
+0

테이블 구조는 어떤 모양입니까? –

+1

그래, 그런 삽입은 나쁜 생각이야. 중간에 열을 추가하여 테이블을 수정하면이 쿼리가 중단됩니다. 예를 들어, 중간 머리 글자를 모으고 싶다면 그 칼럼을 fname과 lname 사이에 넣을 수 있습니다. 그런 다음 쿼리가 실패합니다. VALUES (' ".mysql_real_escape_string ($ fname)."', ' ") ("INSERT INTO'테이블'('fname','lname','snumber','grade','email','target') .mysql_real_escape_string ($ lname). " ','".mysql_real_escape_string ($ snumber). " ','".mysql_real_escape_string ($ grade). " ','".mysql_real_escape_string ($ email). " ','".mysql_real_escape_string ($ target). " ')") –

+0

SYNTAX 오류가 발생하지만 원본 코드에 추가했습니다. –

답변

1

upload.php로 Parse error: syntax error, unexpected T_ECHO in /path/to/upload.php on line 32

새로운납니다 여기에 업데이트됩니다 코드는 그렇게 보이는 해당 파일이 이미 있는지 또는 PHP의 file_exists() 함수를 사용할 수 있는지 데이터베이스를 확인할 수 있습니다.

DB 당신은 분명히 그 성명을 삽입하기 전에 쿼리를 실행하고 조건에 따라 결과를 만듭니다.

그렇지 않으면

,

if(file_exists($target)) 
{ 
    echo 'error'; 
} 
else 
{ 
    move_uploaded_file($_FILES['upload']['tmp_name'], $target); 
    // do success things here 
} 

파일의 전체 경로를 요구할 수있다 존재한다. 그래도 작동하지 않는다면 $ _SERVER [ 'DOCUMENT_ROOT']가 도움이되는지 확인하십시오.

+0

그리고'else '케이스 안에서'INSERT'을 수행하십시오. 그렇지 않으면 데이터베이스에 레코드를 두 번 넣을 것입니다. –

+0

그래서'INSERT' (SQL 문)가 else case에 추가되면 파일 존재 체크가 끝난 후에 행을 추가 만 할 것인가? –

+0

@VedPetkar 가장 좋은 방법은'move_uploaded_file'이 성공했는지 확인하는 것입니다 .' move_uploaded_file'가 TRUE를 반환하면 데이터베이스에 추가하십시오. –

0

나는

if($_GET['action'] == 'testfile'){ 
    $msg = ''; 
    $basedirpath = $_GET['dirpath'] . "/"; 

    if(file_exists($basedirpath . $_GET['file'])) { 
     $msg = 'exists'; 
    } 
    echo $msg; 
} 

$ 데이터를 에코 형태와 사용자가 파일을 테스트하고

var param = "action=testfile&dirpath=" + dirpath + "&file=" + filename; 

    $.ajax({ 
     type: "GET", 
     url: 'combi/testfile.php', 
     data: param, 
     success: function(data) { 
      test data .... if OK submit. 
     } 

testfile.php에서 파일을 제출하기 전에 아약스 쿼리를 적용하여이 문제를 해결 한 msg는 ajax 호출의 데이터로 반환됩니다.