title: How can I use MySQL NOT NULL with PHP?
body: <p>I've form to insert values in MySQL database with PHP (Example code is below):</p>
  
  <pre><code>&lt;form method="post" action="&lt;?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?&gt;"&gt;
  &lt;style&gt;
  label{text-align:left;
         width:150px;
         display:block;
             float:left;
             clear:right;
         font-size:18;}
  
  form {
      width: 30%;
      margin: 0 auto;
  }
  &lt;/style&gt;
  &lt;label&gt;Name:&lt;/label&gt;&lt;input name=name autocomplete=off&gt;&lt;br&gt;
  &lt;label&gt;Date of Birth:&lt;/label&gt;&lt;input size=2 name=dd placeholder=dd&gt; / &lt;input size=2 name=mm placeholder=mm&gt; / &lt;input size=6 name=yyyy placeholder=yyyy&gt;&lt;br&gt;
  &lt;label&gt;Time of Birth:&lt;/label&gt;&lt;input name=tob placeholder="hh : mm : ss"&gt;&lt;br&gt;
  &lt;label&gt;City:&lt;/label&gt;&lt;input name=city&gt;&lt;br&gt;&lt;br&gt;
  &lt;center&gt;&lt;input type=submit value=Save&gt;&lt;/center&gt;
  &lt;/form&gt;
  
  &lt;?php
  if ($_SERVER["REQUEST_METHOD"] == "POST"){
  $servername = "localhost";
  $username = "userame";
  $password = "password";
  $dbname = "myDB2";
  if(empty($_POST["name"])){ echo "Name is Required&lt;br&gt;";}
  else {
  $name = $_POST["name"];
  }
  var_dump($name);
  $tob = $_POST["tob"];
  $city = $_POST["city"];
  $dd = $_POST["dd"];
  $mm = $_POST["mm"];
  $yyyy = $_POST["yyyy"];
  
  $dob = "$yyyy-$mm-$dd";
  
  // Create connection
  $conn = new mysqli($servername, $username, $password, $dbname);
  // Check connection
  if ($conn-&gt;connect_error) {
      die("Connection failed: " . $conn-&gt;connect_error);
  }
  
  //Inserting Data
  $sql = "INSERT INTO Person
  VALUES('$name','$dob','$tob','$city')";
  if ($conn-&gt;query($sql) === TRUE) {
      echo "&lt;br&gt;New record created successfully";
  } else {
      echo "Error: " . $sql . "&lt;br&gt;" . $conn-&gt;error;
  }
  
  $conn-&gt;close();
  }
  ?&gt;
  </code></pre>
  
  <p>AND The COLUMN <code>Name</code> is set to <strong><code>varchar(60) NOT NULL UNIQUE</code></strong> data-type &amp; constraint.</p>
  
  <hr>
  
  <p>In-spite of </p>
  
  <ul>
  <li><p><code>NOT NULL</code> constraint for <code>Name</code> Column, and </p></li>
  <li><p><code>if(empty($_POST["name"])){ echo "Name is Required";}
  else {
  $name = $_POST["name"];
  }</code></p></li>
  </ul>
  
  <p>When I inserts submit form without entering any value in Name Input in the form, I get following message (according to code)</p>
  
  <pre>
  Name is Required
  NULL
  New record created successfully
  </pre>
  
  <p>And My Database output like:</p>
  
  <p><img src="https://i.stack.imgur.com/X1eqN.png" alt="screen0"></p>
  
  <p>Here you can see that <code>var_dump($name)</code> says <code>$name</code> is <strong><code>NULL</code></strong> BUT Still MySQL Database accepting this value for <code>Name</code> Column!</p>
  
  <hr>
  
  <p>So, My question is What is the use of <code>NOT NULL</code> or <strong>How can I use MysQL <code>NOT NULL</code> with PHP?</strong> for inserting data with the help for form?</p>