如何使用单选按钮更新MySQL中的数据?

时间:2016-03-25 10:34:59

标签: php html mysql

大家好,下午好 我能问个问题吗?但在此之前,这是我的代码。

<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>Candidate Name</th>
    <th>Position</th>
    <th>Vote</th>
    <th>Number of Votes</th>
    </tr>";
    while ($row = mysqli_fetch_array($result)) {
        echo "<tr>";

        echo "<td>" . $row['CandidateName'] . "</td>";
        echo "<td>" . $row['Position'] . "</td>";
        echo "<td><input type='radio' name='candidateid'/>";
      echo "<td>" . $row['NumberofVotes'] . "</td>";
    }
    echo "</table>";
    mysqli_close($con);
    ?>

    <br>
    <br>
    <form method = "post" action = "<?php $_PHP_SELF ?>">
     <input name = "update" type = "submit" id = "update" value = "Update">
    </form>
    </center>
    </font>
    </html>
    <?php
             if(isset($_POST['update'])) {
                $dbhost = 'localhost';
                $dbuser = 'root';
                $dbpass = '';

                $conn = mysql_connect($dbhost, $dbuser, $dbpass);

                if(! $conn ) {
                   die('Could not connect: ' . mysql_error());
                }

                $candidateid = $row['candidateid'];


                $sql = "UPDATE candidate_info SET numberofvotes = '1' WHERE candidateid = $candidateid" ;
                mysql_select_db('election2016');
                $retval = mysql_query( $sql, $conn );

                if(! $retval ) {
                   die('Could not update data: ' . mysql_error());
                }
                echo "Updated data successfully\n";

                mysql_close($conn);
             }
    ?>

我的代码的输出在这里它将显示候选人,位置,单选按钮的投票数量列表,并保存按钮。

我的错误是,当我选择一个单选按钮并单击按钮更新时,我想在numberofvotes字段中输入1但不更新。我的代码怎么了?

任何帮助将不胜感激。 TY那么多

1 个答案:

答案 0 :(得分:0)

    <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>Candidate Name</th>
        <th>Position</th>
        <th>Vote</th>
        <th>Number of Votes</th>
        </tr>";
        while ($row = mysqli_fetch_array($result)) {
            echo "<tr>";

            echo "<td>" . $row['CandidateName'] . "</td>";
            echo "<td>" . $row['Position'] . "</td>";
             echo "<td><input type='radio' name='candidateid' value='".$row['candidateid']."' >";
          echo "<td>" . $row['NumberofVotes'] . "</td>";
    $candidateid=$row['candidateid'];
        }
        echo "</table>";
        mysqli_close($con);
        ?>

        <br>
        <br>
        <form method = "post" action = "<?php $_PHP_SELF ?>">
     <input type="hidden" name="candidateid" value="<?php echo $candidateid;?>">
         <input name = "update" type = "submit" id = "update" value = "update">
        </form>
        </center>
        </font>
        </html>
        <?php
                 if(isset($_POST['update'])) {
                    $dbhost = 'localhost';
                    $dbuser = 'root';
                    $dbpass = '';
 $candidateid = $_POST['candidateid'];
                    $conn = mysql_connect($dbhost, $dbuser, $dbpass);

                    if(! $conn ) {
                       die('Could not connect: ' . mysql_error());
                    }

                    $candidateid = $_POST['candidateid'];


                    $sql = "UPDATE candidate_info SET numberofvotes = 1 WHERE candidateid = '$candidateid'" ;
                    mysql_select_db('election2016');
                    $retval = mysql_query( $sql, $conn );

                    if(! $retval ) {
                       die('Could not update data: ' . mysql_error());
                    }
                    echo "Updated data successfully\n";

                    mysql_close($conn);
                 }
        ?>