skip to Main Content

i have a database and want to check if the elements are set or not before they pass to database because i dont want to pass null values for entities mentioned but while using this statement i find out values are passing without being check for availability.

i tried to pass those elements to the database but check for nulls first and they were not checked for nulls and sometimes got this error as well:

Fatal error: Cannot use isset() on the result of an expression (you
can use "null !== expression" instead) in C:xampphtdocs….on line
3

2

Answers


  1. The isset function works differently, you need to put a variable into it, not a condition.

    For example:

    $arr = [
      'a' => 'Hello',
    ];
    
    $bool = isset($arr['a']) // return true
    $bool = isset($arr['b']) // return false
    

    this page detailed


    Let’s try writing the following to fix the error in your code.

    if (
      !isset($_POST["Name"]) ||
      !isset($_POST["Email"]) ||
      !isset($_POST["Password"]) ||
      !isset($_POST["Phone"])
    ) {
      // some fields is null, not valid.
    }
    

    or

    if (!isset($_POST["Name"], $_POST["Email"], $_POST["Password"], $_POST["Phone"])) {
      // some fields is null, not valid.
    }
    
    Login or Signup to reply.
  2. If you want to prevent null values from being inserted into the DB:

    CREATE TABLE User(
        ID int NOT NULL,
        LastName varchar(255) NOT NULL,
        FirstName varchar(255) NOT NULL
    );
    

    also, use the required attribute in your html document:

    <form action="/action_page.php"> 
        <input type="text" id="username" required>  
    </form>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search