如何更新具有唯一ID的表行数据? [英] how to update table row data with unique id?

查看:73
本文介绍了如何更新具有唯一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.在下图中,您看到具有保存按钮的表行仅是一行,类似地,它具有多行,每行中都有保存按钮.现在,我希望当我单击特定行的保存按钮时,仅该行数据将被更新.我该如何解决这个问题?请帮忙.

In this code I want to update table enquires2 with unique id. In following image you see that table row having save button this is only one row similarly it have multiple row which having save button in each row. Now I want that when I click on save button of particular row only that row data will be update. How can I fix this problem ? Please help.

谢谢

推荐答案

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

First of all, for security reason you need to change this query to a prepared statement see 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的开头报价.


This line is bad anyway, you are missing a opening quote for $comment2.

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


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


Are you sure $link is an actual mysqli link?

至于html部分,您需要为每条记录使用一种形式.请参阅发布的链接

As for the html part, you need to mkae one form for each record. See the link posted HTML: Is it possible to have a FORM tag in each TABLE ROW in a XHTML valid way?

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

alternatively you could do something bad like only adding the $id to evry field for every row (similar to:)

<input type ='submit' name='save[<?=$id;?>]' id='save' value='Save' />

,并在php代码中检查是否设置了女巫键.

and in the php code check witch key is set.

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

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

You will need to replicate the bad thing for your comments as well but as a proof of concept you can run this snippet on 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注入不利而且不可接受之外,还有一个意见问题.

Wish i could provide you a really full answer but there's alot of work to be done on your code for it to be 'proper coding'. Again this becaome a matter of opinion beside the fact that your code is vunerable to sql injection and is NOT accepable.

这篇关于如何更新具有唯一ID的表行数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆