2016-06-29 2 views
0

그래서 빈 값을 확인하는 데 문제가 있습니다. 이 문제를 어떻게 해결할 수 있습니까? 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 : mysqli_real_escape_string 반환 (당신이 여기 문서에 http://php.net/manual/en/mysqli.real-escape-string.php를 읽을 수있는) 문자열을 탈출 그래서 NULL을 기대할 수 없다

<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> 
+2

'mysqli_real_escape_string'와 코드

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

의이 부분을 교체합니다. __못__. –

+1

'$ category === NULL '이 (가) 잘못된 위치에 있습니다. 대신에 먼저 여기에'empty() /! empty()'를 사용하고 나서 그것을 할당해야합니다. –

+0

카테고리 입력을 ** 필수 ** 필드로 만드십시오. –

답변

1

기능 in $category 변수. `NULL`을 반환하지 않습니다이

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

고마워요. 5 분 그리고 너의 대답을 받아 들일 수 없어. – Achy

관련 문제