上传文件并保存

时间:2013-10-07 12:12:05

标签: php mysql

再次回来

所以我已经移动了一些代码,现在只获得了不成功的消息。请告诉我在哪里以及我做错了什么。

我在editemployee.php页面上的表单

<h1>Warnings</h1>
<div class="article" style="width:535px">
 <form enctype="multipart/form-data" action="includes/add.php" method="POST"> 
 <table>
  <tr>
  <td>Warning letter:</td>
  <td> <input type="file" name="warning1"></td>
  </tr>
   <tr>
  <td>Warning letter:</td>
  <td><input type="file" name="warning2"></td>
  </tr>
   <tr>
  <td>Warning letter Final:</td>
  <td><input type="file" name="warning3"></td>
  </tr>
   <tr>
  <td></td>
  <td><input type="submit" value="Add"> </td>
  </tr>
  </table>

 </form>

 </div>

现在我已按照建议将处理代码移动到名为add.php

的文件中
<?php 
  //This is the directory where images will be saved 
$target = "files/empdocs"; 
$target1 = $target . basename( $_FILES['warning1']['name']); 
$target2 = $target . basename( $_FILES['warning2']['name']);
$target3 = $target . basename( $_FILES['warning3']['name']);

 //This gets all the other information from the form 

 $warning1=($_FILES['warning1']['name']); 
 $warning2=($_FILES['warning2']['name']);
 $warning3=($_FILES['warning3']['name']); 

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

  //Writes the information to the database 
 mysql_query("INSERT INTO ref_employees VALUES ('$warning1', '$warning2', '$warning3')") ;
  //Writes the file to the server 
if (move_uploaded_file($_FILES['warning1']['tmp_name'], $target1)
&& move_uploaded_file($_FILES['warning2']['tmp_name'], $target2)
&& move_uploaded_file($_FILES['warning3']['tmp_name'], $target3)) {

 //Tells you if its all ok 
 echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory"; 
 } 
 else { 

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

3 个答案:

答案 0 :(得分:1)

你有:

mysql_query("INSERT INTO ref_employees VALUES ('$warning1', '$warning2', '$warningfinal') WHERE 'idnumber' = $idnumber") ; 

WHERE不应该在INSERT声明中!

答案 1 :(得分:0)

没有这样的东西:

if(move_uploaded_file($_FILES['warning1']['tmp_name'], $target))
if(move_uploaded_file($_FILES['warning2']['tmp_name'], $target))
if(move_uploaded_file($_FILES['warning3']['tmp_name'], $target))  {

你必须

if (move_uploaded_file($_FILES['warning1']['tmp_name'], $target1)
&& move_uploaded_file($_FILES['warning2']['tmp_name'], $target2)
&& move_uploaded_file($_FILES['warning3']['tmp_name'], $target3)) {

目标变量,不是这样的:

$target = "files/empdocs"; 
$target = $target . basename( $_FILES['warning1']['name']); 
$target = $target . basename( $_FILES['warning2']['name']);
$target = $target . basename( $_FILES['warning3']['name']);

可是:

$target = "files/empdocs"; 
$target1 = $target . basename( $_FILES['warning1']['name']); 
$target2 = $target . basename( $_FILES['warning2']['name']);
$target2 = $target . basename( $_FILES['warning3']['name']);

答案 2 :(得分:0)

如果您想使用JQuery和PHP上传具有良好UI的多文件,您可以检查此链接并尝试使用JQuery-File-Upload库。这是非常好的库。

我认为它会帮助你

链接是 https://github.com/blueimp/jQuery-File-Upload