I have a PHP form that is supposed to allow the user to upload 5 images at once. All images should save into my website's folder images/
when the user hits submit. Currently, only the image in the first image input gets saved into the images/
folder. The image names are all saved into my MySQL table correctly, though.
Here is the code for my HTML form page:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>
<body>
<form enctype="multipart/form-data" action="add.php" method="POST">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name = "email"><br>
Phone: <input type="text" name = "phone"><br>
Photo: <input type="file" name="photo"><br>
Photo: <input type="file" name="photo1"><br>
Photo: <input type="file" name="photo2"><br>
Photo: <input type="file" name="photo3"><br>
Photo: <input type="file" name="photo4"><br>
<input type="submit" value="Add">
</form>
</body>
</html>
And here is the add.php page code:
<?php
//This is the directory where images will be saved
$target = "images/";
$target = $target . basename( $_FILES['photo']['name']);
$target1 = "images/";
$target1 = $target1 . basename( $_FILES['photo1']['name']);
$target2 = "images/";
$target2 = $target2 . basename( $_FILES['photo2']['name']);
$target3 = "images/";
$target3 = $target3 . basename( $_FILES['photo3']['name']);
$target4 = "images/";
$target4 = $target4 . basename( $_FILES['photo4']['name']);
//This gets all the other information from the form
$name=$_POST['name'];
$email=$_POST['email'];
$phone=$_POST['phone'];
$pic=($_FILES['photo']['name']);
$pic1=($_FILES['photo1']['name']);
$pic2=($_FILES['photo2']['name']);
$pic3=($_FILES['photo3']['name']);
$pic4=($_FILES['photo4']['name']);
// Connects to your Database
mysql_connect("dnsacom", "ksbm", "Kszer") or die(mysql_error()) ;
mysql_select_db("keabm") or die(mysql_error()) ;
//Writes the information to the database
mysql_query("INSERT INTO `employees` VALUES ('$name', '$email', '$phone', '$pic', '$pic1', '$pic2', '$pic3', '$pic4')") ;
//Writes the photo to the server
if(move_uploaded_file($_FILES['photo']['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 image.";
}
if(move_uploaded_file($_FILES['photo1']['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 image.";
}
if(move_uploaded_file($_FILES['photo2']['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 image.";
}
if(move_uploaded_file($_FILES['photo3']['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 image.";
}
if(move_uploaded_file($_FILES['photo4']['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 image.";
}
?>
Thank you for any help. All help is appreciated.