php-如何将HTML表格数据插入MySQL [英] php - How do i insert HTML table data into MySQL

查看:39
本文介绍了php-如何将HTML表格数据插入MySQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有文本和广播输入的HTML表,我想用PHP将每一行插入到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表如下:

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

HTML表格

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

它从MySQL查询循环中获取值,我希望它成为只能单击其中一个无线电,它将单击的无线电发送到状态"列中

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

代码

 <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>

该表在一个表单标签中,并具有对另一个.php的作用.我想要一个提交按钮来插入它

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

推荐答案

由于表是动态填充的,因此需要使用数组作为name属性

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>

php 类似于这样,假设数据中包含值

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/>';
}

那应该显示您在表中每行选择的值,我不使用 mysqli ,所以我不会提供将其插入数据库的功能,但是重要的是您现在有了所需的数据

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

要查看数组的内容,请使用 print_r($ tableRow)

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

注意::我删除了表格中的 echo 部分,我可能错过了一些引号或错别字,只是为了澄清而发表评论

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

这篇关于php-如何将HTML表格数据插入MySQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆