1

I want to create a table that users can fill with values and submit to a database. Each row of the table should be saved as an individual row in the table. Table example is shown below.

First | Last  | Phone
Steve | Jones | 1234
Jason | Smith | 4567
Mark  | Black | 6789

Submit

I'm creating my table as a form and submitting using POST. How can I submit each row as an individual row to my database, using only 1 submit button?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
monkeyman
  • 135
  • 1
  • 4
  • 14

3 Answers3

1
<form method='POST' action='submitPage.php'>
    <table>
        <tr>
            <td><input name='first[]'></td>
            <td><input name='last[]'></td>
            <td><input name='phone[]'></td>
        </tr>
        <tr>
            <td><input name='first[]'></td>
            <td><input name='last[]'></td>
            <td><input name='phone[]'></td>
        </tr>
        .
        .
    </table>
    <input type='submit' value='submit details'>
</form>

submitPage.php

<?
$first = $_POST['first'];
$last = $_POST['last'];
$phone = $_POST['phone'];

$totalFirstName = sizeof($first);

for($i=0;$i<$totalFirstName;$i++)
{
    $FirstName = $first[$i];
    $LastName = $last[$i];
    $Phone = $phone[$i];

    $Query = "INSERT INTO TableName SET first='$FirstName', last='$LastName', phone='$Phone'";
    //Execute Your Query 
}

?>
Nana Partykar
  • 10,556
  • 10
  • 48
  • 77
0
<input ... name="contact[1][first]">
<input ... name="contact[1][last]">
<input ... name="contact[1][phone]">
...
<input ... name="contact[2][first]">
<input ... name="contact[2][last]">
<input ... name="contact[2][phone]">

Also check out this question.

P.S. you need to google better.

Community
  • 1
  • 1
Alexander Mikhalchenko
  • 4,525
  • 3
  • 32
  • 56
  • I didn't use your working but I did use the working in the question you linked so thanks for the help. I shall improve my googling skills before my next question. – monkeyman Dec 23 '15 at 11:16
0
<form>
    <fieldset>
        <input name="first[]" type="text"/>
        ....

May be useful to you, having the field names as arrays allows you to iterate over them, such as:

<?php 
    $firsts = $_POST["first"];
    $lasts = $_POST["lasts"];
    $num = $_POST["numbers"];

    if(count($firsts) == count($lasts) == count($num) {
        for($i = 0; $i < count($firsts); $i++) {
            $toSubmit = [$firsts[$i], $lasts[$i], $num[$i]];
            // put that into your SQL inserts w/e.
        }
    }
?>
Jack hardcastle
  • 2,748
  • 4
  • 22
  • 40