【问题标题】:php - How do i insert HTML table data into MySQLphp - 如何将 HTML 表格数据插入 MySQL
【发布时间】:2017-10-10 07:02:27
【问题描述】:

我有一个带有文本和单选输入的 HTML 表,我想用 PHP 将每一行插入到我的 MySQL 表中

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 查询循环中获取值,我希望它是 只能点击其中一个电台,并将点击的电台发送到状态列

代码

 <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 具有操作 我想要一个提交按钮来插入它

【问题讨论】:

  • 所以你想在你的 mysql 表的 1 列中插入所有数据?
  • 不,文本输入插入“名称”列,3 个收音机中选定的收音机插入“状态”列,文本区域插入“外部”列,所有这些结果为一行,因此它插入多行
  • 您想使用提交按钮吗?提交按钮是否与表单分开?
  • @Swellar 不,提交按钮在表单标签中

标签: php html mysql


【解决方案1】:

由于表格是动态填充的,所以需要使用数组作为名称属性

<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 是这样的,假设数据中有值

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

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

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

注意:我删除了表格中的 echo 部分,我可能遗漏了一些引号或拼写错误,请发表评论以进行澄清

【讨论】:

    【解决方案2】:

    为您的单选按钮添加名称属性。但是,每个表行的名称应该是唯一的,因此您最终可能会使用自动生成的名称。像这样的:

    ...
    $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++;
    }
    ...
    

    事实上,您需要为每个字段添加一个唯一的名称,包括 text 和 textarea,以确保您分别插入每一行的值。

    现在要检索值以保存在数据库中,您可以执行以下操作:

    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);
        }
    }
    

    【讨论】:

    • 如何将数据插入数据库?
    • @Swellar from another table 我可能没有选择显示文本的最佳方式,所以我只是将文本放入只读输入文本中
    【解决方案3】:

    首先,您没有正确发送数据

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

    这只是正确查看和插入的部分。如果您需要 insert.php 方面的帮助,请告诉我

    【讨论】:

    • 是的,你确定如何制作 insert.php?
    • 抱歉回复晚了,能否更新您的问题以显示您在 insert.php 中的努力?
    • 没关系,我已经从另一个人那里找到了答案,不过谢谢!
    猜你喜欢
    • 2017-05-14
    • 2019-06-06
    • 2013-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-17
    • 1970-01-01
    相关资源
    最近更新 更多