Skip to content
Advertisement

i created a php form and connects in to mysql my all data is added in database only gender is not showing in the database

i have created a php form and connected it to mysql successfully, i created a table in mysql and added the data, all data is added except gender is not showing in database instead of gender 0 is displayed in the table.Second probelm is my validation is not working on the form Below is the code: Here is the code for database connection and validation:

<?php
session_start();
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$mail = $_POST['mail'];
$password = $_POST['password'];
$age = $_POST['age'];
$gender = $_POST['gender'];

//database Connection



$conn = new mysqli('localhost', 'root', '', 'codetodesign');

// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
} else {
$stmt = $conn->prepare("insert into registration(firstName, lastName, mail, password, age, gender) 
values(?, ?, ?, ?, ?, ?)");
    $stmt->bind_param("sssssi",$firstname, $lastname, $mail, $password, $age, $gender);
    $stmt->execute();
echo "Registration successfully";
$stmt->close();
$conn->close();
}

//Validations

$firstnameErr = $lastnameErr = $mailErr = $passwordErr = $ageErr = $genderErr = "";
$firstname = $lastname = $mail = $password = $age = $gender = "";

if ($_SERVER["REQUEST_METHOD"] == "POST"){
if (empty($_POST["firstname"])){
    $firstnameErr = "Firstname is required";
} else {
    $firstname = test_input($_POST["firstname"]);
}

if (empty($_POST["lastname"])){
    $lastnameErr = "lastname is required";
} else {
    $lastname = test_input($_POST["lastname"]);
}

if (empty($_POST["mail"])){
    $mailErr = "Mail is required";
} else {
    $mail = test_input($_POST["mail"]);
}

if (empty($_POST["password"])){
    $passwordErr = "Password is required";
} else {
    $password = test_input($_POST["password"]);
}

if (empty($_POST["age"])){
    $ageErr = "Age is required";
} else {
    $age = test_input($_POST["age"]);
}

if (empty($_POST["gender"])){
    $genderErr = "Gender is required";
} else {
    $gender = test_input($_POST["gender"]);
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>

Here is the code for gender values:

<div class="form-group">
            <label for="gender">Gender</label>
        </div>
        <label for="male" class="radio-inline"><input type="radio" name="gender" value="m" 
id="male">Male</label>
        <label for="female" class="radio-inline"><input type="radio" name="gender" value="f" 
id="female">Female</label>
        <label for="others" class="radio-inline"><input type="radio" name="gender" value="o" 
id="other">Other</label>
        <button type="submit" class="btn btn-primary">Submit</button>
    </form>

Advertisement

Answer

You wrong your query, you use “i” with letter. Change:

 $stmt->bind_param("sssssi",$firstname, $lastname, $mail, $password, $age, $gender);

into:

 $stmt->bind_param("ssssss",$firstname, $lastname, $mail, $password, $age, $gender);

Reference for see all type specification chars : Link

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement