我有一个表,其中 ResultHRM 列包含一个文本区域,用户可以在其中写评论。我想知道,当我点击提交按钮时,如何将带有文本区域内容的正确行更新到我的 MySQL 表 NContrib 中? 我读到 textarea 不能有 value 属性。
我的表 NContrib 具有以下结构:
+-------------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------------+--------------+------+-----+---------+----------------+
| IdVariantNContrib | mediumint(9) | NO | PRI | NULL | auto_increment |
| ID | varchar(30) | YES | | NULL | |
| Reference | varchar(20) | YES | | NULL | |
| ResultHRM | varchar(60) | YES | | NULL | |
+-------------------+--------------+------+-----+---------+----------------+
在我的 php 页面中,我这样显示这个表:
<form method='POST' action='SaveValidation.php'>
<?php
require_once 'config.php'; //database connection
$sql='SELECT ID, Reference, ResultHRM FROM NContrib';
$PerformSql=mysqli_query($conn,$sql) or die(mysqli_error($conn));
?>
<table style="width:100%" border='1px' CELLSPACING='0' cellpadding='2'>
<tr>
<th> ID </th>
<th> Reference </th>
<th> ResultsHRM </th>
</tr>
<?php
while($rowNcontrib = mysqli_fetch_assoc($PerformSql)) {
echo "<tr><td> ".$rowNcontrib["ID"]." </td><td> ".$rowNcontrib["Reference"]." </td><td><textarea name='ResultHRM[]' id='ResultHRM[]' cols='30' rows='1'></textarea></td></tr>";
}
?>
</table>
<input type="submit" name="sendEcht" value="Submit" />
</form>
最佳答案
1) 使用 $rowNcontrib['ResultHRM']
在<textarea></textarea>
之间显示value
2) 亦用亦用 name='ResultHRM[]'
这会给你一个 array
的 textarea
您已更新的值,但您将无法跟踪哪个值属于哪个 ID
.所以name='ResultHRM[$id]'
.这里id
应该是row
的id
所以你的代码应该是
喜欢
while($rowNcontrib = mysqli_fetch_assoc($PerformSql)) {
$id = $rowNcontrib["ID"];
echo "<tr><td> ".$rowNcontrib["ID"]." </td><td> ".$rowNcontrib["Reference"]." </td><td><textarea name='ResultHRM[$id]' id='ResultHRM[]' cols='30' rows='1'>".$rowNcontrib['ResultHRM ']."</textarea></td></tr>";
}
现在在页面 SaveValidation.php
检查这个
if(isset($_POST['sendEcht'])){
echo "<pre>";print_r($_POST);die;}
这将向您显示类似的内容
Array
(
[ResultHRM] => Array
(
[3] => hello its test
[11] => test string
[10] =>
)
[sendEcht] => Submit
)
// here 3,11, 10 are my id's of table in your case it will be the id's of your table then you can easily update your table `ResultHRM` field
关于Php MySQL 更新表 textarea html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38655576/