title: PHP Form Input Array with NULLs
body: <p>Using PHP Version 7.1.9, MariaDB 10.1.26.</p>
  
  <p>I'm submitting an array of form data to a MySQL database. The inputs look like this;</p>
  
  <pre><code>// I have removed additional html form code for brevity
  &lt;input type="text" name="ip[]"&gt;
  &lt;input type="text" name="ip[]"&gt;
  etc...
  </code></pre>
  
  <p>When inputs are empty, I want to insert a <code>NULL</code> value into my database, this is where I'm having issues.</p>
  
  <p>I have ensured that my database table is set to;</p>
  
  <ul>
  <li>allow null = yes</li>
  <li>default - null</li>
  </ul>
  
  <p>My PHP code for handling the form submission is below <em>(please ignore any security vulnerabilities)</em>;</p>
  
  <pre><code>// I have removed additional php code for brevity
  $arr_ip = $_POST['ip'];
  for ($i = 0; $i &lt; count($arr_ip); $i++) {
      $arr_ip[$i] = $arr_ip[$i] ? $arr_ip[$i] : 'NULL';
      $sql = "INSERT INTO staff (ip) VALUES ( ".$arr_ip[$i]." )
  }
  </code></pre>
  
  <p>The error I receive is;</p>
  
  <blockquote>
    <p>SQLSTATE[42000]: Syntax error or access violation: 1064 You have an
    error in your SQL syntax; check the manual that corresponds to your
    MySQL server version for the right syntax.. ..</p>
  </blockquote>
  
  <p>When I <code>var_dump($arr_ip)</code> I get;</p>
  
  <pre><code>array(2) {
    [0]=&gt;
    string(15) "123.123.123.123"
    [1]=&gt;
    string(0) ""
  }
  </code></pre>
  
  <p>If I change the PHP to the following (notice the additional <code>' '</code>) in the insert, the query runs successfully, two records are inserted <strong>but</strong> the literal word <code>NULL</code> is inserted into the database instead of a <code>(NULL)</code> value.</p>
  
  <pre><code>$arr_ip = $_POST['ip'];
  for ($i = 0; $i &lt; count($arr_ip); $i++) {
      $arr_ip[$i] = $arr_ip[$i] ? $arr_ip[$i] : 'NULL';
      $sql = "INSERT INTO staff (ip) VALUES ( '".$arr_ip[$i]."' )
  }
  </code></pre>
  
  <p>Can someone tell me what I'm doing wrong and how to resolve the issue please, other than using prepared statements?</p>