title: How to Insert NULL Value from PHP Variable to MySQL, staying away from SQL Injection?
body: <p>I have a Song Uploading Form, where I will not directly input NULL value in MySQL like: mysql_query("INSERT INTO <code>songs</code> (<code>album_id</code>) VALUES (NULL)". I will insert NULL from PHP Variable to MySQL, and surely being safe from SQL Injection. </p>
  
  <p>My SQL Table is:</p>
  
  <pre><code>CREATE TABLE IF NOT EXISTS `songs` (
    `song_id` int(4) NOT NULL,
    `song_name` varchar(64) NOT NULL,
    `artist_id` int(4) NOT NULL,
    `album_id` int(4) DEFAULT NULL,
    `genre_id` int(4) DEFAULT NULL
    PRIMARY KEY (`song_id`)
  ) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
  </code></pre>
  
  <p>My FORM and FORM HANDLER Code is (PHP, HTML) like below:</p>
  
  <pre><code>&lt;?php
  
  if(isset($_REQUEST['SongForm']))
  {
  $song_name = trim($_POST['song_name']);
  $artist_id = trim($_POST['artist_id']);
  $album_id = $_POST['album_id']; if($album_id == 0) { $album_id = 'NULL'; } // I even tried using NULL instead of 'NULL'
  $genre_id = $_POST['genre_id']; if($genre_id == 0) { $genre_id = 'NULL'; }
  
  $query = mysql_query("
  INSERT INTO `songs` (`song_name`, `artist_id`, `album_id`, `genre_id`) 
  VALUES ('".$song_name."', '".$artist_id."', '".$album_id."', '".$genre_id."')
  ");
  }
  
  ?&gt;
  
  &lt;form method="post" action="&lt;?php echo $_SERVER['PHP_SELF']?&gt;" name="SongForm" id="SongForm"&gt;
  &lt;table style="width: 100%"&gt;
  &lt;tr&gt;
  &lt;td&gt;&lt;b&gt;Song Name&lt;/b&gt;&lt;/td&gt;
  &lt;td&gt;&lt;input name="song_name" value="" type="text" required /&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
  &lt;td&gt;&lt;b&gt;Artist Name&lt;/b&gt;&lt;/td&gt;
  &lt;td&gt;
  &lt;select id="artist_id" name="artist_id"&gt;
  &lt;option value ="0"&gt;No Artist&lt;/option&gt;
  &lt;option value ="1"&gt;Jennifer Lopez&lt;/option&gt;
  &lt;/select&gt;
  &lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
  &lt;td&gt;&lt;b&gt;Album Name&lt;/b&gt;&lt;/td&gt;
  &lt;td&gt;
  &lt;select id="album_id" name="album_id"&gt;
  &lt;option value ="0"&gt;No Album&lt;/option&gt;
  &lt;option value ="1"&gt;Rebirth&lt;/option&gt;
  &lt;/select&gt;
  &lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
  &lt;td&gt;&lt;b&gt;Genre Name&lt;/b&gt;&lt;/td&gt;
  &lt;td&gt;
  &lt;select id="genre_id" name="genre_id"&gt;
  &lt;option value ="0"&gt;No Genre&lt;/option&gt;
  &lt;option value ="1"&gt;Epic Records&lt;/option&gt;
  &lt;/select&gt;
  &lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
  &lt;td&gt;&lt;b&gt;&amp;nbsp;&lt;/td&gt;&lt;/b&gt;
  &lt;td&gt;&lt;input name="SongForm" type="submit" value="Upload Song" /&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;/table&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>But after this I get the Result in MySQL like:</p>
  
  <p>Serial ID: 1
  Song Name Name: I, Love
  Artist ID: 1
  Album ID: 1
  Genre ID: 1</p>
  
  <p>Now, when I am NOT SELECTING "Album ID" and "Genre ID" for any song, it should Input "NULL" inside MySQL. But it is inputting "0".</p>
  
  <p>Therefore the result is coming like:</p>
  
  <p>Serial ID: 1
  Song Name Name: I, Love
  Artist ID: 1
  Album ID: 0
  Genre ID: 0</p>
  
  <p>Please give me a solution so that I can input NULL when I choose "No Album" and "No Genre".</p>
  
  <p>Please don't make me confused explaining irrelevant topics.</p>
  
  <p>Thanks to the friends who supported explaining answers, though any of the answers didn't give me proper solution yet.</p>