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

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

图片转代码服务由CSDN问答提供 功能建议

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

id | 男性| 女性

1 | 5 | 3

表格:

 &lt; form method =“post”action =“&lt;?php echo $ _SERVER ['PHP_SELF'  ];?&gt;“&gt; 
 
&lt; fieldset&gt; 
&lt; legend&gt;性别&lt; / legend&gt; 
&lt; div&gt; 
&lt; input type =”radio“name =”gender“id =”male“value  =“1”/&gt;&lt; label for =“male”&gt;男&lt; / label&gt;&lt; br /&gt; 
&lt; input type =“radio”name =“gender”id =“female”value =“  1“/&gt;&lt; label for =”female“&gt;女性&lt; / label&gt;&lt; br /&gt; 
&lt; / div&gt; 
&lt; / fieldset&gt; 
&lt; fieldset&gt; 
&lt; div&gt; 
&lt;  ; label for =“submit”&gt;提交表单&lt; / label&gt; 
&lt; input type =“submit”name =“submit”id =“submit”value =“发送您的输入”/&gt; 
&lt; / div&gt  ; 
&lt; / form&gt; 
   
 
 

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条回答 默认 最新

  • 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);
    
    已采纳该答案
    打赏 评论
  • douli0531 2015-07-03 00:30

    you should make your radio button like this:

    <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 table SET $selected_radio = $selected_radio + 1 WHERE id = '1'"; $q = mysql_query($query);

    打赏 评论

相关推荐 更多相似问题