title: How to insert data into database only when input are not empty?
body: <p>I came up with a problem which made me crazy as I am new to PHP. The problem is: the below timetable submission form works good on Chrome (every time I left the email unfilled, the submission cannot be processed). However, when using on safari, you can always submit blank form into the database.</p>
  
  <p>Here is the html form.</p>
  
  
  
  <pre><code>&lt;script type="text/javascript" src="/membership/my-form/js/jquery-2.1.1.js"&gt;&lt;/script&gt;
  &lt;script type="text/javascript" src="/membership/my-form/js/main.js"&gt;&lt;/script&gt;
  
  &lt;form class="mf-form floating-labels" method="post" action="timetablesubmit.php"&gt;
  
      &lt;div&gt;
          &lt;h1 style="text-align:center"&gt;Availability form&lt;/h1&gt;
      &lt;/div&gt;
      &lt;div&gt;
          &lt;p class="mf-select icon"&gt;
              &lt;select name="timetable-staff" class="user" required&gt;
                  &lt;option value=""&gt;Select Person&lt;/option&gt;
                  &lt;option value="AMY"&gt;Amy&lt;/option&gt;
                  &lt;option value="TOM"&gt;Tom&lt;/option&gt;
              &lt;/select&gt;
          &lt;/p&gt;
  
          &lt;div&gt;
              &lt;input name="location1" value="A" type="hidden"&gt;
              &lt;input name="location2" value="B" type="hidden"&gt;
          &lt;/div&gt;
          &lt;div class="AMY box"&gt;You work in A.&lt;/div&gt;
          &lt;div class="TOM box"&gt;You work in B.&lt;/div&gt;
      &lt;/div&gt;
  
      &lt;div class="icon"&gt;
          &lt;label class="mf-label" for="mf-email"&gt;Email Address&lt;/label&gt;
          &lt;input class="email" type="email" name="timetable-email" id="mf-email" required&gt;
      &lt;/div&gt;
  
  &lt;/form&gt;
  
  
  &lt;script type="text/javascript"&gt;
      $(document).ready(function(){
          $("select").change(function(){
              $( "select option:selected").each(function(){
                  if($(this).attr("value")=="Amy"){
                      $(".box").hide();
                      $(".AMY").show();
                  }
                  if($(this).attr("value")=="Tom"){
                      $(".box").hide();
                      $(".TOM").show();
                  }
              });
          }).change();
      });
  &lt;/script&gt;
  
  
  &lt;style type="text/css"&gt;
      .box{
          padding: 10px;
          display: none;
          margin-top: 20px;
          border: 1px solid #000;
          font-size:1.6em;
          text-align:center;
          background-color: #f1f1f1;
      }
  &lt;/style&gt;
  </code></pre>
  
  
  
  <p>Here is the timetablesubmit.php:</p>
  
  <pre><code>&lt;?php
  header("content-type:text/html;charset=utf-8");
  
  session_start();
  
  $timesubmit=date('Y-m-d H:i:s');
  $staff=$_POST['timetable-staff'];
  $email=$_POST['timetable-email'];
  
  
  $con=mysql_connect("localhost","database","password");
  if (!$con) {
      die ('Could not connect:' . mysql_error());
  }
  
  mysql_select_db("database", $con);
  
  mysql_query("set names utf8");
  mysql_query("INSERT INTO timetable(staff,email)
          VALUES('$staff','$email')");
  
  mysql_close($con);
  
  sleep(2);
  ?&gt;
  
  &lt;html&gt;
  &lt;? require 'header.php'; ?&gt;
  &lt;div class="tk-reg"&gt;
      &lt;p&gt;Thak you &lt;?php echo $_POST['timetable-staff']; ?&gt;&lt;p&gt;
      &lt;p&gt; Time availability submitted successfully.&lt;p&gt;
          Your email address is: &lt;?php echo $_POST["timetable-email"]; ?&gt;
  &lt;/div&gt;
  
  &lt;div class="tk-regfollow"&gt;
      &lt;ul style="text-align:center"&gt;
          &lt;a href="/membership/index.php"&gt;Back to home page.&lt;/a&gt;
      &lt;/ul&gt;
  &lt;/div&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Then I searched the Internet and changed to below, still not working on Safari (the alert appears, however data still been insert into database.</p>
  
  <pre><code>&lt;?php
  require 'header.php';
  $nameErr = $emailErr = "";
  $name = $email = "";
  
  if ($_SERVER["REQUEST_METHOD"] == "POST") {
      if (empty($_POST["timetable-staff"])) {
          $nameErr = "Please select a staff.";
      } else {
          $staff = test_input($_POST["timetable-staff"]);
      }
      if (empty($_POST["timetable-email"])) {
          $emailErr = "Email address is required";
      } else {
          $email = test_input($_POST["timetable-email"]);
      }
  }
  function test_input($data) {
      $data = trim($data);
      $data = stripslashes($data);
      $data = htmlspecialchars($data);
      return $data;
  }
  
  $con=mysql_connect("localhost","database_name","database_password");
  if (!$con) {
      die ('Could not connect:' . mysql_error());
  }
  
  mysql_select_db("database_name", $con);
  
  mysql_query("set names utf8");
  mysql_query("INSERT INTO timetable(staff,email)
          VALUES('$staff','$email')");
  
  mysql_close($con);
  sleep(2);
  ?&gt;
  
  
  &lt;html&gt;
  &lt;style&gt;
      .error {color: #FF0000;}
  &lt;/style&gt;
  
  &lt;h1 style="text-align:center"&gt;Availability form for
      &lt;?php
      $d=strtotime("+1 Months");
      echo date("M", $d) . "  2015 &lt;br&gt;";
      ?&gt;
  &lt;/h1&gt;
  
  &lt;form class="mf-form floating-labels" method="post" action="&lt;?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?&gt;"&gt;
      &lt;div&gt;
          &lt;p class="mf-select icon"&gt;
              &lt;select name="timetable-staff" class="user" required&gt;
                  &lt;option value=""&gt;Select Person&lt;/option&gt;
                  &lt;option value="AMY"&gt;Amy&lt;/option&gt;
              &lt;option value="TOM"&gt;Tom&lt;/option&gt;
              &lt;/select&gt;
              &lt;span class="error"&gt;* &lt;?php echo $nameErr;?&gt;&lt;/span&gt;
          &lt;/p&gt;
      &lt;/div&gt;
      &lt;div class="icon"&gt;
          &lt;label class="mf-label" for="mf-email"&gt;Email Address&lt;/label&gt;
          &lt;input class="email" type="email" name="timetable-email" id="mf-email" required&gt;
          &lt;span class="error"&gt;* &lt;?php echo $emailErr;?&gt;&lt;/span&gt;
      &lt;/div&gt;
      &lt;br&gt;&lt;br&gt;
      &lt;input type="submit" name="submit" value="Submit"&gt;
  &lt;/form&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>I hope someone could help me pointing out my mistakes. Thank you.</p>
  
  <p>======================================================================</p>
  
  <p>I tried a lot of ways to figure this out, however still have either 'this' or 'that' problems. I finally use a method that actually work, but I do not know whether it is not recommended.</p>
  
  <p>Here is the code I modified in timetablesubmit.php
  (my flow is: timetable.php ==>timetablesubmit.php==>welcome.php)</p>
  
  <pre><code>$email = trim($_POST['timetable-email']);
  if($email !='' and $email !=null) {
      $sql1;
  }
  else {
       echo '&lt;html&gt;&lt;head&gt;
               &lt;link rel="stylesheet" type="text/css" href="/membership/style.css"/&gt;
             &lt;head&gt;
          &lt;div class="check-error-message"&gt;
             &lt;p&gt;Error: Email address is required.&lt;/p&gt;&lt;br&gt;
             &lt;a href="javascript:history.back(-1);"&gt;Return&lt;/a&gt;
          &lt;/div&gt;';
      exit;
  };
  </code></pre>