lisa_85 Posted June 1, 2009 Share Posted June 1, 2009 Hi, posting loads of questions today I have linked a script I found online to my database. The script is for uploading multiple files which it does fine. The problem is that I want a new row in my DB table tblfiles for each file uploaded. When I upload, it only inserts a new row for the first file. How do I get it to run through a loop to make an entry for each file. This is my code. function upload(){ foreach($_POST as $key=>$value) { $$key = $value; } mysql_query("INSERT INTO tblpage (uID, ftitle, fdesc) VALUES ('$uID', '$ftitle', '$fdesc')"); $lastinsert = mysql_insert_id(); if(count($_FILES["item_file"]['name'])>0) { //check if any file uploaded $GLOBALS['msg'] = ""; //initiate the global message for($j=0; $j < count($_FILES["item_file"]['name']); $j++) { //loop the uploaded file array $filen = time().'_'.$_FILES["item_file"]['name']["$j"]; //file name $path = 'uploads/'.$filen; //generate the destination path mysql_query("INSERT INTO tblfiles (fID, fname) VALUES ('$lastinsert', '$filen')"); if(move_uploaded_file($_FILES["item_file"]['tmp_name']["$j"],$path)) { //upload the file $GLOBALS['msg'] .= "File# ".($j+1)." ($filen) uploaded successfully<br>"; //Success message } } } else { $GLOBALS['msg'] = "No files found to upload"; //Failed message } uploadForm(); //display the main form } Thanks Quote Link to comment Share on other sites More sharing options...
anupamsaha Posted June 1, 2009 Share Posted June 1, 2009 Whats your html form look like for the file field? Quote Link to comment Share on other sites More sharing options...
lisa_85 Posted June 1, 2009 Author Share Posted June 1, 2009 Hi, I guess the easiest thing is to post the code for the whole page <?php // DB Connect $dbHost = ""; $dbUser = ""; $dbPass = ""; $dbname = ""; $db = mysql_connect($dbHost,$dbUser,$dbPass); mysql_select_db($dbname,$db); if($_POST['pgaction']=="upload") upload(); else uploadForm(); //The form having dynamic file uploader function uploadForm() { ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" > <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" > <head> <title>File Upload</title> <meta http-equiv="Content-Style-Type" content="text/css" /> <meta http-equiv="content-type" content="text/html;charset=utf-8" /> <link rel="stylesheet" href="common/style.css" type="text/css" /> </head> <body> <div id="pagewidth"> <div id="header"></div> <div class="clearfix"> <div id="maincol"> <div id="stylized" class="myform"> <form name="frm" method="post" onsubmit="return validate(this);" enctype="multipart/form-data"> <input type="hidden" name="pgaction" /> <input type="hidden" name="uID" value="1" /> <?php if ($GLOBALS['msg']) { echo '<div class="err">'.$GLOBALS['msg'].'</div>'; }?> <h1>File Upload Form</h1> <p>Upload files</p> <label>Title <span class="small">Add page title</span> </label> <input type="text" name="ftitle" /> <label>Description <span class="small">Add a short description</span> </label> <textarea name="fdesc"></textarea> <label>Upload File <span class="small">Choose a file</span> </label> <div id="listStart"> <input type="file" name="item_file[]" /><a href="javascript:_add_more();" title="Add more"><img src="plus_icon.gif" border="0" /></a> <div id="listEnd" style="padding-left: 140px;"><input type="submit" value="Upload File(s)" /></div> </div> </form> </div> <script language="javascript"> <!-- function _add_more(){ var newRow = document.createElement('<div>'); var txt = "<div style=\"padding-left:140px\"><input type=\"file\"name=\"item_file[]\"></div>"; newRow.innerHTML= txt; var theDad = document.getElementById("listStart"); var theEnd= document.getElementById("listEnd"); theDad.insertBefore(newRow, theEnd); } function validate(f){ var chkFlg = false; for(var i=0; i < f.length; i++) { if(f.elements[i].type=="file" && f.elements[i].value != "") { chkFlg = true; } } if(!chkFlg) { alert('Please browse/choose at least one file'); return false; } f.pgaction.value='upload'; return true; } //--> </script> </div> <div id="leftcol">Home<p />Logout<p /></div> </div> <div id="footer">Copyright © <?php echo date('Y'); ?></div> </div> </body> </html> <?php } //function to store uploaded file function upload(){ foreach($_POST as $key=>$value) { $$key = $value; } mysql_query("INSERT INTO tblpage (uID, ftitle, fdesc) VALUES ('$uID', '$ftitle', '$fdesc')"); $lastinsert = mysql_insert_id(); if(count($_FILES["item_file"]['name'])>0) { //check if any file uploaded $GLOBALS['msg'] = ""; //initiate the global message for($j=0; $j < count($_FILES["item_file"]['name']); $j++) { //loop the uploaded file array $filen = time().'_'.$_FILES["item_file"]['name']["$j"]; //file name $path = 'uploads/'.$filen; //generate the destination path mysql_query("INSERT INTO tblfiles (fID, fname) VALUES ('$lastinsert', '$filen')"); if(move_uploaded_file($_FILES["item_file"]['tmp_name']["$j"],$path)) { //upload the file $GLOBALS['msg'] .= "File# ".($j+1)." ($filen) uploaded successfully<br>"; //Success message } } } else { $GLOBALS['msg'] = "No files found to upload"; //Failed message } uploadForm(); //display the main form } ?> It uploads all the files as it should but just doesn't make all the entries in the DB table tblfiles. Thanks Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted June 1, 2009 Share Posted June 1, 2009 This way is much easier and it does thesame thing. <?php function upload(){ extract($_POST); }?> Quote Link to comment Share on other sites More sharing options...
lisa_85 Posted June 2, 2009 Author Share Posted June 2, 2009 Hi, does anyone have any idea how to solve this problem? I have been trying but am stuck Thanks Quote Link to comment Share on other sites More sharing options...
anupamsaha Posted June 2, 2009 Share Posted June 2, 2009 Hi, does anyone have any idea how to solve this problem? I have been trying but am stuck Thanks Please change: $filen = time().'_'.$_FILES["item_file"]['name']["$j"]; //file name To: $filen = time().'_'.$_FILES["item_file"]['name'][$j]; //file name And all existence of "$j" in the script. Hope this will help. Quote Link to comment Share on other sites More sharing options...
lisa_85 Posted June 2, 2009 Author Share Posted June 2, 2009 Hi, I changed that but it still only runs the query mysql_query("INSERT INTO tblfiles (fID, fname) VALUES ('$lastinsert', '$filen')"); once and enters a row for the first file Quote Link to comment Share on other sites More sharing options...
Alt_F4 Posted June 2, 2009 Share Posted June 2, 2009 try this and post the result foreach($_POST as $key=>$value) { $$key = $value; } mysql_query("INSERT INTO tblpage (uID, ftitle, fdesc) VALUES ('$uID', '$ftitle', '$fdesc')"); $lastinsert = mysql_insert_id(); echo count($_FILES["item_file"]['name']); /*if(count($_FILES["item_file"]['name'])>0) { //check if any file uploaded $GLOBALS['msg'] = ""; //initiate the global message for($j=0; $j < count($_FILES["item_file"]['name']); $j++) { //loop the uploaded file array $filen = time().'_'.$_FILES["item_file"]['name']["$j"]; //file name $path = 'uploads/'.$filen; //generate the destination path mysql_query("INSERT INTO tblfiles (fID, fname) VALUES ('$lastinsert', '$filen')"); if(move_uploaded_file($_FILES["item_file"]['tmp_name']["$j"],$path)) { //upload the file $GLOBALS['msg'] .= "File# ".($j+1)." ($filen) uploaded successfully<br>"; //Success message } } } else { $GLOBALS['msg'] = "No files found to upload"; //Failed message } uploadForm(); //display the main form*/ Quote Link to comment Share on other sites More sharing options...
lisa_85 Posted June 2, 2009 Author Share Posted June 2, 2009 Hi, I ran that code and added 2 files. The number returned was 2 which is correct. Like I said, I can get it to upload the files to the directory but to store the filename in the DB is my problem. Thanks Quote Link to comment Share on other sites More sharing options...
Alt_F4 Posted June 2, 2009 Share Posted June 2, 2009 ok cool, just ruling things out. uncomment the stuff i commented out /*if(count($_FILES["item_file"]['name'])>0) { //check if any file uploaded $GLOBALS['msg'] = ""; //initiate the global message for($j=0; $j < count($_FILES["item_file"]['name']); $j++) { //loop the uploaded file array $filen = time().'_'.$_FILES["item_file"]['name']["$j"]; //file name $path = 'uploads/'.$filen; //generate the destination path mysql_query("INSERT INTO tblfiles (fID, fname) VALUES ('$lastinsert', '$filen')"); if(move_uploaded_file($_FILES["item_file"]['tmp_name']["$j"],$path)) { //upload the file $GLOBALS['msg'] .= "File# ".($j+1)." ($filen) uploaded successfully<br>"; //Success message } } } else { $GLOBALS['msg'] = "No files found to upload"; //Failed message } uploadForm(); //display the main form*/ and change this line mysql_query("INSERT INTO tblfiles (fID, fname) VALUES ('$lastinsert', '$filen')"); to this and see if you get any errors mysql_query("INSERT INTO tblfiles (fID, fname) VALUES ('$lastinsert', '$filen')") or die("error: ".mysql_error()); Quote Link to comment Share on other sites More sharing options...
lisa_85 Posted June 10, 2009 Author Share Posted June 10, 2009 Hiya, sorry I have not responded to this for a while I have been on holiday I did what you said and I get this: "error: Duplicate entry '29' for key 1." Also I noticed that it only uploads 1 file when the error checking is on. Do you know what is wrong? Thanks Quote Link to comment Share on other sites More sharing options...
lisa_85 Posted June 10, 2009 Author Share Posted June 10, 2009 Ooooh, I am so thick lol Just realised what the problem was. It is because the field in my table should be able to have duplicate values but I must have set it as a primary key by mistake. I added a dummy primary key field and now it is working woohoo Thanks for all your help guys Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.