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')";

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.