Hrvoje Posted February 2, 2015 Share Posted February 2, 2015 Hi there, I need a little help... I have simple form/code for upload image and insert it into db, and its working. <form name="unos" action="" method="post" enctype="multipart/form-data"> <select name="kategorija"> <option value="1">Album</option> </select> <input type="file" name="uploaded_file[]" id="file" > </form> <?PHP include "config.php"; $uploads_dir = 'slike'; foreach ($_FILES["uploaded_file"]["error"] as $key => $error) { if ($error == UPLOAD_ERR_OK) { $tmp_name = $_FILES["uploaded_file"]["tmp_name"][$key]; $name = $_FILES["uploaded_file"]["name"][$key]; move_uploaded_file($tmp_name, "$uploads_dir/$name"); } } $sql="INSERT INTO galerija_slike (kategorija_id, url_slike) VALUES ('".$_POST['kategorija']."', '".$_FILES['uploaded_file']['name'][$key]."')"; if (mysql_query($sql)) { echo "success"; } else { echo "error" . mysql_error(); } ?> How can I store more image url's from input fields? When I add more input fields..... move_uploaded_file(): uploads all images, but I don't know how to also add them in db? I need something like this: <input type="file" name="uploaded_file[]" id="file" > <input type="file" name="uploaded_file[]" id="file" > <input type="file" name="uploaded_file[]" id="file" > <input type="file" name="uploaded_file[]" id="file" > <input type="file" name="uploaded_file[]" id="file" > Thanks Link to comment https://forums.phpfreaks.com/topic/294306-need-help-upload-images/ Share on other sites More sharing options...
QuickOldCar Posted February 2, 2015 Share Posted February 2, 2015 You can move your mysql query within the foreach loop <?PHP include "config.php"; $uploads_dir = 'slike'; foreach ($_FILES["uploaded_file"]["error"] as $key => $error) { if ($error == UPLOAD_ERR_OK) { $tmp_name = $_FILES["uploaded_file"]["tmp_name"][$key]; $name = $_FILES["uploaded_file"]["name"][$key]; move_uploaded_file($tmp_name, "$uploads_dir/$name"); $sql="INSERT INTO galerija_slike (kategorija_id, url_slike) VALUES ('".$_POST['kategorija']."', '".$_FILES['uploaded_file']['name'][$key]."')"; if (mysql_query($sql)) { echo "success"; } else { echo "error" . mysql_error(); } } } ?> Link to comment https://forums.phpfreaks.com/topic/294306-need-help-upload-images/#findComment-1504550 Share on other sites More sharing options...
scootstah Posted February 2, 2015 Share Posted February 2, 2015 You can move your mysql query within the foreach loop No! You don't want to be executing SQL queries in a loop if at all possible. Instead you can use batch insert syntax. $insertValues = array(); $kategorija = mysql_real_escape_string($_POST['kategorija']); foreach ($_FILES["uploaded_file"]["error"] as $key => $error) { if ($error == UPLOAD_ERR_OK) { $tmp_name = $_FILES["uploaded_file"]["tmp_name"][$key]; $name = $_FILES["uploaded_file"]["name"][$key]; move_uploaded_file($tmp_name, "$uploads_dir/$name"); $url = mysql_real_escape_string($_FILES['uploaded_file']['name'][$key]); $insertValues[] = "('" . $kategorija . "', '" . $url . "'"); } } $sql="INSERT INTO galerija_slike (kategorija_id, url_slike) VALUES " . implode(',', $insertValues);This will produce a query that looks something like:INSERT INTO galerija_slike (kategorija_id, url_slike) VALUES ('some category', 'some filename'),('some category', 'some other filename'),('some category', 'yet another filename') Link to comment https://forums.phpfreaks.com/topic/294306-need-help-upload-images/#findComment-1504610 Share on other sites More sharing options...
QuickOldCar Posted February 2, 2015 Share Posted February 2, 2015 I agree, and along the way each category will have to be added to an array and used, unless will always be the same category all uploaded images. Should also only insert if the array is not empty if(!empty($insertValues)){ $sql="INSERT INTO galerija_slike (kategorija_id, url_slike) VALUES " . implode(',', $insertValues); } Link to comment https://forums.phpfreaks.com/topic/294306-need-help-upload-images/#findComment-1504616 Share on other sites More sharing options...
Hrvoje Posted February 3, 2015 Author Share Posted February 3, 2015 Thanks for your reply. QuickOldCar - Your code is working. And csootstah, your code only upload images but nothing inserts in dba. Here is pharse error, I correct it but nothing happend. Only upload images. $insertValues[] = "('" . $kategorija . "', '" . $url . "'"); $insertValues[] = "('" . $kategorija . "', '" . $url . "')"; Link to comment https://forums.phpfreaks.com/topic/294306-need-help-upload-images/#findComment-1504679 Share on other sites More sharing options...
scootstah Posted February 4, 2015 Share Posted February 4, 2015 Thanks for your reply. QuickOldCar - Your code is working. And csootstah, your code only upload images but nothing inserts in dba. Here is pharse error, I correct it but nothing happend. Only upload images. $insertValues[] = "('" . $kategorija . "', '" . $url . "'"); $insertValues[] = "('" . $kategorija . "', '" . $url . "')"; Oops, sorry about the error. You're checking for SQL errors, are there any? Link to comment https://forums.phpfreaks.com/topic/294306-need-help-upload-images/#findComment-1504775 Share on other sites More sharing options...
Hrvoje Posted February 4, 2015 Author Share Posted February 4, 2015 Oops, sorry about the error. You're checking for SQL errors, are there any? Is this is a joke? I do not want to be misunderstood. I apologize if you don't understand me. There are no errors in SQL Link to comment https://forums.phpfreaks.com/topic/294306-need-help-upload-images/#findComment-1504781 Share on other sites More sharing options...
scootstah Posted February 4, 2015 Share Posted February 4, 2015 No, not a joke. You're looking for query errors here: if (mysql_query($sql)) { echo "success"; } else { echo "error" . mysql_error(); }So, you said that my query didn't work, and there is no errors? Try printing the $sql variable and posting the output. Link to comment https://forums.phpfreaks.com/topic/294306-need-help-upload-images/#findComment-1504791 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.