简体   繁体   中英

php - How do i insert HTML table data into MySQL

I have an HTML table with text and radio inputs that i want to insert each row into my MySQL table with PHP

MySQL table looks like this:

================================
|     Name      | Status | Ext |
================================

HTML table

===============================================================
|     Name      | Present | Excused | Unexcused |     Ext     | 
===============================================================
|  text input1  |    o    |    o    |     o     |  textarea   |
---------------------------------------------------------------
|  text input2  |    o    |    o    |     o     |  textarea   |
and so on...
*o's are radios

it gets values from a MySQL query loop and i want it to be only one of the radios can be clicked and it sends the clicked one into the Status column

Code

 <form method="post" action="insert.php" enctype="multipart/form-data">
    <table>
        <tr>
            <th>Name</th>
            <th>Present</th>
            <th>Excused</th>
            <th>Unexcused</th>
            <th>Ext</th>
        </tr>
        <?php         
        echo "<tr>";
        $query = "select * from TbCard";
        $sql = mysqli_query($connect, $query);
            while ($data = mysqli_fetch_array($sql)) {
                echo '<tr>';
                echo"<td><input id='name' type='text' value='" . $data['Name'] . "' readonly style='border:none;width:350px'></input></td>";
                echo"<td><input type='radio'></td>";
                echo"<td><input type='radio'></td>";
                echo"<td><input type='radio'></td>";
                echo"<td><textarea></textarea></td>";
                echo '</tr>';
            }
        }
        echo "</tr>";
        ?>
    </table>
<input type="submit" value="Insert">
</form>

EDIT: The table is in a form tag and has the action to another .php and i want a submit button to insert it

Since the table is dynamically filled, you need to use an array as the name attribute

<table>
        <tr>
            <th>Name</th>
            <th>Present</th>
            <th>Excused</th>
            <th>Unexcused</th>
            <th>Ext</th>
        </tr>
        <?php         
        $query = "select * from TbCard";
        $sql = mysqli_query($connect, $query);
        $count = 0;
            while ($data = mysqli_fetch_array($sql)) {
        ?>
                <tr>
                <td>
                    <input name="tableRow[<?php echo $count; ?>]['dataName']" id='name' type='text' value="<?php echo $data['Name'];?>" readonly style='border:none;width:350px'></input>
                </td>
                <td>
                    <input name="tableRow[<?php echo $count; ?>]['status']" type="radio" value="Present"> Present
                </td>
                <td>
                    <input name="tableRow[<?php echo $count; ?>]['status']" type="radio" value="Excused"> Excused
                </td>
                <td>
                    <input name="tableRow[<?php echo $count; ?>]['status']" type="radio" value="Unexcused"> Unexcused
                </td>
                </tr>;
        <?php
             $count++;
            }
        ?>
    </table>

The php would be something like this, assuming that the data has values in it

$tableRow = $_POST['tableRow'];
foreach($tableRow as $row){
    echo $row['dataName'].' '.$row['status'].'<br/>';
}

That should show the values you chosen per row in the table, I don't use mysqli so I will not provide the the functions to insert it into the database, but the important thing is you now have the data needed

To see the content of the array, use print_r($tableRow)

NOTE: I removed the echo part in the table, I might have missed some quotes or some typos, just comment for clarifications

Add name attribute to your radio button. However name should be unique for each table row, so you might end up with auto generated name. Something like this:

...
$index = 0;
while ($data = mysqli_fetch_array($sql)) {
    echo '<tr>';
    echo"<td><input id='name' name='name_$index' type='text' value='" . $data['Name'] . "' readonly style='border:none;width:350px'></input></td>";
    echo"<td><input type='radio' name='status_$index'></td>";
    echo"<td><input type='radio' name='status_$index'></td>";
    echo"<td><input type='radio' name='status_$index'></td>";
    echo"<td><textarea name='Ext_$index'></textarea></td>";
    echo '</tr>';
    $index++;
}
...

As a matter of fact, you need to add a unique name to each of the fields, including text and textarea to make sure you insert values of each row separately.

Now to retrieve values for saving in the db, you could do something like this:

foreach($_POST as $key => $val)
{
    if(strpos($key, 'name_') === 0 )
    {
        $criteria = explode("_", $key);
        $index = $criteria[2];
        //now you can get the rest of the fields
        $status = $_POST["status_$index"];
        $name = $_POST["name_$index"];
        $ext = $_POST["Ext_$index"];
        //assuming you have a SaveFunction:
        SaveFunction($name, $status, $ext);
    }
}

first of all your are not sending your data properly

            echo"<td><input id='name' type='text' value='" . $data['Name'] . "' readonly style='border:none;width:350px'></input></td>";
            echo"<td><input type='radio' name='status' value='Present' ".isset($data['Status']) && $data['Status']=='Present'? checked='checked':null."></td>";
            echo"<td><input type='radio' name='Status' value='Excused' ".isset($data['Status']) && $data['Status']=='Excused'? checked='checked':null."></td>";
            echo"<td><input type='radio' name='Status' value='Unexcused' ".isset($data['Status']) && $data['Status']=='Unexcused'? checked='checked':null."></td>";
            echo"<td><textarea name='Ext'></textarea></td>";

This is only the part for viewing and insertion properly. let me know if you need help in insert.php side

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM