Php MySQL update table textarea html


Php MySQL update table textarea html

我有一个表,其中ResultHRM列包含一个文本区域,用户可以在其中编写注释。我想知道,当我点击提交按钮时,如何将文本区域内容的正确行更新到我的MySQL表NContrib中?我读到文本区域不能有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>&nbsp;ID&nbsp;</th>
    <th>&nbsp;Reference&nbsp;</th>
    <th>&nbsp;ResultsHRM&nbsp;</th>
  </tr>
<?php
while($rowNcontrib =  mysqli_fetch_assoc($PerformSql)) {
  echo "<tr><td>&nbsp;".$rowNcontrib["ID"]."&nbsp;</td><td>&nbsp;".$rowNcontrib["Reference"]."&nbsp;</td><td><textarea name='ResultHRM[]' id='ResultHRM[]' cols='30' rows='1'></textarea></td></tr>";
 }
?>
</table>
<input type="submit" name="sendEcht" value="Submit" />
</form>

1)在<textarea></textarea>之间使用$rowNcontrib['ResultHRM']来显示value

2) 也可以使用使用name='ResultHRM[]',这将为您提供一个arraytextarea值,您已经更新了这些值,但您将无法跟踪哪个值属于哪个ID。因此CCD_ 8。这里id应该是row 的id

所以你的代码应该是

 while($rowNcontrib =  mysqli_fetch_assoc($PerformSql)) {
        $id = $rowNcontrib["ID"];
      echo "<tr><td>&nbsp;".$rowNcontrib["ID"]."&nbsp;</td><td>&nbsp;".$rowNcontrib["Reference"]."&nbsp;</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

我想在这里提到以下几点:

  1. 如果ResultHRM字段将以表单形式保存文本区域中的值,我建议您更改数据类型。目前,其varchar(60)。我建议应该是text
  2. 从数据库中获取数据后,将在表结构中显示数据。我建议您添加文本区域并用现有值填充它。假设您将文本区域命名为ResultHRM_ta
  3. 一旦您遵循第2点并提交表单,您就可以在SaveValidation.php上的$_POST['ResultHRM_ta']中获得文本区域值
  4. 将值保存在数据库中

请注意,我已经提到了你需要遵循的过程的基本纲要。您将不得不自己处理安全措施和编码标准。

要解决这个问题,您可以调用AJAX请求,并在javascript方法中为每个请求传递代码和id。

在html代码中,您可以调用每个表行中的方法,如下所示:

<table style="width:100%" border='1px' CELLSPACING='0' cellpadding='2'>
  <tr>
    <th>&nbsp;ID&nbsp;</th>
    <th>&nbsp;Reference&nbsp;</th>
    <th>&nbsp;ResultsHRM&nbsp;</th>
  </tr>
<?php
while($rowNcontrib =  mysqli_fetch_assoc($PerformSql)) {
  echo "<tr><td>&nbsp;".$rowNcontrib["ID"]."&nbsp;</td><td>&nbsp;".$rowNcontrib["Reference"]."&nbsp;</td><td><textarea name='ResultHRM' id='ResultHRM' cols='30' rows='1'></textarea></td><td><button type='button' onclick='update(".$rowNcontrib["ID"].")'">Update</button>;
 }
?>
</table>