1

Good Afternoon. I just want to ask a Question but before that let me explain it to all of you in a best way I can

As of now I have the ff.

Database: Election2016
Table: Candidate_Info
Fields: CandidateName and Position

As of now here is my code and the output of this is Show the Data in HTML Table

<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>

And my target here is how can I attach a Radio Button to it? next to it? Attach a Radio Button in every row populated.

and my next target here which is very tricky to me is that when I press a button how will the data i choose with the corresponding radiobutton will be saved?

Example:

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

I selected RadioButton1 and press button "Save" how will Student 1 and President will be saved in a table?

I hope you understand TYIA

mitkosoft
  • 5,262
  • 1
  • 13
  • 31

1 Answers1

0

You should create one primary_key field to the table Candidate_Info as CandidateId with Auto increment and not null.

To add radio button

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>";
}

while select the radio button and saving the form you should get the candidate record as

SELECT * FROM candidate_info WHERE CandidateId=[CandidateId]

replace the [CandidateId] with the selected radio button value.

After executing the above query you will be getting the selected candidate info, then you can save it.

Thiyagesan
  • 75
  • 6