How to use inserting multiple rows (bulk inserting) at a time in php (dynamic rows)

this is my html table (dynamic rows)

s.no        name         email
1             abc             .abc@gmail
2             abc1           abc1@gmai
3             abc3           abc3@gmail
////////// and so on…///////////////////

Here my question is how to do bulk insert in Mysql Prepare statement if fetching values after clicking submit button link : $name = $_POST[‘name’]; it will fetch abc3 value only…

any suggestion please…!

You need to use an array name for the form fields - name=‘name[]’ You can then loop over the array of submitted data using a foreach loop. You would prepare the query once, before the start of the loop, then just get and use each set of values when you execute the query inside the loop.

1 Like

Kindly share any demo code please…

regards,
Darshan

http://php.net/manual/en/faq.html.php#faq.html.arrays

Really? Let’s see, Loops, arrays, prepared statements. There are your keywords.

1 Like

@ astonecipher

 $rows = array();
 $sql = 'SELECT * from  parents_details ';
 $query = $conn->prepare($sql);
 $query->execute();

 while($row = $query->fetch(PDO::FETCH_ASSOC))
 {
     $ids[] = $row["name"]; 
 }
 $name= implode(", ", $ids);

OUTPUT

abc,acb1,abc2,abc3,…etc

$query = $conn->prepare("INSERT INTO `student_reg`(`student_name`) VALUES (?)");
$query->bindParam(1, $name);
$result = $query->execute(array($name));

Here inserting all $name values are stored in a single rows in database but i want to in separate rows any suggestion…?

thank you,

I got the Solution…

thank you

Good.

Though I would tell you that you don’t want to store a coma delimited list in a column.

No you dont. Not the right one anyways.

Why would you duplicate data? Learn “Database Normalization”.

This is my code & its working …
anything did i wrong…?

$rows = array();
$sql = 'SELECT * from  parents_details ';
$query = $conn->prepare($sql);
$query->execute();

while($row = $query->fetch(PDO::FETCH_ASSOC))
{
  $ids[] = $row["email"]; 
}

$totalUsername = sizeof($ids);

for($i=0;$i<$totalUsername;$i++) {
   
   $InsertUsername = $ids[$i];
    
   $query = $conn->prepare("INSERT INTO `student_reg`(`student_name`) VALUES (?)");
   $query->bindParam(1, $InsertUsername);
   $result = $query->execute(array($InsertUsername));
   
 }

You haven’t answered the question of why you are duplicating data.

I am not getting what is the Duplicate data…?

You are selecting ALL the columns from one table but only using ONE column and then duplicating that data by inserting the data from that one column to another table, thus duplicating the same exact data that is in the first table. Why? There is a problem somewhere.

Ohhh that one only for testing purpose…
my destination is bulk inserting id from one table to another table and other values are different like bulk assigning product to Tailor…

How about telling me about what you are doing rather than how you are doing it.

Sorry i am not getting what you are saying…

What is the overall task/problem you are trying to solve with this code?

Once user is came and booked some product (ex 5 items ) and stored into DB(with one booking ID) after that admin can search product (ex shirt) it will show around 10 products and i want to assign to one person. If one by one Not a problen, I want to bulk assign to one person…

that is my task…

That code is different than you posted above?

I was questioning the implode usage.

Now, to correct this:

You don’t want to store the email, you want to store a link to that email. What happens when you need to update an email address? Now you have to update it across the spectrum. Instead, you update it in one table and the link stays the same. When you query it, you always have the data from the one source of truth.

Next, why are you using an email from one table to insert into an name column? That means the column is not named properly and is confusing. And why are you selecting everything, when you are only interested in the email? This is a huge performance problem and isn’t anything that you should want to run twice.

Actually i told you above queries are testing purpose then i want to assign to main project…

anyways thank you for your precious time…

Sponsor our Newsletter | Privacy Policy | Terms of Service