-1

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>
Simone Rossaini
  • 8,115
  • 1
  • 13
  • 34
Jawad Kiani
  • 17
  • 1
  • 1
  • 8
  • Have you try to `var_dump($_POST)` or directly `var_dump($_POST["gender"])` for see if is not empty? – Simone Rossaini Dec 24 '20 at 07:55
  • @SimoneRossaini i made a mistake with query i just replaced that i interger with s and it works thanks for the answer – Jawad Kiani Dec 24 '20 at 08:13
  • is php not jquery :) – Simone Rossaini Dec 24 '20 at 08:21
  • @SimoneRossaini yeah sorry its php – Jawad Kiani Dec 24 '20 at 08:28
  • Apart from trimming, the stuff you do in your `test_input` function is unnecessary since you're using prepared statements. They will automatically sanitize your input values. Furthermore, please see about [password hashing](https://stackoverflow.com/questions/14992367/using-php-5-5s-password-hash-and-password-verify-function/). Storing plain text passwords is a huge security hole. – El_Vanja Dec 24 '20 at 10:15

2 Answers2

2

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

Simone Rossaini
  • 8,115
  • 1
  • 13
  • 34
  • 1
    thanks mate your answer worked for me i made a mistake with query i just replaced that i interger with s and it works – Jawad Kiani Dec 24 '20 at 08:12
-1

You may set your datatype of gender in mysql database to integer in mysql table. Please check that. try with this for validation

     if (isset($_POST["firstname"])){
if(($_POST["firstname"]="")){
    $firstnameErr = "Firstname is required";
} else {
    $firstname = test_input($_POST["firstname"]);
}
}

tips : please insert your password using md5() function for secuity

  • i tried this code but same results when i leave the first name place empty in the form no message is shown to enter the name mean validation is not working. Is there any other way to solve it – Jawad Kiani Dec 24 '20 at 08:29
  • Please don't recommend `md5` for password hashing. The [documentation itself](https://www.php.net/manual/en/function.md5) is advising against it as it was built for speed, not safety. The proper things to use would be [`password_hash`](https://www.php.net/manual/en/function.password-hash) and [`password_verify`](https://www.php.net/manual/en/function.password-verify). – El_Vanja Dec 24 '20 at 10:06