简体   繁体   English

PHP的MySQL更新数据库上的按钮单击

[英]php mysql update database on button click

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. 我在表中使用while循环来显示新注册的行,并且状态为NULL,我在td中分别放置了两个按钮(接受,拒绝),以便将它们显示给每个名称,我想要的是单击“接受”按钮,它将表中其行的状态设置为1,单击“拒绝”时,将设置为0而不是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>

First of all, you miss <form> element. 首先,您会错过<form>元素。 Your form inputs are useless without it, or without ajax. 没有它或没有ajax,表单输入是无用的。

Secondly, your $_POST check will only check last item. 其次,您的$_POST支票将仅检查最后一项。 Since after you exit loop $i is set to last value in the loop. 由于退出循环后, $i被设置为循环中的最后一个值。 So your example will only work on last item. 因此,您的示例仅适用于最后一项。

<button> will now send $_POST with one of indexes sAcc or sRej . <button>现在将发送带有索引sAccsRej之一的$_POST And it's value will be ID of your entry. 它的值将是您的条目的ID。

<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. Tip:我认为您是新手。 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. 不要使用它来显示html。

<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='NULL'";
        $result = mysqli_query($conn, $query);

        $i = 1;

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

            echo "<tr id='".$sId."'>";
            echo "<td>".$i." - </td>";
            echo "<td>$sId</td>";
            echo "<td>$sName</td>";
            echo "<td><button name='sAcc' id='acc-".$sId."' onclick='approveuser(this.id)'>Accept</button></td>";
            echo "<td><button name='sRej' id='rec-".$sId."' onclick='approveuser(this.id)'>Reject</button></td>";
            echo "</tr>";

            $i++;
        }

    }

    getStudent();

?>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
function approveuser(id){

     trid=id.split('-')[1];
     //alert(trid);

    $.ajax({
        url: "update.php",
        type:"post",
        data:{ val : id },


        success: function(result){
            //alert(result);
            $('table#sHold tr#'+trid).remove();
            alert('Updated');

    }
    });
}
</script>
//The code give below this update.php pge(ajax page)
<?php
$data=$_POST['val'];
$status =explode('-',$data);
$user_id=$status[1];

if($status[0]=='acc'){
    $value=1;
}
elseif($status[0]=='rec'){
    $value=0;
}

  $conn = mysqli_connect('localhost', 'root', '', 'srs-db') or die('ERROR: Cannot Connect='.mysql_error($conn));
 mysqli_query($conn,"update student_table set status='$value' where student_id=$user_id");

?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM