简体   繁体   中英

Use Radio Buttons to Increase a value in MySql Database

I am trying to set up a voting process where there are 20 sets of two radio buttons you can select from. Then when a "vote" button is clicked I can collect the values of the selected buttons and add +1 to their respective database cells.

I am new to MySql and am struggling...

The database Table looks like..

ID      Park          Votes
1       Zion          0
2       Grand Canyon  0
3       Arches        0
4       Canyonlands   0
5       Yosemite      0
6       Yellowstone   0

The HTML form

<form action="/databases/save.php" method="post">
    <input type="radio" name="match1" value="1" />Zion
    <input type="radio" name="match1" value="1" />Grand Canyon
    <input type="radio" name="match2" value="1" />Arches
    <input type="radio" name="match2" value="1" />Canyonlands
    <input type="radio" name="match3" value="1" />Yosemite
    <input type="radio" name="match3" value="1" />Yellowstone
    <input type="image" src="/graphics/logo.png" />
</form>

And the save.php file

<?php
    if (isset($_POST['match1'])){
        $match1 = $_POST['match1'];
        mysql_query("INSERT INTO results () VALUES ('1')");
    }
    ?>

I am getting thrown off by how to get the selected radio and apply a +1 to the correct table cell. Thanks in advance

Your HTML is incorrect for radio inputs. If you want the user to select only one park, all inputs must have the same name. Use the value attribute of the radio input to store the ID of the park (from the MySQL table):

<form action="/databases/save.php" method="post">
  <input type="radio" name="match" value="1" />Zion
  <input type="radio" name="match" value="2" />Grand Canyon
  <input type="radio" name="match" value="3" />Arches
  <input type="radio" name="match" value="4" />Canyonlands
  <input type="radio" name="match" value="5" />Yosemite
  <input type="radio" name="match" value="6" />Yellowstone
  <input type="image" src="/graphics/logo.png" />
</form>

The selected value for that input will be returned as $_POST['match'] if one item was selected or else $_POST['match'] will not be set at all.

So on your save.php page you can update your table like this (assuming your table is named "parks_table"):

<?php
if (isset($_POST['match'])) {
  mysql_query( 'UPDATE `parks_table` SET Votes = Votes + 1 WHERE ID = '.$_POST['match'] );
}
?>

Since you seem to be starting with PHP and MySQL please read: Why shouldn't I use mysql_* functions in PHP?

    <button type="submit" name="vote" id="vote" value=" ' .  $row['candidate_position'] . ' "  class="btn btn-success">CAST VOTE</button>
     <?php

$vote = $_POST['vote'];

if (isset($_POST['vote'])) {
 mysqli_query($con, "UPDATE tbCandidates SET candidate_votes=candidate_votes+1 WHERE  position_id='$vote'");
 echo "string";
}


mysqli_close($con);
?> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM