user2340383
user2340383

Reputation: 43

Uploading photos and adding their name to MySQL

I have a form that can enter a string (a name that has to be put in the database), and 2 for files. Those files have to be uploaded to a folder, and their names put in the sql database.

EDIT: The database part works, but i cant get it uploaded to the folder.

Here's my code.

    <!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>

And the PHP Code

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





?>

Upvotes: 2

Views: 71

Answers (1)

Unni Babu
Unni Babu

Reputation: 1814

You are trying to enter a file into database, not it's name.
When saving photo "name" to database table, it must be


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

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

then add this to query


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



And pls change

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

to

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

Upvotes: 2

Related Questions