Uploading multiple files to server writing detail to MySQL

images php coding issues or problems here.

Moderators: macek, egami, gesf

dchionsini
New php-forum User
New php-forum User
Posts: 1
Joined: Fri Dec 31, 2010 1:22 pm

Uploading multiple files to server writing detail to MySQL

Postby dchionsini » Fri Dec 31, 2010 1:37 pm

I need some assistance. I'm just learning PHP and found an easy to understand example and implement upload for a file using PHP, MySQL, and HTML. I've provided the fields for the DB, example of the HTML, and example of the PHP script. What I'm looking to do is expand the functionality to allow me the ability to upload up to 3 files at one time writing the id, name, email, phone, and file1, file2, file3 details to the MySQL database and upload the 3 files to a directory on the server.

Can someone show me how the current code I've provided could do this. Obviously, I'm probably looking at creating an array of files. Problem is, I don't know what mods need to be made to the HTML and the PHP.


Database:
id (primary key)
name
email
phone
phone

HTML Form:
<form enctype="multipart/form-data" action="add.php" method="POST">
Company ID: <input type="text" name="id"><br>
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>
<input type="submit" value="Add">
</form>

PHP Script:
<?php

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

//This gets all the other information from the form
$id=$_POST['id'];
$name=$_POST['name'];
$email=$_POST['email'];
$phone=$_POST['phone'];
$pic=($_FILES['photo']['name']);

// Connects to your Database
mysql_connect("xxx.xxx.xxx.xx", "username", "password") or die(mysql_error()) ;
mysql_select_db("myemployees") or die(mysql_error()) ;

//Writes the information to the database
mysql_query("INSERT INTO `employees` VALUES ('$id', '$name', '$email', '$phone', '$pic')");

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

johnlm
New php-forum User
New php-forum User
Posts: 8
Joined: Sat Jan 29, 2011 10:22 am
Location: Byron, Wyoming
Contact:

Re: Uploading multiple files to server writing detail to MyS

Postby johnlm » Sat Jan 29, 2011 11:46 am

Ok, I changed some of your code to match a project I have been working on myself. If you look at the code, you'll see it will work for your project as well. Here's what I came up with that works for me.

html form
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="generator" content="CoffeeCup HTML Editor (www.coffeecup.com)">
<meta name="created" content="Sat, 29 Jan 2011 18:37:45 GMT">
<meta name="description" content="">
<meta name="keywords" content="">
<title></title>

<!--[if IE]>
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
</head>
<body>
<form enctype="multipart/form-data" action="photo_store.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>
<input type="submit" value="Add">
</form>


</body>
</html>



photo_store.php file

<?php

//This is the directory where images will be saved
$target = "images/";
$target = $target . basename( $_FILES['photo']['name']);
$target1 = $target . basename( $_FILES['photo1']['name']);
$target2 = $target . basename( $_FILES['photo2']['name']);

//This gets all the other information from the form
$id=
$name=$_POST['name'];
$email=$_POST['email'];
$phone=$_POST['phone'];
$photo=($_FILES['photo']['name']);
$photo1=($_FILES['photo1']['name']);
$photo2=($_FILES['photo2']['name']);

// Connects to your Database

$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("my_db") or die(mysql_error()) ;

if ($con) echo "Connection Made!!<br>";

//Writes the information to the database

$sql="INSERT INTO persons (name, email, phone, photo, photo1, photo2)
VALUES
('$name','$email','$phone','$photo','$photo1','$photo2')";

if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}

//Writes the photo to the server
if(move_uploaded_file($_FILES['photo']['tmp_name'], $target));
(move_uploaded_file($_FILES['photo1']['tmp_name'], $target1));
(move_uploaded_file($_FILES['photo2']['tmp_name'], $target2))


//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";

?>


I hope this helps.


Return to “PHP coding => Images”

Who is online

Users browsing this forum: No registered users and 1 guest