erok Posted February 23, 2015 Share Posted February 23, 2015 Hi all , I am trying to write some codes which needs to be able to upload multiple images into separate rows in a table within one form. i only got the last picture inserted twice to mysqli database table, instead of inserting the first picture in the form first and moving to the second picture next. Any direction or help greatly appreciated. My code like this ; if (!empty($_POST['kitchen'])) { //Assign Variables $path = $_FILES['image']['tmp_name'] ; $name = $_FILES['image']['name'] ; $type = $_FILES['image']['type'] ; $error= $_FILES['image']['error'] ; $caption = $_POST['caption'] ; // read file contents $content2 = file_get_contents($path); $content = mysqli_real_escape_string($con, $content2); $caption = mysqli_real_escape_string($con, $caption) ; $bol = "insert into dakimages (name, type, content, caption) values ('{$name}', '{$type}', '{$content}', '{$caption}');"; $soy = mysqli_query($con, $bol) ; } if (!empty($_POST['kitchen'])) { //Assign Variables $path = $_FILES['image']['tmp_name'] ; $name = $_FILES['image']['name'] ; $type = $_FILES['image']['type'] ; $error= $_FILES['image']['error'] ; $caption = $_POST['caption'] ; // read file contents $content2 = file_get_contents($path); $content = mysqli_real_escape_string($con, $content2); $caption = mysqli_real_escape_string($con, $caption) ; // insert into table $dor = "insert into dakimages (name, type, content, caption) values ('{$name}', '{$type}', '{$content}', '{$caption}');"; $scl = mysqli_query($con, $dor) ; } ?> And this is form ; <input type="file" name="image" /> <br /> <br /> <span class="margin1">Caption for photo (1)</span> <br /> <textarea type="text" name="caption" maxlength="150" rows="3" col="85"></textarea> <br /> <input type="file" name="image" /> <br /> <span class="margin1">Caption for photo (2)</span> <br /> <textarea type="text" name="caption" maxlength="150" rows="3" col="85"></textarea> <br /> <input type="submit" name="kitchen" id="forminsert" value="upload all" size="35" /> Quote Link to comment https://forums.phpfreaks.com/topic/294840-last-row-overwriting-the-previous-row-in-the-table/ Share on other sites More sharing options...
tryingtolearn Posted February 23, 2015 Share Posted February 23, 2015 Since you have the file names the same you would need to submit it as an array . (ref http://php.net/manual/en/features.file-upload.multiple.php) <input type="file" name="image[]" /> Quote Link to comment https://forums.phpfreaks.com/topic/294840-last-row-overwriting-the-previous-row-in-the-table/#findComment-1506541 Share on other sites More sharing options...
Solution tryingtolearn Posted February 23, 2015 Solution Share Posted February 23, 2015 and then process the array something like this $img_count = count($_FILES['image']['name']); for($i=0;$i<$img_count;$i++) { $file_name = $_FILES['image']['name'][$i]; $file_type = $_FILES['image']['type'][$i]; $file_size = $_FILES['image']['size'][$i]; $file_path = $_FILES['image']['path'][$i]; Quote Link to comment https://forums.phpfreaks.com/topic/294840-last-row-overwriting-the-previous-row-in-the-table/#findComment-1506542 Share on other sites More sharing options...
erok Posted February 23, 2015 Author Share Posted February 23, 2015 To tryingtolearn; Thank you very much sir, Code is working now the way I wanted. Each image goes to another row in one query. Quote Link to comment https://forums.phpfreaks.com/topic/294840-last-row-overwriting-the-previous-row-in-the-table/#findComment-1506547 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.