-1

I have a table where I am inserting multiple rows at a time. The rows are getting inserted properly but the images are not getting inserted.

table1
------
id name
1  Abhi
2  Pritam

table2
------
id subid multipleimages
1  1     1.jpg
2  1     2.jpg
3  2     3.jpg
4  2     4.jpg

Above is the table structure. I have a form having dynamic values like "add more". So here is the below code for the form.

<form>
<input type="text" name="name[]">
<input type="file" name="multipleimages[]"> 
<button>Add more</button>
</form>
// Insert query in php
<?php
 $name=$_POST['name'];
 $lastid=array();
 foreach($name as $key=>$val){
   mysql_query("insert into table1 (name) values ('".$name."')");
   $lastid=mysql_insert_id();
 }
 foreach ($lastid as $key => $value){
  // here will be the image upload code.
  // insert into table 2 
 }
?>
Abhishek
  • 149
  • 1
  • 1
  • 15
  • 1
    This is a design flaw in your database. See [Is storing a delimited list in a database column really that bad?](https://stackoverflow.com/questions/3653462/is-storing-a-delimited-list-in-a-database-column-really-that-bad) – ADyson Aug 22 '22 at 07:24
  • Yeah I know that but the project is already set up. – Abhishek Aug 22 '22 at 07:27
  • It's never too late to change, it'll save you trouble later on – ADyson Aug 22 '22 at 07:37
  • Anyway we can't answer your question because it seems you have some sort of issue with reading the uploaded images and writing them to the database, but you haven't shown any code which does that. You'd need to provide a [mre] of your issue – ADyson Aug 22 '22 at 07:39
  • okay so if I need to change the database structure, then I have to add another table where I have to add the images with the last id . But how to upload those images . please help to share the code. – Abhishek Aug 22 '22 at 07:43
  • I have edited the question as per your last suggestion. Now please help me to upload and insert the images in table2. – Abhishek Aug 22 '22 at 07:52
  • You have a serious security issue. You need to urgently read https://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php as well, and then follow it with https://stackoverflow.com/questions/7537377/how-to-include-a-php-variable-inside-a-mysql-statement – ADyson Aug 22 '22 at 10:06
  • Anyway your logic is wrong. You need to insert into table2 during the `foreach($name as $key=>$val)` loop, immediately after you get the ID of the row you just added to table1. – ADyson Aug 22 '22 at 10:09
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/247463/discussion-between-abhishek-and-adyson). – Abhishek Aug 23 '22 at 06:21

1 Answers1

0
  1. $lastid=mysql_insert_id(); this doesn't seem to add every $val to $lastid; only the last one
RorinL
  • 84
  • 8
  • 1
    So how to do that? Can you please suggest? – Abhishek Aug 22 '22 at 08:49
  • $lastid[]=mysql_insert_id(); This returns mysql_insert_id() to the $lastid array – RorinL Aug 22 '22 at 09:33
  • Yes that I have done but the same images are getting inserted for row 1 and row 2. The images should be different. Fo example: for name: abhi, the file uploads are 1.jpg, 2.jpg and for name: pritam, the files should be 3.jpg and 4.jpg. – Abhishek Aug 22 '22 at 09:52
  • as he said. @ADyson – RorinL Aug 22 '22 at 10:35
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 26 '22 at 06:23