gpt4 book ai didi

php - 如何将 HTML 表数据插入 MySQL

转载 作者:行者123 更新时间:2023-11-29 05:59:01 24 4
gpt4 key购买 nike

我有一个带有文本和 radio 输入的 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 查询循环中获取值,我希望它是只有一个 radio 可以被点击,它会将被点击的 radio 发送到状态栏

代码

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

最佳答案

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

<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 部分,我可能遗漏了一些引号或一些拼写错误,只是评论澄清

关于php - 如何将 HTML 表数据插入 MySQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46660465/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com