duanliang8464
2014-12-10 13:01
浏览 55
已采纳

如何在从数据库检索的表中使用单选按钮并将值存储在另一个表中

i am trying to make an attendance chart in php. i wish to increment the counter of attendance in the database on click of a radio button. how do i retrieve the value and then increment for every "yes"?

do i need to put the updation code in the next page?

i tried it that way..but the fields are not getting updated..

if($class=="cse")
    {
        $result = mysqli_query($con, "SELECT * FROM cse");
        echo "<table border=15px cellspacing=40>";
        echo "<tr><th>NAME</th><th>BUPIN</th><th>ATTENDANCE</th></tr>";
        while ($row = mysqli_fetch_array($result)) 
    {
        echo "<tr><td>" . $row["name"] . "</td>" . "<td>" . $row["bupin"] . "</td>";
        echo "<td>" . "<input type='radio' name='presence[".$row["bupin"]."]' value='yes'>Yes";
        echo "<input type='radio' name='presence[".$row["bupin"]."]' value='no'>No" . "</td></tr>";
    }
    echo "</table>";
    ?>
    <html>
    <form action= "final6.php" method="POST">
    <input type="Submit" value="Submit">
    </form>
    </html>
    }}

and the next page code is as:if (isset($_POST["presence"])) { foreach ($presence as $key => $val) { if ($val == 'yes') { $sql = "UPDATE cse SET 1attendance = 1attendance + 1 WHERE bupin = " .$key; msyqli_query($con, $sql); } } } echo "Attendance marked successfully.."; }

where am i wrong?

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongyanzhui0524 2014-12-10 13:08
    已采纳

    If the bupin the primary key (id) in your table cse, then try this. Of course, change the field in the query to your field, what you want to incrase.

    $result = mysqli_query($con, "SELECT * FROM cse");
    echo "<table border=15px cellspacing=40>";
    echo "<tr><th>NAME</th><th>BUPIN</th><th>ATTENDANCE</th></tr>";
    while ($row = mysqli_fetch_array($result)) {
        echo "<tr><td>" . $row["name"] . "</td>" . "<td>" . $row["bupin"] . "</td>";
        echo "<td>" . "<input type='radio' name='presence[".$row["bupin"]."]' value='yes'>Yes";
        echo "<input type='radio' name='presence[".$row["bupin"]."]' value='no'>No" . "</td></tr>";
    }
    echo "</table>";
    
    
    if (isset($_POST["presence"])) {
        foreach ($presence as $key => $val) {
            if ($val == 'yes') {
                $sql = "UPDATE cse SET field = field + 1 WHERE bupin = " . $key;
                msyqli_query($con, $sql);
            }
        }
    }
    
    已采纳该答案
    打赏 评论

相关推荐 更多相似问题