我在每一行上都批准了按钮,单击后,它应该更新数据库中该特定行的批准状态

时间:2019-05-05 04:37:47

标签: php

我是displaying一张桌子,桌子的每一行都有一个approve并拒绝button,在单击“批准”后,状态应为reflect to database as approved

我尝试过,但是单击任何行的批准按钮后,它将status设置为所有行的批准。

我的PHP代码是:

$doc_re = "select * from files where receiver='$user'";
$record = mysqli_query($conn,$doc_re);

if($record)
{
    echo " <br><center><span class='badge badge-light' style='font-size:30px; background-color:teal;color:white;padding:10px;'>RECEIVED RECORDS</span></center>";
    echo "<br><table class='table' border='3' style='background-color:rgba(2,2,2,0.7);'>";
    echo "<thead class='thead-dark' >";
    echo "<tr style='font-size:23px;'><th style='color:skyblue;'><center>TO</center></th>
          <th style='color:skyblue;'><center>FROM</center></th>
          <th style='color:skyblue;'><center>FILE</center></th>
          <th style='color:skyblue;'><center>MESSAGE</center></th>
          <th style='color:skyblue;'><center>APPROVE</center></th>
          <th style='color:skyblue;'><center>REJECT</center></th>
          <th style='color:skyblue;'><center>STATUS</center></th></tr></thead>";

    while($r = mysqli_fetch_array($record,MYSQLI_ASSOC))
    {
        echo "<tr><td style='color:white; font-size:18px;'><center><strong>{$r['receiver']}</strong></center></td>
            <td style='color:white; font-size:18px;'><center><strong>{$r['sender']}</strong></center></td>
            <td style='color:white; font-size:18px;'><center><strong><a href='".$r['file']."'>{$r['file']}</a></strong></center></td>
            <td style='color:white; font-size:18px;'><center><strong>{$r['message']}</strong></center></td>
            <form method='POST'>
            <td><center>
            <button class='button button1' name='approve'  type='submit'><span>&#10003;</span></button></center></td>
            <td><center><button class='button button1' style='background-color:red;' name='reject' type='submit'><span>&#10008;</span></button></center></td>
            </form>
            <td style='color:white; font-size:18px;'><center><strong>{$r['Status']}</strong></center></td>
        </tr>";

        if(isset($_POST['approve']))
        {   
            $q1 = "update files set Status='APPROVED' where receiver='{$r['receiver']}' and sender='{$r['sender']}' ";
            $res = mysqli_query($conn,$q1);
        }
        elseif(isset($_POST['reject']) )
        {
            $q2 = "update files set Status='REJECTED' where receiver='{$r['receiver']}' and sender='{$r['sender']}' ";
            $ress = mysqli_query($conn,$q2);
        }   

    }
    echo "</table>";
} 

将所有行的状态设置为已批准

2 个答案:

答案 0 :(得分:0)

您拥有row的{​​{1}}可能有selected,因此您可以使用此id传递给查询,并id传递给您的{{ 1}}如下所示批准或拒绝:

set

在上面的代码中,status这是重要的行,在这里您将所选行的 <td><center> <button class='button button1' name='approve' type='submit'><a href="yourphp.page?status=approved&id=<?php echo $r['id'];?>"><span>&#10003;</span></a></button></center></td> <td><center><button class='button button1' style='background-color:red;' name='reject' type='submit'><a href="yourphp.page?status=reject&id=<?php echo $r['id'];?>"><span>&#10008;</span></a></button></center></td> yourphp.page?status=reject&id=<?php echo $r['id'];?>传递给某些status,在这里我也假设id可能是您表中的yourphp.page。 现在,要传递价值,您可以像下面这样写:

$r['id']

通过了id中的值,如下所示:

$status=$_GET['status'];//will give you status
$id=$_GET['id'];//will give you id of row selected

以上查询将使用query更新该特定行。然后在执行查询后,您可以 $q1 = "update files set Status='$status' where id=$id"; $res = mysqli_query($conn,$q1); 到同一页..您的表所在的位置。希望这会有所帮助!

注意 :另外,尝试使用prepared statement是安全的。

答案 1 :(得分:0)

我认为您的查询有问题。它没有在数据库的表文件中定位正确的值。应该是这样的:

if(isset($_POST['approve'])) 
  $q = "update files set Status='APPROVED' where receiver='{$r['receiver']}' and sender='{$r['sender']}' and file='{$r['file']}'";
elseif(isset($_POST['reject']))
  $q = "update files set Status='REJECTED' where receiver='{$r['receiver']}' and sender='{$r['sender']}' and file='{$r['file']}'";

$res = mysqli_query($conn,$q);