通过php上传图片并向mysql添加细节

时间:2014-03-16 17:27:40

标签: php mysql image post file-upload

所以我到处搜索,我根本无法解决这个问题。我正在尝试将POST信息和图像名称上传到SQL,并将图像上传到uploads目录。 SQL将更新除图像行之外的所有信息,也不会实际上传图像。不良后期代码

编辑:我把文件名添加到SQL表中但仍然不会上传文件。

表单信息

<table width="100%" border="0">
      <tr><form action="specialadd.php" method="post">
        <td>Name of Special</td>
        <td>Special Price</td>
      </tr>
      <tr>
        <td valign="top">
        <input type="text" name="name"></td>
        <td><input type="text" name="price"></td>
      </tr>
      <tr>
        <td>Description #1</td>
        <td>Description #2</td>
      </tr>
      <tr>
        <td><textarea name="desc1" rows="6" cols="50"></textarea></td>
        <td><textarea name="desc2" rows="6" cols="50"></textarea></td>
      </tr>
                <tr>
                  <td>Upload Photo</td>
                  <td>&nbsp;</td>
                </tr>
                <tr>
        <td><input type="file" name="image"></td>
        <td><input type="submit" value="Save Your Special"></form></td>
      </tr>
    </table>

PHP信息

<?php 

 //This is the directory where images will be saved 
 $target = "/public_html/uploads"; 
 $target = $target . basename( $_FILES['image']['name']); 

 //This gets all the other information from the form 
 $name=$_POST['name']; 
 $desc1=$_POST['desc1']; 
 $desc2=$_POST['desc2']; 
 $price=$_POST['price']; 
 $image=($_FILES['image']['name']); 

 // Connects to your Database 
 include "process/connect.php";

 //Writes the information to the database 
 mysql_query("UPDATE specials
 SET name='$name', desc1='$desc1', desc2='$desc2', price='$price', image='$image'") ; 

 //Writes the photo to the server 
 if(move_uploaded_file($_FILES['image']['tmp_name'], $target)) 
 { 

 //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."; 
 } 
 ?>

我也想通了这个enctype我完全忘记了这一点。它运作得很好。

1 个答案:

答案 0 :(得分:1)

您需要添加

enctype="multipart/form-data"

在表单中,因此您的表单应为

<form action="specialadd.php" method="post" enctype="multipart/form-data">