doudeng2025 2015-07-03 00:09
浏览 42
已采纳

Php使用单选按钮id更新数据库列

Trying to make a simple database and form to collect data. I am trying to increment the value in the tables column but dont know how to tell the sql to update the column based off of the id value from the radio buttons. Any help would be great.

id | male | female

1 | 5 | 3

form:

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">

<fieldset>
<legend>Gender</legend>
<div>
<input type="radio" name="gender" id="male" value="1" /><label for="male">Male</label><br />
<input type="radio" name="gender" id="female" value="1" /><label for="female">Female</label><br />
</div>
</fieldset>
<fieldset>
<div>
<label for="submit">Submit the form</label>
<input type="submit" name="submit" id="submit" value="Send your Input" />
</div>
</form>

Php database update:

$query = "UPDATE table SET x = x + 1 WHERE id = '1'";
$q = mysql_query($query);

From looking around ive been trying to use something like this but cant seem to get the checked radio value to be pulled in. Think I am missing something from the form to tell what radio button has been checked and to post that on submit.

$selected_radio = $_GET['id'];
$query = "UPDATE table SET $selected_radio = $selected_radio + 1 WHERE id = '1'";
$q = mysql_query($query);
  • 写回答

2条回答 默认 最新

  • douji6667 2015-07-03 00:28
    关注

    You will have to get $selected_radio by $_POST and not $_GET... BTW inside $_POST['gender'], after submit, your PHP script should receive the value defined in "value" attribute from input radio so.. change the values:

    <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    
    <fieldset>
    <legend>Gender</legend>
    <div>
    <input type="radio" name="gender" id="male" value="5" /><label for="male">Male</label><br />
    <input type="radio" name="gender" id="female" value="3" /><label for="female">Female</label><br />
    </div>
    </fieldset>
    <fieldset>
    <div>
    <label for="submit">Submit the form</label>
    <input type="submit" name="submit" id="submit" value="Send your Input" />
    </div>
    </form>
    
    /*Then, you could increment your x column where the row id = 5 (male) or 3 (female)*/
    
    $selected_radio = $_POST['gender'];
    $query = "UPDATE table SET x = x + 1 WHERE id = '" .  $selected_radio . "'";
    $q = mysql_query($query);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集