PHP向数据库发送空白信息

标签 php mysql unity3d isset

我将 Unity 表单与 PHP 结合使用,将数据发送到数据库,我可以成功输入信息并将其发送到数据库,但问题是如果字段为空,它也会发送数据。

我正尝试使用 isset 方法来防止这种情况,但它似乎不起作用。有人可以帮我仔细检查一下吗?

数据到达数据库的图片

enter image description here

统一形式的形象

enter image description here

<?php

    if(!isset($_POST['usernamePost'], $_POST['passwordPost'], $_POST['emailPost']))
    {
        echo "This is not working";
    }
    else
    {
    //Enter the data into the database
    $username = filter_var($_POST['usernamePost'], FILTER_SANITIZE_STRING);
    $password = filter_var($_POST['passwordPost'], FILTER_SANITIZE_STRING);
    $email = filter_var($_POST['emailPost'], FILTER_SANITIZE_STRING);

    //variable for connection
    $servername = "localhost";
    $server_username = "root";
    $server_password = "";
    $dbName = "ontrigger_game";

        try
        {
            //connection to the database
            $dbhandle = mysqli_connect($servername, $server_username, $server_password) 
              or die("Unable to connect to MySQL");
            echo "Connected to MySQL<br>";

            //select a database to work with
            $selected = mysqli_select_db($dbhandle, $dbName)
                    or die("Could not select database");

            $sql = "INSERT INTO users (username, password, email)
                VALUES ('".$username."','".$password."','".$email."')";
            if(!mysqli_query($dbhandle, $sql))
            {
              die('Error: ' . mysqli_error());
            }
            echo "1 record added";

            //close the connection
            mysqli_close($dbhandle);

        } 
        catch (Exception $ex) 
        {

        }
    }
?>

最佳答案

您还应该使用 empty() 方法检查它是否为空

if (!isset($_POST['usernamePost']) && empty($_POST['usernamePost'] )) {

// error message here

} else {

// What you want to do if not empty and is set.

}

注意:使用&& 运算符而不是逗号

关于PHP向数据库发送空白信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46075909/

相关文章:

php - 如何将 MySQL 记录字段结果放入 PHP 对象中?

mysql - MariaDB 是否具有与 MYSQL/Oracle JSON_TABLE 等效的可扩展性

php - MySQL不删除记录

php - mysql_connect() 中的 localhost 与 127.0.0.1

php - 将属性添加到关联类 ManyToMany

php - 如何检查数组元素是否存在?

mysql - 批量 SQL 更新将整个数据库中的某些短语更改为新短语

c# - 从 Unity 中的另一个脚本访问对象的位置

c# - 构建项目时出错 : Error building Player because scripts have compile errors in the editor

c# - 在 Unity 中缩放游戏对象