PHP trim() Function

The trim() is very useful to remove whitespace before and after any data; thus your strings will be precise characters. The code below shows a form. When the form is submitted, the $_POST variables are escaped and trimmed of any potential whitespace.

<?php
include('connect.inc');
$db = public_db_connect();
if (count($_POST) > 0) {

    $firstname = mysqli_real_escape_string($db, trim($_POST['firstname']));
    $lastname = mysqli_real_escape_string($db, trim($_POST['lastname']));
    $height = mysqli_real_escape_string($db, trim($_POST['height']));
    $email = mysqli_real_escape_string($db, trim($_POST['email']));
    $age = mysqli_real_escape_string($db, trim($_POST['age']));
    $weight = mysqli_real_escape_string($db, trim($_POST['weight']));
    $command2 = "INSERT INTO table_sort3 VALUES (NULL, '$lastname', '$firstname', '$email', '$height', '$age', '$weight', now()) ";
    $result2 = mysqli_query($db, $command2);
    if ($result2) {
        echo "Successsul Update!";
    }
}

?>
<form method="post" action="">
    Lastname:<input type="text" name="lastname" value=""/><br/>
    Firstname:<input type="text" name="firstname" value=""/><br/>
    Email:<input type="text" name="email" value=""/><br/>
    Height:<input type="text" name="height" value=""/><br/>
    Age:<input type="text" name="age" value=""/><br/>
    Weight:<input type="text" name="weight" value=""/><br/>
    <input type="submit" name="submit" value="Submit"/>
</form>