[英]php - How do i insert HTML table data into MySQL
我有一个带有文本和无线电输入的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的操作,我想要一个提交按钮将其插入
由于表是动态填充的,因此您需要使用数组作为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
部分,我可能错过了一些引号或错别字,只是为了澄清而发表评论
将名称属性添加到您的单选按钮。 但是名称对于每个表行都应该是唯一的,因此您可能会以自动生成的名称结尾。 像这样:
...
$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++;
}
...
实际上,您需要为每个字段添加唯一的名称,包括文本和文本区域,以确保分别插入每一行的值。
现在要检索要保存在数据库中的值,您可以执行以下操作:
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);
}
}
首先您没有正确发送数据
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方面的帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.