제출 시스템을 설정 했으므로 중복 된 항목을 제출할 수 없습니다. 제출 된 경우 원본 레코드 및 파일 업로드가 유지됩니다 (덮어 쓰지 않음). 또한, 존재하는 경우 양식을 사용자에게 오류를 표시하고 싶습니다. 여기에 내 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.";
}
?>
테이블 구조는 어떤 모양입니까? –
그래, 그런 삽입은 나쁜 생각이야. 중간에 열을 추가하여 테이블을 수정하면이 쿼리가 중단됩니다. 예를 들어, 중간 머리 글자를 모으고 싶다면 그 칼럼을 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). " ')") –
SYNTAX 오류가 발생하지만 원본 코드에 추가했습니다. –