Jump to content

if statement for importing into mysql database.


davidcriniti

Recommended Posts

Hi everyone,

 

I am currently making a page for a friend to upload a bunch of photos at a time.

 

I was quite pleased that after a bit of googling and trial and error, I figured out how to do this so that multiple records could be added to my table with one submit button.

 

However, my form has 10 browse iconcs. A few tests have revealed that my problem is that if only one picture is uploaded, I still get 9 entries in my database, which I don't want.

 

My question is how can I alter the code so that a row is only populated in the database if an image is uploaded.

 

I guess something that sort of says :

 

if($imgx!="")

{

populate that row in the table

}

 

else

 

{

don't

}

 

 

...and the same for $imgx002 through to $imgx010

 

 

The current query is below. Any pointers are much appreciated.

 



$query = "INSERT INTO photo_uploads (date, photo_name)" . "VALUES (NOW(), '$imgx'), (NOW(), '$imgx002'), (NOW(), '$imgx003'), (NOW(), '$imgx004'), (NOW(), '$imgx005'), (NOW(), '$imgx006'), (NOW(), '$imgx007'), (NOW(), '$imgx008'), (NOW(), '$imgx009'), (NOW(), '$imgx010')";

If you name the form file fields such as image1 through to image 10 then it is easy. You need to put your upload functions within the loop, but I guess if you are uploading 10 images in 1 form there is already a loop in your script. You should be able to understand this code:

 

<?php
// loop from 1 to 10 and upload each image
for($x = 1; $x <= 10; $x++) {
	// if file field has been completed
	if(strlen($_FILES['image'.$x]['name'])) {	
		// code here to upload file
		// ..........
		// this needs completing
		// $upload = true / false

		// test if file upload was successful
		if($upload == true) {
                                // add to database
			$result = mysql_query("INSERT INTO photo_uploads SET date=NOW(), photo_name='".mysql_real_escape_string($_FILES['image'.$x]['name'])."'");
		}
	}
}
?>

Archived

This topic is now archived and is 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.