如何从数据库中捕获ID以更新状态?

时间:2019-07-17 07:57:34

标签: php html

我正在尝试通过单击更新按钮将状态从待定更新为“已批准”。但是,当我单击按钮时,它不会响应并更改状态。但是,如果我对ID进行硬编码,则状态可以更新。单击按钮后,如何从数据库获取ID捕获并更新状态?

//edit1.php:
<?php

    $con= mysqli_connect('127.0.0.1','root','');

    if(!$con)
    {
        echo 'Not Connected To Server';
    }

    if(!mysqli_select_db($con,'satsform1'))
    {
        echo 'Database Not Selected';
    }

    $ID = 'ID';

    $sql = "update handover set status= 'Approved' where ID = '$ID'";

    if(!mysqli_query($con,$sql))
    {
        echo 'Not Submitted';
    }
    else
    {
        echo "<meta http-equiv='refresh' content='0;url=index3.php'>";
    }

?>

<?php
//fetch3.php
$connect = mysqli_connect('127.0.0.1','root','', 'satsform1');
$output = '';
if(isset($_POST["query"]))
{
 $search = mysqli_real_escape_string($connect, $_POST["query"]);
 $query = "
  SELECT * FROM handover 
  WHERE name LIKE '%".$search."%'
  OR staffno LIKE '%".$search."%' 
  OR date LIKE '%".$search."%'
  OR email LIKE '%".$search."%'
  OR mobno LIKE '%".$search."%'
  OR roles LIKE '%".$search."%'
  OR location LIKE '%".$search."%'
  OR flightno LIKE '%".$search."%'
  OR flightdate LIKE '%".$search."%'
  OR seatno LIKE '%".$search."%'
  OR class LIKE '%".$search."%'
 ";
}
else
{
 $query = "
  SELECT * FROM handover ORDER BY ID
 ";
}
$result = mysqli_query($connect, $query);
if(mysqli_num_rows($result) > 0)
{
 $output .= '
  <div class="table-responsive">
   <table class="table table bordered">
    <tr>
     <th>ID</th>
     <th>Staff Name</th>
     <th>Staff Number</th>
     <th> Status </th>
    </tr>
 ';
 while($row = mysqli_fetch_array($result))
 {
  $output .= '
   <tr>
    <td>'.$row["ID"].'</td>
    <td>'.$row["name"].'</td>
    <td>'.$row["staffno"].'</td>

    <td>'.$row["status"].'</td>

    // this is the update button where use the edit1.php to update the status by ID
    <td> <form action="edit1.php" method="GET">
            <button type="submit">update </button>

        </form>
    </td>

   </tr> 
  ';
 }
 echo $output;
}
else
{
 echo 'Data Not Found';
}

?>

我希望在单击“更新”按钮后,输出结果能够将状态更新为“已批准”。

2 个答案:

答案 0 :(得分:0)

一种方法是将ID放在实际按钮中。

因此您可以像这样将其添加到value属性中。这是主意:

$output .= '
<tr>
    <td>'.$row["ID"].'</td>
    <td>'.$row["name"].'</td>
    <td>'.$row["staffno"].'</td>
    <td>'.$row["status"].'</td>


    <td>
        <form action="edit1.php" method="GET">
            <button type="submit" name="approve" value="' . $row['ID'] . '">Approve</button>
        </form>
    </td>
</tr> 
';

因此,在edit1.php中,您可以传输和重复使用ID:

if (!empty($_GET['approve'])) {
    $id = (int) $_GET['approve'];
    $sql = "UPDATE handover SET `status` = 'Approved' WHERE ID = ?";
    $stmt = $con->prepare($sql);
    $stmt->bind_param('i', $id);
    $stmt->execute();
    header('Location: fetch3.php'); exit;
}

旁注:您也可以为此使用<input type="hidden。只要它具有相同的名称,并且可以像访问其他PHP页面中的任何值一样对其进行访问,则它将起作用。

如果您不喜欢按钮的想法,可以利用锚点:

$output .= '
<tr>
    <td>'.$row["ID"].'</td>
    <td>'.$row["name"].'</td>
    <td>'.$row["staffno"].'</td>
    <td>'.$row["status"].'</td>


    <td>
       <a href="edit1.php?approve=' . $row['ID'] . '">Approve</a>
    </td>
</tr> 
';

它也将起作用。

答案 1 :(得分:0)

尝试一下:

//edit1.php:
<?php

    $con= mysqli_connect('127.0.0.1','root','');

    if(!$con) {
        echo 'Not Connected To Server';
    }

    if(!mysqli_select_db($con,'satsform1')) {
        echo 'Database Not Selected';
    }

    if ( isset( $_GET['update_id'] ) && is_numeric( $_GET['update_id'] ) ) {
        $ID = $_GET['update_id'];

        $sql = "update handover set status= 'Approved' where ID = '$ID'";

        if(!mysqli_query($con,$sql)) {
            echo 'Not Submitted';
        }
        else {
            echo "<meta http-equiv='refresh' content='0;url=index3.php'>";
        }
    }

?>

<?php
//fetch3.php
$connect = mysqli_connect('127.0.0.1','root','', 'satsform1');
$output = '';
if(isset($_POST["query"])) {
    $search = mysqli_real_escape_string($connect, $_POST["query"]);
    $query = "
        SELECT * FROM handover
        WHERE name LIKE '%".$search."%'
        OR staffno LIKE '%".$search."%'
        OR date LIKE '%".$search."%'
        OR email LIKE '%".$search."%'
        OR mobno LIKE '%".$search."%'
        OR roles LIKE '%".$search."%'
        OR location LIKE '%".$search."%'
        OR flightno LIKE '%".$search."%'
        OR flightdate LIKE '%".$search."%'
        OR seatno LIKE '%".$search."%'
        OR class LIKE '%".$search."%'
    ";
}
else {
    $query = "
        SELECT * FROM handover ORDER BY ID
    ";
}
$result = mysqli_query($connect, $query);
if(mysqli_num_rows($result) > 0) {
    $output .= '
        <div class="table-responsive">
            <table class="table table bordered">
                <tr>
                <th>ID</th>
                <th>Staff Name</th>
                <th>Staff Number</th>
                <th> Status </th>
                </tr>
    ';
    while($row = mysqli_fetch_array($result)) {
        $output .= '
            <tr>
                <td>'.$row["ID"].'</td>
                <td>'.$row["name"].'</td>
                <td>'.$row["staffno"].'</td>

                <td>'.$row["status"].'</td>

                // this is the update button where use the edit1.php to update the status by ID
                <td>
                    <form action="edit1.php" method="GET">
                        <input type="hidden" name="update_id" value="'.$row["ID"].'">
                        <button type="submit">update </button>
                  </form>
                </td>

            </tr>
        ';
    }
    echo $output;
}
else {
    echo 'Data Not Found';
}

?>

将输入字段添加为type=hidden,并在此字段的值中设置ID。在更新查询之前,我已经检查了是否设置了id(如果仅设置了它),然后运行update查询,否则什么也不做。您可以根据需要添加else