Inserting multiple upload file info to db

I have an upload script here that will let me upload 5 images and the info posted into a database. only problem is, i dont know how to make it so it will loop and insert the info for all into a database.

If i set it to 1 upload… it works perfect.

i get the feeling its something like… insert into db SET imgurl="$imgurl[$i]"

if so i just dont know exactly how to write that out so it works correctly…

if not. then im more lost than i could imagine?

[php]<?php
error_reporting(7);

// Max size PER file in KB, not bytes for simplicity!
$max_file_size=“1024”;

// Max size for all files COMBINED in KB, not bytes for simplicity!
$max_combined_size=“2048”;

//How many file uploads do you want to allow at a time?
$file_uploads=“1”;

//The name of the uploader…
$websitename=“Contact Box Image Uploader”;

// Use random file names? true=yes (recommended), false=use original file name. Random names will help prevent overwritting of existing files!
$random_name=true;

// Please keep the array structure.
$allow_types=array(“jpg”,“gif”,“png”);

// Path to files folder. If this fails use $fullpath below. With trailing slash
$folder="./uploads/ctboxes/";

// Full url to where files are stored. With Trailing Slash
$full_url=“http://www.er0x.byethost6.com/test/uploads/ctboxes/”;

// Only use this variable if you wish to use full server paths. Otherwise leave this empty! With trailing slash
$fullpath="";

//Use this only if you want to password protect your uploads.
$password="";

/*
//================================================================================

  • ! ATTENTION !
    //================================================================================
    : Don’t edit below this line unless you know some php. Editing some variables or other stuff could cause undeseriable results!!
    */

// MD5 the password… why not?
$password_md5=md5($password);

// If you set a password this is how they get verified!
If($password) {
If($_POST[‘verify_password’]==true) {
If(md5($_POST[‘check_password’])==$password_md5) {
setcookie(“phUploader”,$password_md5,time()+86400);
sleep(1); //seems to help some people.
header(“Location: http://”.$_SERVER[‘HTTP_HOST’].$_SERVER[‘PHP_SELF’]);
exit;

	}
}

}

// The password form, if you set a password and the user has not entered it this will show.
$password_form="";
If($password) {
If($_COOKIE[‘phUploader’]!=$password_md5) {
$password_form="<form method=“POST” action="".$_SERVER[‘PHP_SELF’]."">n";
$password_form.=“

n”;
$password_form.=“n”;
$password_form.=“n”;
$password_form.=“n”;
$password_form.=“n”;
$password_form.=“n”;
$password_form.=“n”;
$password_form.=“n”;
$password_form.=“n”;
$password_form.=“n”;
$password_form.=“
Password Required
Enter Password:
n”;
$password_form.=“n”;
$password_form.=“n”;
$password_form.=“
n”;
$password_form.=“n”;
}
}

// Function to get the extension a file.
function get_ext($key) {
$key=strtolower(substr(strrchr($key, “.”), 1));
// Cause there the same right?
$key=str_replace(“jpeg”,“jpg”,$key);
return $key;
}

$ext_count=count($allow_types);
$i=0;
foreach($allow_types AS $extension) {

//Gets rid of the last comma for display purpose..

If($i <= $ext_count-2) {
	$types .="*.".$extension.", ";
} Else {
	$types .="*.".$extension;
}
$i++;

}
unset($i,$ext_count); // why not

$error="";
$display_message="";
$uploaded==false;

// Dont allow post if $password_form has been populated
If($_POST[‘submit’]==true AND !$password_form) {

For($i=0; $i <= $file_uploads-1; $i++) {
				
	If($_FILES['file']['name'][$i]) {
					
		$ext=get_ext($_FILES['file']['name'][$i]);
		$size=$_FILES['file']['size'][$i];
		$max_bytes=$max_file_size*1024;
		
		// For random names
		If($random_name){
			$file_name[$i]=time()+rand(0,100000).".".$ext;
		} Else {
			$file_name[$i]=$_FILES['file']['name'][$i];
		}
		
		//Check if the file type uploaded is a valid file type. 
					
		If(!in_array($ext, $allow_types)) {
						
			$error.= "Invalid extension for your file: ".$_FILES['file']['name'][$i].", only ".$types." are allowed.<br />Your file(s) were <b>not</b> uploaded.<br />";
						
			//Check the size of each file
						
		} Elseif($size > $max_bytes) {
			
			$error.= "Your file: ".$_FILES['file']['name'][$i]." is to big. Max file size is ".$max_file_size."kb.<br />Your file(s) were <b>not</b> uploaded.<br />";
			
			// Check if the file already exists on the server..
		} Elseif(file_exists($folder.$file_name[$i])) {
			
			$error.= "The file: ".$_FILES['file']['name'][$i]." exists on this server, please try again.<br />Your file(s) were <b>not</b> uploaded.<br />";
			
		}
					
	} // If Files

} // For

//Tally the size of all the files uploaded, check if it's over the ammount.
			
$total_size=array_sum($_FILES['file']['size']);
  			
$max_combined_bytes=$max_combined_size*1024;
			
If($total_size > $max_combined_bytes) {
	$error.="The max size allowed for all your files combined is ".$max_combined_size."kb<br />";
}
	

// If there was an error take notes here!

If($error) {
	
	$display_message=$error;
	
} Else {
	
	// No errors so lets do some uploading!
	
	For($i=0; $i <= $file_uploads-1; $i++) {
			
		If($_FILES['file']['name'][$i]) {
			
			If(@move_uploaded_file($_FILES['file']['tmp_name'][$i],$folder.$file_name[$i])) {
				$uploaded=true;

} Else {
$display_message.=“Couldn’t copy “.$file_name[$i].” to server, please make sure “.$folder.” is chmod 777 and the path is correct.n”;
}
}

	} //For
	
} // Else

} // $_POST AND !$password_form

/*
//================================================================================

  • Start the form layout
    //================================================================================
    :- Please know what your doing before editing below. Sorry for the stop and start php… people requested that I use only html for the form…
    */
    ?>
<? If($password_form) { Echo $password_form; } Elseif($uploaded==true) {?>

files uploaded

<? For($i=0; $i <= $file_uploads-1; $i++) { If($_FILES['file']['name'][$i]) { $file=$i+1; Echo("File #".$file.": ".$full_url.$file_name[$i]."

n"); $host = "xxxxxxxx"; $username = "xxxxxxxxx"; $password = "xxxxxxxxx"; $db = "xxxxxxxxxxx"; mysql_connect($host,$username,$password) or die ("error"); mysql_select_db($db) or die("error"); $imgurl = "$full_url$file_name[$i]"; $cat = $_POST['cat']; $kwords = $_POST['kwords']; $ctbname = $_POST['ctbname']; mysql_query("INSERT INTO pectboxes (id,ctbname,cat,imgurl,kwords) VALUES ('id','$ctbname','$cat','$imgurl','$kwords')") or die("error inserting into db"); } } ?>
Go Back
<?} Else {?> <?If($display_message){?>
<div align="center" class="error_message"><?=$display_message;?></div>
<br />
<?}?>
                    <b><a href="?page=ctboxes">View Contact Tables</a></b><br /><br />
                    <b>Allowed Types:</b> <?=$types?><br />
		<b>Max size per file:</b> <?=$max_file_size?>kb.<br />
		<b>All files will be reviewed before accepted!</b><br />

<?For($i=0;$i <= $file_uploads-1;$i++) {?>

html that works

<?}?>

                    <input type="hidden" name="submit" value="true" />
		<input type="submit" value=" Upload File(s) " /> &nbsp;
		<input type="reset" name="reset" value=" Reset Form " />


<?php} ?>[/php]

[size=99px]**Added [php] tags for better readability. [/size]

lot of code, can’t find the prob as i don’t know where to look at.

what is it doing/not doing? any errors?

well as it stands right now it works perfect…

when changing the amount of uploads to 5… it will upload the images correctly but not place the info in the database. I need some kind of loop to insert them but im not sure how to make it?

im guessing its a foreach $_POST or something along those lines

i hope this explains a bit more by what i mean?

the insert, is inside a lopp, isn’t it?

so why u wanna konw wherther it has to be inside a loop?

tell us what the loop, u posted, is doing!

[code]For($i=0; $i <= $file_uploads-1; $i++) {

If($_FILES['file']['name'][$i]) {
    $file=$i+1;
    
            Echo("<b>File #".$file.":</b> <a href="".$full_url.$file_name[$i]."" target="_blank">".$full_url.$file_name[$i]."</a><br /><br />n");

$host = “xxxxxxxx”;
$username = “xxxxxxxxx”;
$password = “xxxxxxxxx”;
$db = “xxxxxxxxxxx”;
mysql_connect($host,$username,$password) or die (“error”);
mysql_select_db($db) or die(“error”);

$imgurl = “$full_url$file_name[$i]”;
$cat = $_POST[‘cat’];
$kwords = $_POST[‘kwords’];
$ctbname = $_POST[‘ctbname’];

mysql_query(“INSERT INTO pectboxes (id,ctbname,cat,imgurl,kwords) VALUES (‘id’,’$ctbname’,’$cat’,’$imgurl’,’$kwords’)”) or die(“error inserting into db”);

}
} [/code]

the only loop is for the filename and showing the file name…

if i have $file_uploads=“1”; this works fine and will post to the database for the 1 upload.

if i have $file_uploads=“5”; it will upload the 5 images, show the url to each one but not insert my data into the database.

so do i need to make an additional loop for the data insertion or can i just use the same loop? im a bit lost on t his part.

If I understand what you want to do you will need to create a for loop with a query that inserts the appropriate data into the database.

If you are updating you are going to use something like:

$q=“UPDATE mytable SET myfield1 = ‘$myValue’, $myfield2 = ‘$myValue2’ WHERE myID = ‘$id’”;

Or if you are inserting something:

$q=“INSERT INTO mytable (field1, field2, field3) VALUES (’$myValue1’, ‘$myValue2’, ‘$myValue3’)”;

Hope that helps.

Thanks… that tip got me to thinking… it works great now… heres what i ended up doing…

changed part to this…

$imgurl = "$full_url$file_name[$i]"; $ctname = $_POST['ctbname']; $ctbname = $ctname[$i]; $caty = $_POST['cat']; $cat = $caty[$i]; $kwords = $kword[$i]; $kword = $_POST['kwords'];

and on my form… on the inputs… i had to add [] to it so its like this now… name=“cat[]”

Sponsor our Newsletter | Privacy Policy | Terms of Service