我正在尝试设置一个投票过程,其中有20组两个单选按钮可供您选择。然后,当单击“投票”按钮时,我可以收集所选按钮的值,并将+1添加到它们各自的数据库单元格中。
我是MySql的新手并且正在努力......
数据库表看起来像..
ID Park Votes
1 Zion 0
2 Grand Canyon 0
3 Arches 0
4 Canyonlands 0
5 Yosemite 0
6 Yellowstone 0
HTML表单
<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>
save.php文件
<?php
if (isset($_POST['match1'])){
$match1 = $_POST['match1'];
mysql_query("INSERT INTO results () VALUES ('1')");
}
?>
我对如何获取所选无线电并将+1应用于正确的表格单元格感到失望。提前致谢
答案 0 :(得分:1)
radio
输入的HTML不正确。如果您希望用户只选择一个停车场,则所有输入必须具有相同的名称。使用无线电输入的value
属性来存储公园的ID(来自MySQL表):
<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>
如果选择了一个项目,则该输入的选定值将返回$_POST['match']
,否则将不会设置$_POST['match']
。
因此,在您的save.php
页面上,您可以像这样更新您的表格(假设您的表名为“parks_table”):
<?php
if (isset($_POST['match'])) {
mysql_query( 'UPDATE `parks_table` SET Votes = Votes + 1 WHERE ID = '.$_POST['match'] );
}
?>
由于您似乎从PHP和MySQL开始,请阅读:Why shouldn't I use mysql_* functions in PHP?
答案 1 :(得分:-1)
<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);
?>