繁体   English   中英

如何更新具有唯一ID的表行数据?

[英]how to update table row data with unique id?

码:

<?php
if(isset($_POST['save']))
{
  $comment1 = $_POST['comment2'].",".date('Y-m-d');
  $comment2 = $_POST['comment2'];
  $id = $_POST['id'];
  $query = "update enquires2 set comment1 = '$comment1', comment2 = '$comment2', s_date = '$s_datee' where id='$id'";
  $result = mysqli_query($link,$query);
  if($result==true)
  {
    echo "successfull";
  }
  else
  {
    echo "error!";
  }
}
?>

<form method="post" name="myform">
<table>
  <tr>
    <th>comment1</th>
    <th>comment2</th>
    <th>Action</th>
  </tr>
  <?php
    $sql = "select * from enquires2 ";
    $result = mysqli_query($link,$sql);
    while ($row = mysqli_fetch_array($result)) 
    {
  ?>
  <tr>
    <td>
      <input type='hidden' name='id' value='<?php echo $row['id']; ?>'>
    </td>

    <td>
     <?php echo $row['comment1']; ?>
    </td>

    <td>
      <input type='text' name='comment2' id='comment2' value=""/>
    </td>

    <td>
      <input type ='submit' name='save' id='save' value='Save' />
    </td>
  </tr>
  <?php
    }
  ?>
</table>
</form>

在此处输入图片说明

在这段代码中,我想用唯一的ID更新表enquires2。 在下图中,您看到具有保存按钮的表行仅是一行,类似地,它具有多行,每行中都有保存按钮。 现在我想要当我单击特定行的保存按钮时,仅该行数据将被更新。 我该如何解决这个问题? 请帮忙。

谢谢

您可以使用AJAX和jQuery进行此操作,然后将数据发送到单独的PHP文件,然后将$row['ID']分配给按钮的data-value属性,

$("#save-btn").click(function(){
    id = $(this).attr(data-value);
    ***** rest of values here
    $.ajax({
       method: "GET",
       data: {id: id, rest of: data here},
       url: phpfile.php,
       success: function(){
          console.log("Success");
       }
   })
});

在PHP文件中,您将获得如下ID:

$_GET['id'] ,并且与其他值相同,因为我们使用的是GET方法,然后将它们放入更新查询中。

首先,出于安全原因,您需要将此查询更改为准备好的语句,请参见PHP MySQLI Prevent SQL Injection

  $id = $_POST['id'];
  $query = "update enquires2 set comment1 = '$comment1', comment2 = $comment2', s_date = '$s_datee' where id='$id'";
  $result = mysqli_query($link,$query);


无论如何,这行是不好的,您缺少$ comment2的开头报价。

  $query = "update enquires2 set comment1 = '$comment1', comment2 = $comment2', s_date = '$s_datee' where id='$id'"; 

您确定$ link是实际的mysqli链接吗?

至于html部分,您需要为每条记录设置一种形式。 看到链接发布的HTML:是否可以通过XHTML有效方式在每个TABLE ROW中都有一个FORM标记?

或者,您可以做一些不好的事情,例如仅将$ id添加到每一行的evry字段中(类似于:)

if(isset($_POST['save']) && is_array($_POST['save'])){
   $id=key($_POST['save']);
}

并在php代码中检查女巫密钥是否已设置。

<?php
print_r($_POST);
if(isset($_POST['save']) && is_array($_POST['save'])){
  echo key($_POST['save']);
}
?>
<html>
    <form method='post'>
        <input type='submit' name='save[1]' value='1' />
        <input type='submit' name='save[2]' value='2' />
    </form>
</html>

您也需要复制坏处来发表您的评论,但作为概念证明,您可以在phpfiddle.org上运行此代码段

 <?php print_r($_POST); if(isset($_POST['save']) && is_array($_POST['save'])){ echo key($_POST['save']); } ?> <html> <form method='post'> <input type='submit' name='save[1]' value='1' /> <input type='submit' name='save[2]' value='2' /> </form> </html> 

希望我能为您提供一个完整的答案,但是要使其成为“适当的编码”,您的代码还有很多工作要做。 再次,这是因为除了您的代码对sql注入不利而且不可接受之外,还有一个意见问题。

根本不要将代码用于安全漏洞。 在此处阅读有关sql注入的更多信息。 毕竟,对于每行(),使用隐藏的输入存储row的id创建一个表单。

我修改了代码以使其正常运行,在您的td内创建了一个嵌套表,以便该标记可以被接受,

另请参见此链接以获取有效的参考资料HTML:是否可以通过XHTML有效方式在每个TABLE ROW中都有一个FORM标记?

    <?php
    if(isset($_POST['save']))
    {
      $comment1 = $_POST['comment2'].",".date('Y-m-d');
      $comment2 = $_POST['comment2'];
      $id = $_POST['id'];
      $query = "update enquires2 set comment1 = '$comment1', comment2 = '$comment2', s_date = '$s_datee' where id='$id'";
      $result = mysqli_query($link,$query);
      if($result==true)
      {
        echo "successfull";
      }
      else
      {
        echo "error!";
      }
    }
    ?>


    <table>
      <tr>
        <th>comment1</th>
        <th>comment2</th>
        <th>Action</th>
      </tr>
      <?php
        $sql = "select * from enquires2 ";
        $result = mysqli_query($link,$sql);
        while ($row = mysqli_fetch_array($result)) 
        {
      ?>
<tr><td><table>

    <form method="post" name="myform">

      <tr>
        <td>
          <input type='hidden' name='id' value='<?php echo $row['id']; ?>'>
        </td>

        <td>
         <?php echo $row['comment1']; ?>
        </td>

        <td>
          <input type='text' name='comment2' id='comment2' value=""/>
        </td>

        <td>
          <input type ='submit' name='save' id='save' value='Save' />
        </td>
      </tr>
    </form>

</table>
</td>
</tr>
      <?php
        }
      ?>
    </table>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM