I cannot understand why the radio button does not stay checked after submitting the right value to the database. I can see in the database the value being updated correctly but the corresponded radio button is empty. Could you help me?
<?php
session_start();
include 'connect_to_database.php';
$tbl_name="members"; // Table name
$db = connect_to_database();
$email = $_SESSION['email'];
// To protect MySQL injection (more detail about MySQL injection)
function add_value_to_members($db, $email, $key, $value)
{
$new_name=$_POST[$value];
$new_name = stripslashes($new_name);
$new_name = mysqli_real_escape_string($db, $new_name);
if (strlen($new_name) > 0){
$new_name_sql="UPDATE members SET $key='$new_name' WHERE email='$email'";
$result=mysqli_query($db, $new_name_sql);
echo $result;
}
}
add_value_to_members($db, $email, "gender", "new_gender");
header("Location: ../edit_user.php");
exit();
// html
<div class="form-group">
<label class="col-lg-3 control-label">Gender</label>
<div class="col-lg-8">
<div class="radio">
<label>
<input type="radio" name="new_gender" value="male" <?php if (isset($_POST['gender']) && $_POST['gender'] == "male") echo "checked"; ?>> Male
</label>
</div>
<div class="radio">
<label>
<input type="radio" name="new_gender" value="female" <?php if(isset($_POST['gender']) && $_POST['gender']=="female" ) echo "checked"; ?>> Female
</label>
</div>
</div>
</div>
Where is your form tag? Anyway i think it has to do with sending a header wich redirects, and doesnt send a post, if this is what you need you could place the radio button value into a session variable right after updating to your database and check in your radio button tag for the session value or you could use a get paramater wich gets sended into the header url