所以,我在检查空值时遇到了一些麻烦。我该如何解决这个问题?我想检查category_name中是否存在category_name(它的工作),我想检查输入是否为空(这不起作用)。这是我的代码:
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:
<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>
答案 0 :(得分:1)
函数mysqli_real_escape_string
返回转义字符串(正如您在此处的文档http://php.net/manual/en/mysqli.real-escape-string.php中所读到的那样),因此$category
变量中无法预期NULL。替换代码的这一部分
if($data > 1 || $category === NULL)
用这个
if($data > 1 || empty($category))