dousi4257 2019-07-17 07:57
浏览 67
已采纳

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

I'm trying to update the status to 'Approved' from pending by clicking the update button. However when i click on the button it does not respond and change the status. But if I hardcode the ID, the status is able to update. How do I get the ID capture from the database and update the status once click on the button?

//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';
}

?>

I expect the output to be able to update the status to 'Approved' into the database after clicking the update button.

  • 写回答

1条回答 默认 最新

  • dongwo5110 2019-07-17 08:11
    关注

    Try this:

    //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';
    }
    
    ?>
    

    Adding the input field as type=hidden and set the ID in the value of this field. Before update query, I have checked that the id is set or not if it's set only then run the update query otherwise do nothing. You may add else as well as per requirement.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图