Hi to all,

Can anyone help me to show the code how to save multiple data into mysql via PHP? Thanks in advance.

Here's some code

<?php

  echo "<form action='savetoanothertable.php' method='post'>";
  $query = mysql_query("SELECT * FROM tbl_student");
  while($result = mysql_fetch_array($query)
  {
    echo "<input type='text' name='studname' value='$result[studentname]'>";
    echo "<input type='text' name='studno' value='$result[studentnumber]'>";
    echo "<br>";
//lets say i have 5 students on my tbl_student. so we can expect on this code that we have 10 text box(5 for studentname and 5 for student number)
  }

  echo "<input type='submit' name='submit' value='submit'>";
  echo "</form>";

my question is, what is the code for savetoanothertable.php for me to save the above 10 record into another table. please help. thanks so much.

Recommended Answers

All 2 Replies

Okay, here's what I think, so maybe I'd be wrong and everything.

The code you wrote will not work properly, as all the text field inputs you generated by using this while loop will have the same name, 5 will have the name 'studname', and the other ones will have the name 'studno'.

So, when the form is submitted it will in face submit only two text fields, one containing the last value of 'studname', and the same goes for 'studno' as well. Why? Because the POST array will contain each text field as a name and its value will be the last value (i.e., the 5th. text field value).

What I'm saying is you should find another way to populate those text fields, you can put an index variable to use in the name property in the form and increment it in the end of the while loop each time you iterate through it.

And on the form processing page you're asking about, you can do another loop with a similar index thing to retrieve those values from the POST array and drop it into a database or something.


Hope I could help you with anything. Regards.

Thanks for your reply elwafdy.

I found the answer by my self through the help to google.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.