在PHP中添加一个单选按钮并执行SQL命令


Add a Radio Button and Do an SQL Command in PHP

下午好。我只想问一个问题,但在此之前,让我以我所能做到的最好的方式向你们解释一下

到目前为止,我有ff。

Database: Election2016
Table: Candidate_Info
Fields: CandidateName and Position

到目前为止,这是我的代码,它的输出是在HTML表中显示数据

<html>
    <center>
        <font size="2" face = "century gothic">
        <?php
            $con = mysqli_connect("localhost", "root", "", "election2016");
            // Check connection
            if (mysqli_connect_errno()) {
                echo "Failed to connect to MySQL: " . mysqli_connect_error();
            }
            $result = mysqli_query($con, "SELECT * FROM candidate_info");
            echo "<table border='1'>
            <tr>
                <th>CandidateName</th>
                <th>Position</th>
            </tr>";
            while ($row = mysqli_fetch_array($result)) {
                echo "<tr>";
                echo "<td>" . $row['CandidateName'] . "</td>";
                echo "<td>" . $row['Position'] . "</td>";
                echo "</tr>";
            }
            echo "</table>";
            mysqli_close($con);
        ?>
    </center>
    </font>
</html>

我的目标是如何在上面安装一个单选按钮?旁边?在填充的每一行中附加一个单选按钮。

我的下一个目标是,当我按下一个按钮时,我用相应的单选按钮选择的数据将如何保存?

示例:

Candidate Name
Student 1
Position
President
Radio Button 1 (Example name of the Radio Button)

我选择了RadioButton1并按下"保存"按钮,学生1和校长将如何保存在表格中?

我希望你能理解TYIA

您应该为表Candidate_Info创建一个primary_key字段,作为CandidateId,并使用自动增量而非null。

添加单选按钮

while ($row = mysqli_fetch_array($result)) {
    echo "<tr>";
    echo "<td><input type='radio' name='candidateid' value='".$row['CandidateId']."' />" . $row['CandidateName'] . "</td>";
    echo "<td>" . $row['Position'] . "</td>";
    echo "</tr>";
}

当选择单选按钮并保存表格时,您应该将候选记录作为

从候选人信息中选择*CandidateId=[CcandidateId]

[CcandidateId]替换为所选单选按钮值。

执行上述查询后,您将获得所选的候选人信息,然后您可以保存它。