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 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c