Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/272.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用while打印数据时更新sql中的值_Php_Button_While Loop - Fatal编程技术网

Php 使用while打印数据时更新sql中的值

Php 使用while打印数据时更新sql中的值,php,button,while-loop,Php,Button,While Loop,使用表单是为了在每次单击按钮时都可以使用方法post,有关更多信息,请查看代码 <form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="post"> <?php //selecting rowa where the approve status is false or simple "0" $result = mysqli_que

使用表单是为了在每次单击按钮时都可以使用方法post,有关更多信息,请查看代码

<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="post">
    <?php
        //selecting rowa where the approve status is false or simple "0"
        $result = mysqli_query($db,"SELECT id, rid, time, amount, transid, wallet FROM donations WHERE approve = 0");
        //this button will be used later update the rows and set approve to true or "1"
        $button = "<button type='submit' name='approve' class='btn btn-success' role='button'>" . "APPROVE" . "</button>";
        echo "<table class='table'>
            <tr>
                <th>ID</th>
                <th>RID</th>
                <th>TIME</th>
                <th>AMOUNT</th>
                <th>TRANS HASH</th>
                <th>WALLET</th>
                <th>APPROVE</th>
            </tr>";
        //printing all the values where approve = '0'
        while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
            echo "<tr>";
            echo "<td class='col-sm-1'>" . $row['id'] . "</td>";
            echo "<td class='col-sm-1'>" . $row['rid'] . "</td>";
            echo "<td class='col-sm-2'>" . $row['time'] . "</td>";
            echo "<td class='col-sm-1'>" . $row['amount'] . "</td>";
            echo "<td class='col-sm-3'>" . $row['transid'] . "</td>";
            echo "<td class='col-sm-3'>" . $row['wallet'] . "</td>";
            echo "<td class='col-sm-1'>" . $button . "</td>";
            echo "</tr>";
        }echo "</table>";
        //when the button is clicked, the method 'post' is invoked and the value is updated
        if($_SERVER["REQUEST_METHOD"] == "POST"){
            $id = $row['id'];
            $approve = mysqli_query($db,"UPDATE `donations` SET approve = 1 WHERE approve = 0 and id = '$id'");
        }
    ?>

1st:更改代码顺序。更新部分应该在页面顶部。因为若它是提交的,你们需要更新。那个么你们只需要从数据库中获取数据。按照您的顺序获取数据并显示给用户,然后最后更新。不应该是这样

2nd:只需像这样将
行id
作为
提交
按钮
值传递,它应该在while循环中

$button = "<button type='submit' name='approve' value='".$row["id"]."' class='btn btn-success' role='button'>" . "APPROVE" . "</button>";
注:以上代码应在页面顶部

更新1:代码顺序应如下所示

<?php

        //when the button is clicked, the method 'post' is invoked and the value is updated
        if($_SERVER["REQUEST_METHOD"] == "POST"){
            $id = $_POST['approve'];
            $approve = mysqli_query($db,"UPDATE `donations` SET approve = 1 WHERE approve = 0 and id =$id");
        }
?>
    <form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="post">
    <?php

        //selecting rowa where the approve status is false or simple "0"
        $result = mysqli_query($db,"SELECT id, rid, time, amount, transid, wallet FROM donations WHERE approve = 0");
        //this button will be used later update the rows and set approve to true or "1"

        echo "<table class='table'>
            <tr>
                <th>ID</th>
                <th>RID</th>
                <th>TIME</th>
                <th>AMOUNT</th>
                <th>TRANS HASH</th>
                <th>WALLET</th>
                <th>APPROVE</th>
            </tr>";
        //printing all the values where approve = '0'
        while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){

        $button = "<button type='submit' name='approve' value='".$row["id"]."' class='btn btn-success' role='button'>" . "APPROVE" . "</button>";

            echo "<tr>";
            echo "<td class='col-sm-1'>" . $row['id'] . "</td>";
            echo "<td class='col-sm-1'>" . $row['rid'] . "</td>";
            echo "<td class='col-sm-2'>" . $row['time'] . "</td>";
            echo "<td class='col-sm-1'>" . $row['amount'] . "</td>";
            echo "<td class='col-sm-3'>" . $row['transid'] . "</td>";
            echo "<td class='col-sm-3'>" . $row['wallet'] . "</td>";
            echo "<td class='col-sm-1'>" . $button . "</td>";
            echo "</tr>";
        }echo "</table>";

    ?>


然后,您必须使用javascript将数据AJAX到PHP,或者将您的按钮更改为包含键的查询字符串的锚定标记“prepared statement”是什么意思?仍然没有得到任何信息,在加载页面时显示错误消息。“未定义索引:id”。错误出现在按钮行,我们将$row['id']分配给按钮。将$button放入while循环并更改撇号后,效果良好。谢谢@jyothif我的答案有用用绿色勾号标记。这对将来的用户参考很有用@RaviGohelI did,我还有一个问题要问,如果我想更新超过1个值,我该怎么办?
<?php

        //when the button is clicked, the method 'post' is invoked and the value is updated
        if($_SERVER["REQUEST_METHOD"] == "POST"){
            $id = $_POST['approve'];
            $approve = mysqli_query($db,"UPDATE `donations` SET approve = 1 WHERE approve = 0 and id =$id");
        }
?>
    <form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="post">
    <?php

        //selecting rowa where the approve status is false or simple "0"
        $result = mysqli_query($db,"SELECT id, rid, time, amount, transid, wallet FROM donations WHERE approve = 0");
        //this button will be used later update the rows and set approve to true or "1"

        echo "<table class='table'>
            <tr>
                <th>ID</th>
                <th>RID</th>
                <th>TIME</th>
                <th>AMOUNT</th>
                <th>TRANS HASH</th>
                <th>WALLET</th>
                <th>APPROVE</th>
            </tr>";
        //printing all the values where approve = '0'
        while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){

        $button = "<button type='submit' name='approve' value='".$row["id"]."' class='btn btn-success' role='button'>" . "APPROVE" . "</button>";

            echo "<tr>";
            echo "<td class='col-sm-1'>" . $row['id'] . "</td>";
            echo "<td class='col-sm-1'>" . $row['rid'] . "</td>";
            echo "<td class='col-sm-2'>" . $row['time'] . "</td>";
            echo "<td class='col-sm-1'>" . $row['amount'] . "</td>";
            echo "<td class='col-sm-3'>" . $row['transid'] . "</td>";
            echo "<td class='col-sm-3'>" . $row['wallet'] . "</td>";
            echo "<td class='col-sm-1'>" . $button . "</td>";
            echo "</tr>";
        }echo "</table>";

    ?>