Jump to content

Recommended Posts

Hi. I want to create a form that allows for 6 images to be uploaded with PHP and the paths saved to a MySQL DB.

I have made a script that does this with only one image, bu I do not know how to do it with multiple images.

 

Here is my script:

 

$uploadDir = '../uploads/';

if(isset($_POST['upload']))
{
$fileName = $_FILES['userfile']['name'];
$tmpName = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];

if($fileName==""){

$filePath = '../img/none.jpg';
}
else{

$filePath = $uploadDir . $fileName;
}

$filePath = str_replace(" ", "_", $filePath);
$result = move_uploaded_file($tmpName, $filePath);

if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
$filePath = addslashes($filePath);
} 
$empresa = $_POST["E_nombre"];
$direccion = $_POST["E_direccion"];
$cp = $_POST["E_cp"];
$poblacion = $_POST["E_poblacion"];
$tel = $_POST["E_telefono"];
$fax = $_POST["E_fax"];
$email = $_POST["E_mail"];
$web = $_POST["E_web"];
$descripcionesp = $_POST["E_descripcion_esp"];
$descripcioneng = $_POST["E_descripcion_eng"];
$descripcionger = $_POST["E_descripcion_ger"];
$descripcionfra = $_POST["E_descripcion_fra"];

$query = "INSERT INTO amat.t_empresas (E_nombre, E_direccion, E_cp, E_poblacion, E_telefono, E_fax, E_mail, E_web, E_descripcion_esp, E_descripcion_eng, E_descripcion_ger, E_descripcion_fra, E_logo) ".
"VALUES ('$empresa','$direccion','$cp','$poblacion','$tel','$fax','$email', '$web', '$descripcionesp', '$descripcioneng', '$descripcionger', '$descripcionfra','$filePath')";

mysql_query($query) or die('Error, query failed : ' . mysql_error()); 

 

Sorry but I am a PHP Mysql beginner, so any patient help will be greatly appreciated.

Thanks

First of all, it looks like in your database, you only have one field for one image.  So either that field would contain a string of all 6 image paths, or you would create 6 fields for each row, or you could create another table just to store the image paths.

 

If you will always have 6 or less files to upload.  In your form you would have 6 inputs with different names instead of only user.  So the form would have:

<input type="file" name="image1"  /> 
<input type="file" name="image2"  /> 
//etc.

 

If you're going to put all your file paths in one field, your php file would be:

 


$filename .= $_FILES['image01']['name'];
$filename .= $_FILES['image02']['name'];
//etc.

 

I guess it just depends on how you want to store your data in the db.  I'm a novice too, so hopefully I didn't steer you in the wrong directions.

 

 

I like the separate image table  you would want to construct it so you have :

picID autoincrement integer primary key

userID integer    --links to the original table

location  VARCHAR(256)  path to image and filename

 

You should construct the table so that you can just output the location into an <img src="$location">

 

You will put one row into this table for every picture.

 

You are going to want to query this table using

 

SELECT location FROM pictures WHERE userID=$id

 

You are going to want to put the values into this table using

 

$id=mysql_insert_id();
$insert="INSERT INTO pictures (userID, location) VALUES ";
foreach $_FILES as $file
{
$fileName = $file['name'];
$tmpName = $file['tmp_name'];
$fileSize = $file['size'];
$fileType = $file['type'];

if($fileName==""){

$filePath = '../img/none.jpg';
}
else{

$filePath = $uploadDir . $fileName;
}

$filePath = str_replace(" ", "_", $filePath);
$result = move_uploaded_file($tmpName, $filePath);

if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
$filePath = addslashes($filePath);
}

$insert .= "($id , $filePath),";
}
$insert=trim($insert, ",");

mysql_query($insert);

 

 

*This is totally untested and not even read all the way through.

 

Thanks all for your help. I have decided to create 7 image fields and 7 different colums on the db.  I have done this because it is easier for me as I'm very new.

 

My form looks like this:

 

<input name="logo" type="file" id="logo">
<input name="image01" type="file" id="image01" />
<input name="image02" type="file" id="image02" />
<input name="image03" type="file" id="image03" />
<input name="image04" type="file" id="image04" />
<input name="image05" type="file" id="image05" />
<input name="image06" type="file" id="image06" />

 

I would like to know how to upload them now. Please help me to modify this:

 

$uploadDir = '../uploads/';

if(isset($_POST['upload']))
{
$fileName = $_FILES['userfile']['name'];
$tmpName = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];

if($fileName==""){

$filePath = '../img/none.jpg';
}
else{

$filePath = $uploadDir . $fileName;
}


$filePath = str_replace(" ", "_", $filePath);
$result = move_uploaded_file($tmpName, $filePath);


if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
$filePath = addslashes($filePath);
} 
$empresa = $_POST["E_nombre"];
$direccion = $_POST["E_direccion"];
$cp = $_POST["E_cp"];
$poblacion = $_POST["E_poblacion"];
$tel = $_POST["E_telefono"];
$fax = $_POST["E_fax"];
$email = $_POST["E_mail"];
$web = $_POST["E_web"];
$descripcionesp = $_POST["E_descripcion_esp"];
$descripcioneng = $_POST["E_descripcion_eng"];
$descripcionger = $_POST["E_descripcion_ger"];
$descripcionfra = $_POST["E_descripcion_fra"];

$query = "INSERT INTO amat.t_empresas (E_nombre, E_direccion, E_cp, E_poblacion, E_telefono, E_fax, E_mail, E_web, E_descripcion_esp, E_descripcion_eng, E_descripcion_ger, E_descripcion_fra, E_logo) ".
"VALUES ('$empresa','$direccion','$cp','$poblacion','$tel','$fax','$email', '$web', '$descripcionesp', '$descripcioneng', '$descripcionger', '$descripcionfra','$filePath')";

mysql_query($query) or die('Error, query failed : ' . mysql_error()); 


$uploadDir = '../uploads/';

if(isset($_POST['upload']))
{
foreach $_FILES as $file
{
$fileName = $file['name'];
$tmpName = $file['tmp_name'];
$fileSize = $file['size'];
$fileType = $file['type'];

if($fileName==""){

$filePath = '../img/none.jpg';
}
else{

$filePath = $uploadDir . $fileName;
}

$filePath = str_replace(" ", "_", $filePath);
$result = move_uploaded_file($tmpName, $filePath);

if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
$filePath = addslashes($filePath);
}
$fileinsert[]=$filePath;
}

$empresa = $_POST["E_nombre"];
$direccion = $_POST["E_direccion"];
$cp = $_POST["E_cp"];
$poblacion = $_POST["E_poblacion"];
$tel = $_POST["E_telefono"];
$fax = $_POST["E_fax"];
$email = $_POST["E_mail"];
$web = $_POST["E_web"];
$descripcionesp = $_POST["E_descripcion_esp"];
$descripcioneng = $_POST["E_descripcion_eng"];
$descripcionger = $_POST["E_descripcion_ger"];
$descripcionfra = $_POST["E_descripcion_fra"];

$query = "INSERT INTO amat.t_empresas (E_nombre, E_direccion, E_cp, E_poblacion, E_telefono, E_fax, E_mail, E_web, E_descripcion_esp, E_descripcion_eng, E_descripcion_ger, E_descripcion_fra, E_logo, E_image01, E_image02, E_image03, E_image04, E_image05, E_image06) ".
"VALUES ('$empresa','$direccion','$cp','$poblacion','$tel','$fax','$email', '$web', '$descripcionesp', '$descripcioneng', '$descripcionger', '$descripcionfra','$fileinsert[0]','$fileinsert[1]','$fileinsert[2]','$fileinsert[3]','$fileinsert[4]','$fileinsert[5]','$fileinsert[6]')";

mysql_query($query) or die('Error, query failed : ' . mysql_error()); 

 

If it doesn't work I'm not going to spend too much effort troubleshooting it becuase it is the wrong way to go about it.  This is like teaching your kid to piss in an electrical socket.

Thanks for your help. I am now uploading the images with no problem.

At the moment I am displaying them on my PHP page like his:

 

uploads/<?php echo $row_detalle_asociados_RS['E_image01']; ?>
uploads/<?php echo $row_detalle_asociados_RS['E_image02']; ?>

etc...

 

This is ok, but the problem is that some records have less than 6 images so with this method I will have to display blank images where there is no image uploaded.

 

How cain I display only the uploaded images for each record?. Can I display them in a loop like this?

uploads/<?php echo $row_detalle_asociados_RS['E_image[?]']; ?>

 

 

Ahh.... I see that you have figured out why it is better to use a table to store your pictures, then you query the table for userid=$id and only get back the pictures that exist, c'est la vie.  You are going to have to find a way to set a NULL value to the column that has no images.  Then when you return you have to put a condition around the entire <img tag

 

if ($row_detalle_asociados_RS['E_image02']){
echo "<img src=\"uploads/". $row_detalle_asociados_RS['E_image02']."\">";

 

And as for looping, I think you can do it with:

 

for ($i=0; $i<7; $i++){
echo $row_detalle_asociados_RS['E_image0'.$i];
}

if not, you'll need to set a temporary variable as:

$index="E_image0".$i;
echo $row_detalle_asociados_RS[$index];

 

But I'm pretty sure the first one works.

 

Good catch-- I sure did miss a set of parenthesis.  Kinda glad that it was something easy to spot!

 

 

Thanks again for your help.

I have a problem updating images now.

This is the code I use for updating a record:

$imgActual = $row_asociados_update_RS['E_logo']; // this mantains the previously uploaded image if this is not updated
$uploadDir = '../uploads/';

if(isset($_POST['editar']))
{
foreach ($_FILES as $file)
{
$fileName = $file['name'];
$tmpName = $file['tmp_name'];
$fileSize = $file['size'];
$fileType = $file['type'];

if($fileName==""){  // this prevents existing uploaded image from being deleted when other fields are updated

$filePath = $imgActual;
}
else{

$filePath = $uploadDir . $fileName;
}

$filePath = str_replace(" ", "_", $filePath);
$result = move_uploaded_file($tmpName, $filePath);

if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
$filePath = addslashes($filePath);
}
$fileinsert[]=$filePath;
}

$empresa = $_POST["E_nombre"];
$direccion = $_POST["E_direccion"];
$cp = $_POST["E_cp"];
$poblacion = $_POST["E_poblacion"];
$tel = $_POST["E_telefono"];
$fax = $_POST["E_fax"];
$email = $_POST["E_mail"];
$web = $_POST["E_web"];
$descripcionesp = $_POST["E_descripcion_esp"];
$descripcioneng = $_POST["E_descripcion_eng"];
$descripcionger = $_POST["E_descripcion_ger"];
$descripcionfra = $_POST["E_descripcion_fra"];

$Sql="UPDATE t_empresas SET E_nombre='$empresa', E_direccion='$direccion', E_cp='$cp', E_poblacion='$poblacion', E_telefono='$tel', E_fax='$fax', E_mail='$email', E_web='$web', E_descripcion_esp='$descripcionesp', E_descripcion_eng='$descripcioneng', E_descripcion_ger='$descripcionger', E_descripcion_fra='$descripcionfra', E_logo='$fileinsert[0]', E_image01='$fileinsert[1]', E_image02='$fileinsert[2]', E_image03='$fileinsert[3]', E_image04='$fileinsert[4]', E_image05='$fileinsert[5]', E_image06='$fileinsert[6]' WHERE id_E='$colname_asociados_update_RS'";

mysql_query($Sql) or die('Error, query failed : ' . mysql_error()); 


 

The above code worked fine when  had only 1 image, but now all images get deleted everytime I update ay of them.

 

 

 

$imgActual = $row_asociados_update_RS['E_logo']; // this mantains the previously uploaded image if this is not updated

$uploadDir = '../uploads/';

 

if(isset($_POST['editar']))

{

foreach ($_FILES as $file)

{

$fileName = $file['name'];

$tmpName = $file['tmp_name'];

$fileSize = $file['size'];

$fileType = $file['type'];

 

if($fileName==""){  // this prevents existing uploaded image from being deleted when other fields are updated

 

$filePath = $imgActual;

 

}

else{

 

$filePath = $uploadDir . $fileName;

}

 

replace with:

$imgActual[0] = $row_asociados_update_RS['E_logo']; // this mantains the previously uploaded image if this is not 
updated
$imgActual[1]= $row_asociados_update_RS['E_Image01'];
$imgActual[2]= $row_asociados_update_RS['E_Image02'];
$imgActual[3]= $row_asociados_update_RS['E_Image03'];
$imgActual[4]= $row_asociados_update_RS['E_Image04'];
$imgActual[5]= $row_asociados_update_RS['E_Image05'];
$imgActual[6]= $row_asociados_update_RS['E_Image06'];
$uploadDir = '../uploads/';
$i=0;
if(isset($_POST['editar']))
{
foreach ($_FILES as $file)
{
$fileName = $file['name'];
$tmpName = $file['tmp_name'];
$fileSize = $file['size'];
$fileType = $file['type'];

if($fileName==""){  // this prevents existing uploaded image from being deleted when other fields are updated

$filePath = $imgActual[$i];
}
else{

$filePath = $uploadDir . $fileName;
}
$i++;

 

*totally untested*

Many thanks again, however I get the following error:

 

Parse error: syntax error, unexpected $end in /usr/home/mallorcaattraction.com/web/admin/PC_asociados_update.php on line 402

 

I have read this is caused by a missing bracket or parenthesis, unfortunately not easy to find for me this time

I think the problem is the fact that you aren't posting any files with the update.  You should rewrite this page so it checks each file in the $_FILES[] to see if it exists and then execute move_uploaded_file and concat a variable that goes on the end of the query.  That way you won't update the query unless there was a change and won't move/upload files unless there was a change.  I will look at this again, but I am about to go to work, and when I get home I'm going to spend the night with my wife, so I may not look into this again until tomorrow morning (appx 21hours from now).  Keep me updated, because otherwise I will rewrite this function for you.  Man, it would have been so much easier to use a separate table.

Many thanks for your help. I'm sorry about all the trouble but I am still learning and some things look really difficult at this stage.

 

I think I better spend some time with my wife also.

 

Thanks again. I'll wait for you to come back as I'm really stuck now.

Try this, I set up an array that maps the "html_element" => "SQL_element"  But I never used the SQL_element, so I am suspicious that it won't work.  Let me know, please. Oh, and you are going to have to rename it back to the original name.

 

[attachment deleted by admin]

it works!!!!...Thankyou very very much for your time and effort.

 

I am now trying to do a multi user login script. I have managed to do one that logs in all the users on my DB, but my problem is that I need every user to access only his data in order to update it.

 

At the moment all the users can access all info and that's not good.

 

Could you point me on the right direction to a tutorial or something?

  • 2 years later...

$uploadDir = '../uploads/';

if(isset($_POST['upload']))
{
foreach $_FILES as $file
{
$fileName = $file['name'];
$tmpName = $file['tmp_name'];
$fileSize = $file['size'];
$fileType = $file['type'];

if($fileName==""){

$filePath = '../img/none.jpg';
}
else{

$filePath = $uploadDir . $fileName;
}

$filePath = str_replace(" ", "_", $filePath);
$result = move_uploaded_file($tmpName, $filePath);

if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
$filePath = addslashes($filePath);
}
$fileinsert[]=$filePath;
}

$empresa = $_POST["E_nombre"];
$direccion = $_POST["E_direccion"];
$cp = $_POST["E_cp"];
$poblacion = $_POST["E_poblacion"];
$tel = $_POST["E_telefono"];
$fax = $_POST["E_fax"];
$email = $_POST["E_mail"];
$web = $_POST["E_web"];
$descripcionesp = $_POST["E_descripcion_esp"];
$descripcioneng = $_POST["E_descripcion_eng"];
$descripcionger = $_POST["E_descripcion_ger"];
$descripcionfra = $_POST["E_descripcion_fra"];

$query = "INSERT INTO amat.t_empresas (E_nombre, E_direccion, E_cp, E_poblacion, E_telefono, E_fax, E_mail, E_web, E_descripcion_esp, E_descripcion_eng, E_descripcion_ger, E_descripcion_fra, E_logo, E_image01, E_image02, E_image03, E_image04, E_image05, E_image06) ".
"VALUES ('$empresa','$direccion','$cp','$poblacion','$tel','$fax','$email', '$web', '$descripcionesp', '$descripcioneng', '$descripcionger', '$descripcionfra','$fileinsert[0]','$fileinsert[1]','$fileinsert[2]','$fileinsert[3]','$fileinsert[4]','$fileinsert[5]','$fileinsert[6]')";

mysql_query($query) or die('Error, query failed : ' . mysql_error()); 

 

If it doesn't work I'm not going to spend too much effort troubleshooting it becuase it is the wrong way to go about it.  This is like teaching your kid to piss in an electrical socket.

 

hi andrew, im new to php too. to my understanding, in the "VALUES ('$empresa','$direccion','$cp','$poblacion','$tel','$fax','$email', '$web', '$descripcionesp', '$descripcioneng', '$descripcionger', '$descripcionfra','$fileinsert[0]','$fileinsert[1]','$fileinsert[2]','$fileinsert[3]','$fileinsert[4]','$fileinsert[5]','$fileinsert[6]')";

 

i din see you hv insert the path of the image into the database. did we hv to insert the path or just leave it as it??

 

im doing a similar multiple image uploading in my php too. kindly guide me through..

 

tq :-[

Guest
This topic is now closed to further replies.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.