1

I've currently got a prepared statement that puts the data from an HTML form through a Prepared Statement (PHP) into a SQL database.

The problem here; one field remains empty after putting it into the DB. The 'gender' field, which is an ENUM(M,F,O). The rest gets filled up with the right data.

Is there any way for me to get the gender into the table along with the other data?

The relevant HTML:

    <input type="radio" id="man" name="gender" value="man">
    <label for="man">Man</label>

    <input type="radio" id="woman" name="gender" value="woman">
    <label for="woman">Woman</label>

    <input type="radio" id="other" name="gender" value="other">
    <label for="other">Other</label>

The PHP:

$firstname= htmlentities($_POST['firstname'], ENT_QUOTES);
$lastname= htmlentities($_POST['lastname'], ENT_QUOTES);
$gender=  htmlentities($_POST['gender'], ENT_QUOTES);
$birthdate= preg_replace("([^0-9-])", "", $_POST['birthday'], ENT_QUOTES);
$email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);
$portfolio = filter_var($_POST['portfolio'], FILTER_SANITIZE_URL);

$query = "INSERT INTO `table` (`firstname`, `lastname`, `gender`, `birthdate`, `email`, `portfolio`)
VALUES (?, ?, ?, ?, ?, ?)";

mysqli_stmt_bind_param($stmt, "ssssss", $firstname, $lastname, $gender, $birthdate, $email, $portfolio);

mysqli_stmt_execute($stmt);
$prepstmt = mysqli_stmt_get_result($stmt);
2
  • 4
    Don't use htmlentities() when saving into the DB. That should only be used when displaying text on a web page. Commented Apr 22, 2021 at 23:28
  • What have you tried to resolve the problem? Where are you stuck? Have you even checked whether you are sending the proper data? Commented Apr 23, 2021 at 12:18

1 Answer 1

1

Change the values of the radio buttons to match the ENUM values.

<input type="radio" id="man" name="gender" value="M">
<label for="man">Man</label>

<input type="radio" id="woman" name="gender" value="F">
<label for="woman">Woman</label>

<input type="radio" id="other" name="gender" value="O">
<label for="other">Other</label>

If you can't change the form, you can map the values in PHP:

$gender_map = ['man' => 'M', 'woman' => 'F', 'other' => 'O'];
$gender = $gender_map[$_POST['gender']];
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.