简体   繁体   English

检查空值时遇到问题

[英]Trouble with checking empty value

So, i have some trouble with checking empty value. 因此,我在检查空值时遇到了一些麻烦。 How can i fix this problem? 我该如何解决这个问题? I want to check if category_name exist in database (its working), and i want to check if input is empty (thats not work). 我想检查category_name是否存在于数据库中(它的工作状态),并且我想检查输入是否为空(那是行不通的)。 Here is my code: 这是我的代码:

PHP: PHP:

 <?php
    error_reporting(E_ERROR);
    include("../db_config.php");
    $category = mysqli_real_escape_string($conn, $_POST['category']);
    $result = "SELECT * FROM category WHERE category_name='$category'";
    $rs = mysqli_query($conn,$result);
    $data = mysqli_fetch_array($rs);
    if($data > 1 || $category === NULL)
    {
        echo "<script>
        alert('Category name already exist or the value is empty.');
        window.location.href='category.php';
        </script>";
    }

    else
    {   
        $sql[0] = "INSERT INTO category (category_name) VALUES ('$category')";
        if(mysqli_query($conn, $sql[0]))
        { 
                header("Location:category.php");
                exit();
        } 
        else
        {
        echo "ERROR: Could not able to execute $sql[0]. " . mysqli_error($conn);
        }
    $conn->close();
    }
?>

HTML: HTML:

 <form action="category_insert.php" method="post" enctype="multipart/form-data">
        <input type="hidden" name="mode" value="add">
        Name:<br />
        <input type="text" name="category" placeholder="Category name"><br><br>
        <input type="submit" value="Submit"><br><br>
    </form>

Function mysqli_real_escape_string return escaped string (as you can read in documentation here http://php.net/manual/en/mysqli.real-escape-string.php ) so there is no possible to expect NULL in $category variable. 函数mysqli_real_escape_string返回转义的字符串(您可以在http://php.net/manual/en/mysqli.real-escape-string.php的文档中阅读),因此不可能在$category变量中期望NULL。 Replace this part of your code 替换这部分代码

if($data > 1 || $category === NULL)

with this 有了这个

if($data > 1 || empty($category))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM