How to leave NULL if $ _POST is empty in the MySQL database?

0

Is there any way I can leave fields that have not been filled as null ? When creating the table, the fields have already been defined as DEFAULT NULL , but when doing insert - in the MySQL database - by $ _ POST , the fields that were not filled remain empty.

Below is the code.

if (isset($_POST['update'])) {

    $nome          = $_POST['nome'];
    $sobrenome     = $_POST['sobrenome'];
    $nivel         = $_POST['nivel'];
    $date     = date('Y-m-d H:i:s');

    $_SESSION['warning']['type']    = "success"; // tipos success, info, warning e error
    $_SESSION['warning']['message'] = "Player Created Successfully!"; // mensagem que vai aparecer

    if (empty($_POST['nome'])) {
        $_SESSION['warning']['type']    = "error"; // tipos success, info, warning e error
        $_SESSION['warning']['message'] = "The NAME is required."; // mensagem que vai aparecer
        echo "history.go(-1);";
        exit;
    }

    $vsl = "INSERT INTO 'files' ('id', 'nome', 'sobrenome', 'nivel', 'date')VALUES(NULL, '$nome', '$sobrenome', '$nivel', '$date')";
    $rsl = mysqli_query($conn, $vsl);

    header("Location: add.php");
    exit();

}
    
asked by anonymous 18.10.2018 / 20:00

1 answer

0

How is inserting via PHP simple quotes is different from null an idea is to put in the variable content example:

$sobrenome = strlen($sobrenome) > 0 ? "'$sobrenome'" : "NULL";
$vsl = "INSERT INTO files (id, nome, sobrenome, nivel, date) VALUES(NULL, '$nome', $sobrenome, '$nivel', '$date')";
$rsl = mysqli_query($conn, $vsl);

That would solve.

    
18.10.2018 / 20:11