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

时间:2015-07-03 00:09:22

标签: php mysql database increment

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

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

从环顾四周,我一直试图使用这样的东西,但似乎无法将已检查的无线电值拉入。想想我从表格中遗漏了一些内容,告诉我们检查了哪个单选按钮并在提交时发布

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

2 个答案:

答案 0 :(得分:1)

你必须通过$ _POST得到$ selected_radio而不是$ _GET ... BTW里面$ _POST ['性别'],提交后,你的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);

答案 1 :(得分:1)

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

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

$ selected_radio = isset($ _ POST ['gender']);

$ query =“UPDATE表SET $ selected_radio = $ selected_radio + 1 WHERE id ='1'”; $ q = mysql_query($ query);