多文件上传不保存

时间:2013-10-13 14:12:08

标签: php mysql

我现在已经尝试了好几天来绕过这一个,但我不能让它发挥作用。我是php的新手,我相信你们可以帮助或指出我做错了什么。

我有一个需要将文件上传到某个目录的表单,虽然浏览器进度条显示文件正在上传,但它仍未保存在目录中,也未在MySql的表字段中捕获。

具有多重上传表单的文件:

     <table cellpadding="5" cellspacing="0">
 <form action="includes/add.php" method="post" enctype="multipart/form-data">

 <th>Ad Warnings Documents</th>
 <tr>
 <td>Warning File 1</td>
 <td><input type="file" name="warning1" /></td>

 </tr>
  <tr>
 <td>Warning File 2</td>
 <td><input type="file" name="warning2"  /></td>

 </tr>
  <tr>
 <td>Warning File 3</td>
 <td><input type="file" name="warning3" /></td>
 </tr>

  <tr><td><input type="submit"  value="add"></form></td></tr>
 </table>

 </div>


 </body>
 </html> 

上传脚本:

            <?php
include 'core/init.php';
 // Connects to your Database 
include 'includes/overall/header.php';
error_reporting(1);

$connect_error = 'Sorry, we\'re experiencing connection problems.';
mysql_connect('localhost', 'username', 'password') or die($connect_error);
mysql_select_db('DB_Name') or die($connect_error);

  //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=mysql_real_escape_string($_FILES['warning1']['name']); 
 $warning2=mysql_real_escape_string($_FILES['warning2']['name']);
 $warning3=mysql_real_escape_string($_FILES['warning3']['name']); 


  //Writes the information to the database 
mysql_query("INSERT INTO ref_employees (`warning1`,`warning2`,`warning3`) 
VALUES ('$warning1', '$warning2', '$warning3')")  or die(mysql_error());
  //Writes the file to the server 
if (move_uploaded_file($_FILES['warning1']['tmp_name'], $target1))
echo "The file ". basename( $_FILES['warning1']['name']). " has been uploaded, and your information has been added to the directory"; 
if (move_uploaded_file($_FILES['warning2']['tmp_name'], $target2))
 echo "The file ". basename( $_FILES['warning2']['name']). " has been uploaded, and your information has been added to the directory"; 
if (move_uploaded_file($_FILES['warning3']['tmp_name'], $target3))
    echo "The file ". basename( $_FILES['warning3']['name']). " has been uploaded, and your information has been added to the directory";
 //Tells you if its all ok 

 else { 

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

 }
 ?>

 <?php
include 'includes/overall/footer.php'; ?>

1 个答案:

答案 0 :(得分:1)

您没有在SQL语句中指定列名称,并且值count可能多于或少于列数。我建议您从现在开始为每个INSERT语法指定所有必需的列名。

mysql_query("INSERT INTO ref_employees (`col1`,`col2`,`col3`) 
VALUES ('$warning1', '$warning2', '$warning3')") ;