title: How to save checkbox value to MySQL database?
body: <p>I have following html:</p>
  
  <pre><code>&lt;label for="live"&gt;Live&lt;/label&gt;
  &lt;input type="checkbox" name="live" id="live" /&gt;
  </code></pre>
  
  <p>How to save <code>text value of selected = '1'</code>/ or <code>text value of unchecked = '0'</code> to database using SQL INSERT?</p>
  
  <p>Any suggestion much appreciated.</p>
  
  <p>PHP handling the html form (at the moment 'live' is an input):</p>
  
  <pre><code>&lt;?php
      //Start session
      session_start();
  
      //Include database connection details
      require_once('../inc/config.php');
  
      //Connect to mysql server
      $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
      if(!$link) {
          die('Failed to connect to server: ' . mysql_error());
      }
      if($link) {
          echo "DB SUCESS &lt;br /&gt;";
      }
  
      //Select database
      $db = mysql_select_db(DB_DATABASE);
      if(!$db) {
          die("Unable to select database");
      }
      if($db){ echo "TABLE SUCCESS&lt;br /&gt;"; }
  
      //Function to sanitize values received from the form. Prevents SQL injection
      function clean($str) {
          $str = @trim($str);
          if(get_magic_quotes_gpc()) {
              $str = stripslashes($str);
          }
          return mysql_real_escape_string($str);
      }
  
      //Sanitize the POST values
      $live = clean($_POST['live']);
      $content = clean($_POST['content']);
  
  
          //Create INSERT query
      $qry = "INSERT INTO news(live, content) VALUES('$live','$content') ";
      $result = @mysql_query($qry);
  
      //Check whether the query was successful or not
      if($result) {
          //header("location: ../form/register-success.php");
          echo "Succes";      
          exit();
      }else {
          die("Query failed");
      }
  ?&gt;
  </code></pre>