doufan8805 2015-12-24 12:13 采纳率: 0%
浏览 353
已采纳

单击按钮时,php mysql更新数据库

as the title states I am trying to write a code that will update a boolean data in column (I called 'status') for a specific row. I used while loop in table to display the rows of new registered and where the status is NULL, I've put two buttons (accept, reject) each in td so they'll be displayed to each name, What I want is when the accept button clicked, it sets the status of its row in the table to 1, and when reject is clicked, same thing but sets 0 instead of 1.

I've did a lot of research over this but hit a road block after road block, so I really hope your help in this, many thanks!

Here is my code:

<table id="sHold" style="border:none;">

<?php
    $conn = mysqli_connect('localhost', 'root', '', 'srs-db') or die('ERROR: Cannot Connect='.mysql_error($conn));

    function getStudent () {
        global $conn;
        $query = "SELECT * FROM student_table WHERE status IS NULL;";
        $result = mysqli_query($conn, $query);

        $i = 1;

        while ($row = mysqli_fetch_array($result)) {
            $sId = $row['student_id'];
            $sName = $row['student_name'];

            echo "<tr id='sNew".$i."'>";
            echo "<td>".$i." - </td>";
            echo "<td>$sId</td>";
            echo "<td>$sName</td>";
            echo "<td><button name='sAcc".$i."'>Accept</button></td>";
            echo "<td><button name='sRej".$i."'>Reject</button></td>";
            echo "</tr>";

            $i++;
        }
        if (isset($_POST['sAcc'.$i])) {
            $row['status'] = 1;
        }
    }

    getStudent();

?>

</table>
  • 写回答

2条回答 默认 最新

  • douyun7285 2015-12-24 12:22
    关注

    First of all, you miss <form> element. Your form inputs are useless without it, or without ajax.

    Secondly, your $_POST check will only check last item. Since after you exit loop $i is set to last value in the loop. So your example will only work on last item.

    <button> will now send $_POST with one of indexes sAcc or sRej. And it's value will be ID of your entry.

    <table id="sHold" style="border:none;">
    <form method="post" action="">
    <?php
        $conn = mysqli_connect('localhost', 'root', '', 'srs-db') or die('ERROR: Cannot Connect='.mysql_error($conn));
    
        function getStudent () {
            global $conn;
            $query = "SELECT * FROM student_table WHERE status IS NULL;";
            $result = mysqli_query($conn, $query);
    
            $i = 1;
    
            while ($row = mysqli_fetch_array($result)) {
                $sId = $row['student_id'];
                $sName = $row['student_name'];
    
                echo "<tr id='sNew".$i."'>";
                echo "<td>".$i." - </td>";
                echo "<td>{$sId}</td>";
                echo "<td>{$sName}</td>";
                echo "<td><button type='submit' name='sAcc' value='{$sId}'>Accept</button></td>";
                echo "<td><button type='submit' name='sRej' value='{$sId}'>Reject</button></td>";
                echo "</tr>";
    
                $i++;
            }
        }
    
        if (isset($_POST['sAcc']) && intval($_POST['sAcc'])) {
            $user_id = (int) $_POST['sAcc'];
    
            // Do the database update code to set Accept
        }
        if (isset($_POST['sRej']) && intval($_POST['sRej'])) {
            $user_id = (int) $_POST['sRej'];
    
    
            // Do the database update code to set Reject
        }
    
    
    
        getStudent();
    
    ?>
    </form> 
    </table>
    

    Tip: I assume you're beginner. I remade your code. But you dont need to put this code into function. Use functions to handle data retrieval for example. Dont use it to display html.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路