上传照片并将其名称添加到MySQL

时间:2015-06-20 14:42:58

标签: php mysql

我有一个可以输入字符串的表单(必须放入数据库的名称),以及2个用于文件的表单。这些文件必须上传到一个文件夹,并将它们的名字放在sql数据库中。

编辑:数据库部分有效,但我无法将其上传到文件夹。

这是我的代码。

    <!DOCTYPE html>
<html>
    <head>
        <title>Uploading</title>
    </head>
    <body>
     <form method="post" action="fototoevoegen.php" enctype="multipart/form-data">
            <p>
              Foto Name
            </p>

            <input type="text" name="fotonaam"/>
            <p>
              First Foto. Max size is 500kb.
            </p>

            <input type="hidden" name="size" value="5000000">
            <input type="file" name="photo"> 


         <p> Second Foto. Max size is 500kb.</p>
         <input type="hidden" name="size2" value="5000000">
            <input type="file" name="photo1"> 
            <br/>
            <br/>
            <input TYPE="submit" name="upload" title="Add data to the Database" value="Add Member"/>
          </form>

    </body>
</html>

和PHP代码

         <?php



//This gets all the other information from the form
$name=$_POST['fotonaam'];


$picname1 = $_FILES["photo"]["name"];  

$picname2= $_FILES["photo1"]["name"];

//This is the directory where images will be saved
$target = "images/".$picname1;
$target2 = "images/".$picname2;

if((move_uploaded_file($_FILES['photo']['tmp_name'], $target)) && (move_uploaded_file($_FILES['photo1']['tmp_name'], $target2)) )
{
Echo "Succes";
    // Connects to your Database
mysql_connect("localhost", "root", "") or die(mysql_error()) ;
mysql_select_db("Manu") or die(mysql_error()) ;

//Writes the information to the database
mysql_query("INSERT INTO fotos (naamfoto,foto,fotothumb)
VALUES ('$name', '$picname1', '$picname2')") ;

//Tells you if its all ok
}
else {

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





?>

1 个答案:

答案 0 :(得分:2)

您正在尝试将文件输入数据库,而不是它的名称。
保存照片&#34; 名称&#34;到数据库表,必须是


    $picname1 = $_FILES["photo"]["name"];  // $picname1 contains name of photo 1

    $picname2= $_FILES["photo1"]["name"];  // $picname2 contains name of photo 2

然后将其添加到查询


    mysql_query("INSERT INTO tableName (naamfoto,foto,fotothumb)
    VALUES ('$name', '$picname1', '$picname2')") ;



并请改变

if((move_uploaded_file($_FILES['photo']['tmp_name'], $target)) && (move_uploaded_file($_FILES['photo1']['tmp_name'], $target)) )

if((move_uploaded_file($_FILES['photo']['tmp_name'], $target.$picname1)) && (move_uploaded_file($_FILES['photo1']['tmp_name'], $target.$picname2)) )