title: PHP - checkbox value / db insert and if(isset())
body: <p>I'm new to PHP, and I have stumble on the problem which I don't know how to solve. I'm 99% it is due my poor knowledge of PHP ( I'm PHP user since last Monday:) )</p>
  
  <p>Just in front I will declarate that:</p>
  
  <ul>
  <li>db conncetion is working</li>
  <li>table does exist</li>
  <li>values are saved correctly to the db</li>
  </ul>
  
  <p>I have following form: </p>
  
  <pre><code>&lt;form id="loginForm" name="loginForm" method="post" action="../exe/news-exec.php"&gt;
        &lt;input name="live" type="checkbox" class="textfield" id="live" /&gt;
        &lt;input name="content" type="text" class="textfield" id="content" /&gt;
        &lt;input type="submit" name="Submit" value="Register" /&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>And following file is executing this:</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());
      }
  
      //Select database
      $db = mysql_select_db(DB_DATABASE);
      if(!$db) {
          die("Unable to select database");
      }
  
      //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']);
  
  
  
      if(isset($live)) { $live = 1;}
      if(!isset($live)) { $live = 0;}
  
  
  
      //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) {
          echo $live."&lt;br /&gt;&lt;br /&gt;";
          echo '&lt;a href="../"&gt;Index File&lt;/a&gt;';
  
          exit();
      }else {
          die("Query failed");
      }
  ?&gt;
  </code></pre>
  
  <p>What the form should do:</p>
  
  <ul>
  <li>if the checkbox is checked - save the value of '1' into field 'live' in the table 'news'</li>
  <li>if the checkbox is NOT checked - save the value of '0'</li>
  </ul>
  
  <p>If the checkbox has been checked everything is working fine, but if the checkbox is not checked (should echo $live = 0 ), but is displaying value = 1 and following notice: <code>Notice: Undefined index: live in C:\wamp\www\exe\news-exec.php on line 30</code></p>
  
  <p>Line 30: <code>$live = clean($_POST['live']);</code></p>
  
  <p>I'm 99% sure the problem are those declaration:</p>
  
  <p><code>if(isset($live)) { $live = 1;}</code></p>
  
  <p><code>if(!isset($live)) { $live = 0;}</code></p>
  
  <p>What I'm doing wrong? Any suggestion much appreciated. </p>