我有一個提交系統設置,我想要它,所以沒有重複的條目可以提交。如果提交了一個,原始記錄和文件上傳被保留(不被覆蓋)。此外,如果存在,我希望表單向用戶顯示錯誤。這是我的upload.php(在HTML表單中提到)。防止通過表單覆蓋文件上傳和MySQL記錄?
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'];
// 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.";
}
?>
我怎麼會去這樣做呢?
編輯:從下面結合的建議,這裏是更新的代碼但現在我得到一個Parse error: syntax error, unexpected T_ECHO in /path/to/upload.php on line 32
新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之間。然後查詢失敗。應該是(「INSERT INTO表」('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)。「')」) –
將其添加到原始代碼中,但獲取SYNTAX錯誤。 –