cdoggg94 Posted May 23, 2012 Share Posted May 23, 2012 Before I was having trouble uploading multiple files at once, and I have successfully done it now. Now my issue is putting the names of those files in the column I want in the data base. I think its something like this but i dont know how to display the correct name of the file. <?php set_time_limit(0); require_once("Connections/myConnect.php"); mysql_select_db("phpmy1_norfolkartscentre_ca"); $preName = $_POST['name']; $preText = $_POST['text']; $preLink = $_POST['links']; if(isset($_FILES['picture'])){ foreach($_FILES['picture']['tmp_name'] as $key => $tmp_name){ move_uploaded_file($tmp_name, "data/{$_FILES['picture']['name'][$key]}"); } //print_r($_FILES); } $querystring = "INSERT INTO presentation_tbl(pre_id,pre_name,pre_banner,pre_text,pre_link,pre_logo1,pre_logo2,pre_logo3) VALUES(NULL,'".$preName."','".$[0]."','".$preText."','".$preLink."','".$[1]."','".$[2]."','".$[3]."')"; $doquery = mysql_query($querystring); echo ""; } else{ echo "There was an error uploading the file, please try again!"; } The ".$[1]."','".$[2]."','".$[3] part is where i want to display the specific names of the array contents. I just dont know how to display it. Quote Link to comment https://forums.phpfreaks.com/topic/263000-inserting-arrays-into-database/ Share on other sites More sharing options...
Kays Posted May 23, 2012 Share Posted May 23, 2012 No. Insert them each into its own row in the table. You probably want to add a page ID so you know what page those images should be displayed in. And if pre_id is a PRIMARY_KEY, exclude it from the SQL. It will increment itself so you don't have to set it to NULL. Quote Link to comment https://forums.phpfreaks.com/topic/263000-inserting-arrays-into-database/#findComment-1347993 Share on other sites More sharing options...
mrMarcus Posted May 23, 2012 Share Posted May 23, 2012 Specific names of what array? The $_FILES array? Please be more specific as to what values you're looking to derive if that's the case. Also, and perhaps it's just because this is a work in progress, but that final ELSE statement will return an error: } else{ echo "There was an error uploading the file, please try again!"; } as there is no opening IF for it. Maybe you meant to place query within the: if(isset($_FILES['picture'])){ block? Then it should look like this: <?php set_time_limit(0); require_once("Connections/myConnect.php"); mysql_select_db("phpmy1_norfolkartscentre_ca"); $preName = $_POST['name']; $preText = $_POST['text']; $preLink = $_POST['links']; if(isset($_FILES['picture'])){ foreach($_FILES['picture']['tmp_name'] as $key => $tmp_name){ move_uploaded_file($tmp_name, "data/{$_FILES['picture']['name'][$key]}"); } //print_r($_FILES); $querystring = "INSERT INTO presentation_tbl(pre_id,pre_name,pre_banner,pre_text,pre_link,pre_logo1,pre_logo2,pre_logo3) VALUES(NULL,'".$preName."','".$[0]."','".$preText."','".$preLink."','".$[1]."','".$[2]."','".$[3]."')"; $doquery = mysql_query($querystring); echo ""; } else { echo "There was an error uploading the file, please try again!"; } Quote Link to comment https://forums.phpfreaks.com/topic/263000-inserting-arrays-into-database/#findComment-1347995 Share on other sites More sharing options...
cdoggg94 Posted May 23, 2012 Author Share Posted May 23, 2012 There are 4 file inputs in the form, one for banner, one for logo1, one for logo2, and the last for logo3. I would assume that since they are in that order, they would be : [0], [1], [2], [3]. i just don't know what to put in front of them in the INSERT query to make it display the correct content. As for if I was trying to put the query inside the $_FIELDS block, no i wasn't. I'm not 100% sure if it should go inside or out. I thought out was the correct place. Quote Link to comment https://forums.phpfreaks.com/topic/263000-inserting-arrays-into-database/#findComment-1348002 Share on other sites More sharing options...
mrMarcus Posted May 23, 2012 Share Posted May 23, 2012 There are 4 file inputs in the form, one for banner, one for logo1, one for logo2, and the last for logo3. I would assume that since they are in that order, they would be : [0], [1], [2], [3]. i just don't know what to put in front of them in the INSERT query to make it display the correct content. As for if I was trying to put the query inside the $_FIELDS block, no i wasn't. I'm not 100% sure if it should go inside or out. I thought out was the correct place. Well, it would make sense to have the query execute only if a file has been uploaded, correct? Having the INSERT query run without a file being uploaded wouldn't make any sense. You need to post the form code as well, please. Quote Link to comment https://forums.phpfreaks.com/topic/263000-inserting-arrays-into-database/#findComment-1348005 Share on other sites More sharing options...
cdoggg94 Posted May 23, 2012 Author Share Posted May 23, 2012 Very true... Here is what I have changed it to: <?php set_time_limit(0); require_once("Connections/myConnect.php"); mysql_select_db("phpmy1_norfolkartscentre_ca"); $preName = $_POST['name']; $preText = $_POST['text']; $preLink = $_POST['links']; if(isset($_FILES['picture'])){ foreach($_FILES['picture']['tmp_name'] as $key => $tmp_name){ move_uploaded_file($tmp_name, "data/{$_FILES['picture']['name'][$key]}"); } $querystring = "INSERT INTO presentation_tbl(pre_name,pre_banner,pre_text,pre_link,pre_logo1,pre_logo2,pre_logo3) VALUES(".$preName."','".$[0]."','".$preText."','".$preLink."','".$[1]."','".$[2]."','".$[3]."')"; $doquery = mysql_query($querystring); echo "Upload was a sucess"; }else{ echo "there was a problem uploading, please try again!"; } ?> and the form code is right here: <form action="admin_presentation2.php" method="post" enctype="multipart/form-data"> <table border="0"> <tr> <td align="right">Name: </td> <td colspan="2"><input name="name" type="text" /></td> </tr> <tr> <td align="right">Text: </td> <td colspan="2"><input name="text" type="text" /></td> </tr> <tr> <td align="right">Links: </td> <td colspan="2"><input name="links" type="text" /></td> </tr> <tr> <td align="right">Banner: </td> <td colspan="2"><input name="picture[]" type="file" /></td> <td>(800px X 152px)</td> </tr> <tr> <td align="right">Logo1: </td> <td colspan="2"><input name="picture[]" type="file" /></td> </tr> <tr> <td align="right">Logo2: </td> <td colspan="2"><input name="picture[]" type="file" /></td> </tr> <tr> <td align="right">Logo3: </td> <td colspan="2"><input name="picture[]" type="file" /></td> </tr> <tr> <td><input name="id" type="hidden" value="" /></td> <td colspan="2"><input name="submit" type="submit" value="Add!" /></td> </tr> </table> </form> Quote Link to comment https://forums.phpfreaks.com/topic/263000-inserting-arrays-into-database/#findComment-1348007 Share on other sites More sharing options...
mrMarcus Posted May 23, 2012 Share Posted May 23, 2012 It's quite simple, actually. And you had the concept within your foreach() loop by using the $key to differentiate between files: if (isset($_POST['submit'])) { if (isset($_FILES['picture'])) { foreach($_FILES['picture']['tmp_name'] as $key => $tmp_name){ move_uploaded_file($tmp_name, "data/{$_FILES['picture']['name'][$key]}"); } $pre_banner = $_FILES['picture']['name'][0]; $pre_logo1 = $_FILES['picture']['name'][1]; $pre_logo2 = $_FILES['picture']['name'][2]; $pre_logo3 = $_FILES['picture']['name'][3]; $querystring = "INSERT INTO presentation_tbl(pre_name,pre_banner,pre_text,pre_link,pre_logo1,pre_logo2,pre_logo3) VALUES(".$preName."','".$pre_banner."','".$preText."','".$preLink."','".$pre_logo1."','".$pre_logo2."','".$pre_logo3."')"; $doquery = mysql_query($querystring); echo "Upload was a sucess"; } else { echo "there was a problem uploading, please try again!"; } } Quote Link to comment https://forums.phpfreaks.com/topic/263000-inserting-arrays-into-database/#findComment-1348009 Share on other sites More sharing options...
mrMarcus Posted May 23, 2012 Share Posted May 23, 2012 Where you had: //print_r($_FILES); You can use <pre> tags to help clarify the data: echo '<pre>'; print_r($_FILES); echo '</pre>'; Then you can see exactly what's contained within the array, nice and neat. Quote Link to comment https://forums.phpfreaks.com/topic/263000-inserting-arrays-into-database/#findComment-1348011 Share on other sites More sharing options...
mrMarcus Posted May 23, 2012 Share Posted May 23, 2012 Oh, and don't forget to sanitize incoming (form) data before placing within a query. Look at mysql_real_escape_string. Also, if you are allowing files to be uploaded by the public, you will want to check the MIME types upon upload and not allow for executables or system files (file.exe; file.ini, etc). Quote Link to comment https://forums.phpfreaks.com/topic/263000-inserting-arrays-into-database/#findComment-1348015 Share on other sites More sharing options...
cdoggg94 Posted May 23, 2012 Author Share Posted May 23, 2012 Worked like a charm! Many thanks! Go leafs! Quote Link to comment https://forums.phpfreaks.com/topic/263000-inserting-arrays-into-database/#findComment-1348019 Share on other sites More sharing options...
mrMarcus Posted May 23, 2012 Share Posted May 23, 2012 Yeah, Go Leafs... :'( Quote Link to comment https://forums.phpfreaks.com/topic/263000-inserting-arrays-into-database/#findComment-1348020 Share on other sites More sharing options...
cdoggg94 Posted May 23, 2012 Author Share Posted May 23, 2012 One day... :-\ Quote Link to comment https://forums.phpfreaks.com/topic/263000-inserting-arrays-into-database/#findComment-1348023 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.