jacko_162 Posted November 26, 2016 Share Posted November 26, 2016 i have the below code; // start of image upload $insert_id = mysql_insert_id() or die("Unable to get insert id for image name.<br>" . mysql_error()); extract($_POST); $fileArray = array(); $error=array(); $extension=array("jpeg","jpg","png","gif"); foreach($_FILES["files"]["tmp_name"] as $key=>$tmp_name) { $file_name=$_FILES["files"]["name"][$key]; $file_tmp=$_FILES["files"]["tmp_name"][$key]; $ext=pathinfo($file_name,PATHINFO_EXTENSION); if(in_array($ext,$extension)) { if(!file_exists("../images/listings/".$file_name)) { $filename=basename($file_name,$ext); $newFileName=$insert_id."_".mt_rand(1, 99999).".".$ext; move_uploaded_file($file_tmp=$_FILES["files"]["tmp_name"][$key],"../images/listings/".$newFileName); // NOW ADD NAME TO DATABASE $insertimg = "insert into $table3 (name,insert_id) values ('".$newFileName."','".$insert_id."')"; mysql_query($insertimg); array_push($fileArray, "$newFileName"); } else { $filename=basename($file_name,$ext); $newFileName=$filename.mt_rand(1, 99999).".".$ext; move_uploaded_file($file_tmp=$_FILES["files"]["tmp_name"][$key],"../images/listings/".$newFileName); // NOW ADD NAME TO DATABASE $insertimg = "insert into $table3 (name,insert_id) values ('".$newFileName."','".$insert_id."')"; mysql_query($insertimg); array_push($fileArray, "$newFileName"); } } else { $insertimg = "insert into $table3 (name,insert_id) values (' ','".$insert_id."')"; mysql_query($insertimg); array_push($error,"$file_name, "); } } // end of image upload I have managed to get this working fine, and it uploads the files and inserts data into a table. now i'm working on the edit page code and struggling to figure out how i can get this data down and display it, should i pull the data, echo the images and have some sort of IF statement to say if image exists then show a delete checkbox and if no image exists then show another file input? I'm also not sure if the add images code is the right way of doing it by adding the images to a separate line in the table or adding the array into the table for ease of use. i am not great at PHP and any help, or links to code would be great. Thanks Quote Link to comment Share on other sites More sharing options...
benanamen Posted November 26, 2016 Share Posted November 26, 2016 You are using obsolete insecure code that has been completely removed from Php. You need to be using PDO. Update your code and we will be happy to help. https://phpdelusions.net/pdo Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted November 26, 2016 Share Posted November 26, 2016 here's a much easier way of doing this. get the main table's last insert id, like you are doing now, and as you loop over the uploaded files, for each successful one (your code isn't actually testing the ['error'] element to know if the file uploaded without any error), simply insert a new row containing the main table's last insert id into whatever $table3 is. get the last insert id from this table and use that as the destination file name for the move_uploaded_file() statement. this assigns a unique id (filename) for each image. if you are storing information about each image, such as original file name, description, ... you would store it in the correct row in this table. to 'edit' the file information, you need to handle each possibility - 1) no change, i.e. keep the existing image. you would display the existing file (thumbnail), original name, description, and use the image id (filename) as the type='file' form field name's array index value, which will become the $key value in the php code. i would use a different form field name for existing images (such as 'existing_files'), from the form field name for new images (currently it's 'files'). if no new file is uploaded (there's a specific error value, which is where checking the ['error'] element comes in), you would do nothing for the particular image. 2) replace an existing image. in this case you would select a new image in the browser and upload it. the ['error'] element would indicate a successful uploaded image. you would get the existing id (filename) from the $key value, and after making sure it corresponds to the current main table data being edited, you would simply use the id (filename) in the move_uploaded_file() statement to replace the image, leaving everything else as is. 3) delete an existing image. you would have a checkbox as you have theorized. the checkbox name would be an array with the array index value being the id (filename). for any checkboxes that are checked, you would get the array index value and after making sure it corresponds to the current main table data being edited, delete the corresponding image file and the row in $table3. 4) add image(s). this would use your existing code. by using a different form field name for existing files and for new files, the 'edit' code and the 'insert' code would operate on their own set of form fields. this will simplify your existing database code, making it easier to update it to current best practices and standards. 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.