doufan8805
2015-12-24 12:13
浏览 240
已采纳

单击按钮时,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>

图片转代码服务由CSDN问答提供 功能建议

标题声明我正在尝试编写一个代码来更新列中的布尔数据(我称之为'状态' ')针对特定行。 我在表中使用while循环来显示新注册的行和状态为NULL的行,我在td中分别放了两个按钮(accept,reject),这样它们就会显示给每个名字,我想要的是什么时候 单击“接受”按钮,它将表中行的状态设置为1,单击拒绝时,同样但设置为0而不是1。

我做了很多 对此进行了研究,但在路障之后遇到了障碍,所以我非常希望你能帮上这个,非常感谢!

这是我的代码:

 &lt; table id =“sHold”style =“border:none;”&gt; 
 
&lt;?php 
 $ conn = mysqli_connect('localhost','root','','srs-  db')或die('错误:无法连接='。mysql_error($ conn)); 
 
函数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']; 
 $ s  Name = $ row ['student_name']; 
 
 echo“&lt; tr id ='sNew”。$ i。“'&gt;”; 
 echo“&lt; td&gt;”。$ i。“ - &lt  ; / td&gt;“; 
 echo”&lt; td&gt; $ sId&lt; / td&gt;“; 
 echo”&lt; td&gt; $ sName&lt; / td&gt;“; 
 echo”&lt; td&gt;&lt;按钮名称 ='sAcc“。$ i。”'&gt;接受&lt; / button&gt;&lt; / td&gt;“; 
 echo”&lt; td&gt;&lt; button name ='sRej“。$ i。”'&gt;拒绝&lt;  / button&gt;&lt; / td&gt;“; 
 echo”&lt; / tr&gt;“; 
 
 $ i ++; 
} 
 if(isset($ _ POST ['sAcc'。$ i])){  
 $ row ['status'] = 1; 
} 
} 
 
 getStudent(); 
 
?&gt; 
 
&lt; / table&gt; 
   
 
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

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.

    点赞 评论
  • drhgzx4727 2015-12-24 13:29
    <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");
    
    ?>
    
    点赞 评论

相关推荐 更多相似问题