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

Php update database column using radio button id

尝试制作一个简单的数据库和表格来收集数据。我正在尝试增加表列中的值,但不知道如何告诉 sql 根据单选按钮的 id 值更新列。任何帮助都会很棒。

编号 |男性 |女性

1 | 5 | 3

形式:

<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 数据库更新:

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

环顾四周,我一直在尝试使用类似的东西,但似乎无法获取要拉入的选中单选值。我想我从表单中遗漏了一些东西来告诉单选按钮已被选中并 post 提交时。

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

您必须通过 $_POST 而不是 $_GET 获取 $selected_radio...顺便说一句,在 $_POST['gender'] 中,提交后,您的 PHP 脚本应该会收到输入收音机的 "value" 属性中定义的值,因此..更改值:

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

你应该像这样制作你的单选按钮:

<input type="radio" name="gender" id="gender" value="female" />
<input type="radio" name="gender" id="gender" value="male" />

$selected_radio = isset($_POST['gender']);

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