我有一个带有文本和单选输入的 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 查询循环获取值,我希望它是
只能单击其中一个无线电,并将单击的无线电发送到“状态”列
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>
编辑:该表位于表单标记中,并且具有对另一个 .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>
The php
会是这样的,假设数据中有值
$tableRow = $_POST['tableRow'];
foreach($tableRow as $row){
echo $row['dataName'].' '.$row['status'].'<br/>';
}
这应该显示您在表中每行选择的值,我不使用mysqli
所以我不会提供将其插入数据库的功能,但重要的是您现在拥有所需的数据
要查看数组的内容,请使用print_r($tableRow)
NOTE:我删除了echo
表中的部分,我可能错过了一些引用或一些拼写错误,只需评论以进行澄清
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)