title: Introducing NULL fields into MYSQL from PHP
body: <p>Im having problems trying to introduce NULL into fields in my database. My rows are composed of 24 columns (double values) and it always appears 0 no matter what I try.</p>
  
  <p>Any idea? Ive read inserting null without quotes should work (reserved word for mysql), but it inserts me 0 though.</p>
  
  <p>This is the short version of my code:</p>
  
  <p>Android:</p>
  
  <pre><code>for(int i = lastID; i &lt; data2.get(SensorType.ACCELEROMETER_X).size(); i++){
  
      List&lt;NameValuePair&gt; nameValuePairs = new ArrayList&lt;NameValuePair&gt;(data2.keySet().size()+1);
      Set&lt;SensorType&gt; signals = data2.keySet();
      nameValuePairs.add(new BasicNameValuePair("TableName", tableNameSignals));
      for(SensorType s: signals){
  
          if(data2.get(s).get(i).isNaN()){
              //HERE IS WHERE I WANT THE FIELD TO APPEAR NULL
              nameValuePairs.add(new BasicNameValuePair(s.getAbbreviature(), "null"));
          }
          else{
              nameValuePairs.add(new BasicNameValuePair(s.getAbbreviature(), data2.get(s).get(i).toString()));
  
          }
      }
  
      httpPost2.setEntity(new UrlEncodedFormEntity(nameValuePairs));
      HttpResponse httpResponse2 = httpClient.execute(httpPost2);
  </code></pre>
  
  <p>PHP:</p>
  
  <pre><code>&lt;?php
  
  //Array for Json response
  $response = array();
  
  //Include DB class
  require_once __DIR__ . '/db_connect_v2.php';
  
  require_once __DIR__ . '/db_config_v2.php';
  
  $mysqli = new mysqli(DB_SERVER, DB_USER, DB_PASSWORD, DB_DATABASE);
  
  if ($mysqli-&gt;connect_errno)
  
      die('Connection Error (' . $mysqli-&gt;connect_errno . ') ' . $mysqli-&gt;connect_error);
  
  $tableName = $_POST['TableName'];
  
  $sql1 = "CREATE TABLE IF NOT EXISTS " . "$tableName" . "(ID INT NOT NULL PRIMARY KEY AUTO_INCREMENT, AccX double, AccY double, AccZ double, MagX double, MagY double, MagZ double, GyrX double, GyrY double,
      GyrZ double, Light double, Pressure double, Proximity double, GravX double, GravY double, GravZ double, LinAccX double, LinAccY double, LinAccZ double, 
      RotVecX double, RotVecY double, RotVecZ double, Humidity double, Temperature double, TimeStamp double);";
  
  $result1 = $mysqli-&gt;query($sql1);
  
  if($result1){
  
      $accX = $_POST['AccX']; $accY = $_POST['AccY']; $accZ = $_POST['AccZ']; $magX = $_POST['MagX'];
      $magY = $_POST['MagY']; $magZ = $_POST['MagZ']; $gyrX = $_POST['GyrX']; $gyrY = $_POST['GyrY'];
      $gyrZ = $_POST['GyrZ']; $light = $_POST['Light']; $pressure = $_POST['Pressure']; $proximity = $_POST['Proximity'];
      $gravX = $_POST['GravX']; $gravY = $_POST['GravY']; $gravZ = $_POST['GravZ']; $linAccX = $_POST['LinAccX'];
      $linAccY = $_POST['LinAccY']; $linAccZ = $_POST['LinAccZ']; $rotVecX = $_POST['RotVecX'];
      $rotVecY = $_POST['RotVecY'];  $rotVecZ = $_POST['RotVecZ'];  $humidity = $_POST['Humidity'];  $temperature = $_POST['Temperature'];
      $timeStamp = $_POST['TimeStamp'];
  
      $sql2 = "INSERT INTO " . "$tableName" . "(AccX, AccY, AccZ, MagX, MagY, MagZ, GyrX, GyrY, GyrZ, Light, Pressure, Proximity, GravX, GravY, GravZ, 
      LinAccX, LinAccY, LinAccZ, RotVecX, RotVecY, RotVecZ, Humidity, Temperature, TimeStamp) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);";
  
      $stmt = $mysqli-&gt;prepare($sql2);
      $stmt-&gt;bind_param('dddddddddddddddddddddddd', $accX, $accY, $accZ, $magX, $magY, $magZ, $gyrX, $gyrY, $gyrZ, $light, $pressure,
      $gravX, $gravY, $gravZ, $proximity, $linAccX, $linAccY, $linAccZ, $rotVecX, $rotVecY, $rotVecZ, $humidity, $temperature, $timeStamp);
  
      $result2 = $stmt-&gt;execute();
  
      $stmt-&gt;close();
      $mysqli-&gt;close();
  
      if($result2){
  
          $response['Success'] = 1;
          $response['Message'] = "Row was uploaded succesfully!";
          echo json_encode($response);
      }
  
      else{
  
          $response['Success'] = 0;
          $response['Message'] = "Oops! Couldnt upload data! (Mobile Signals)";
          $response["PhpError"] = mysql_error();
          echo json_encode($response);
      }
  }
  
  else{
  
      $response['Success']=0;
      $response['Message']="Oops! Couldnt create table! (Mobile Signals)";
      $response["PhpError"] = mysql_error();
      echo json_encode($response);
  }
  
  ?&gt;
  </code></pre>
  
  <p>SOLUTION:</p>
  
  <p>The problem was that inserting null in the BasicNameValuePair, the field is sent to the php script as an empty string. If in the php script I do this:</p>
  
  <pre><code>if($_POST['AccX'] != "") $accX = $_POST['AccX']; else $accX = NULL;
  </code></pre>
  
  <p>Everything works fine.</p>