Bosc
Bosc

Reputation: 1509

Php update database column using radio button 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);

Upvotes: 0

Views: 1592

Answers (2)

Filipe Tagliacozzi
Filipe Tagliacozzi

Reputation: 1431

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

Upvotes: 1

chubzkittinz
chubzkittinz

Reputation: 9

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

Upvotes: 1

Related Questions