title: Using an if statement to update OR insert into
body: <p>I have the following code which up dates my database table perfectly. However, I now wish it to either, update an existing row if the value of $status is 'open', or create a new row if the value of $status is 'completed'.</p>
  
  <p>This is my code so far;</p>
  
  <pre><code>        &lt;?php
      if (isset($_POST['submit_update_activity'])); {
        require 'dbh.inc.php';
        $activity_id          =   $_POST['hidden_activity_id'];
        $idFromKnowledgeBase  =   $_POST['hidden_idFromKnowledgeBase'];
        $hiddenUserID         =   $_POST['hidden_userId'];
        $title                =   $_POST['title'];
        $description          =   $_POST['description'];
        $assigned_to          =   $_POST['assigned_to'];
        $category             =   $_POST['category'];
        $cost                 =   $_POST['cost'];
        $next_due             =   $_POST['next_due'];
        $due_every            =   $_POST['due_every'];
        $frequency            =   $_POST['frequency'];
        $supplier             =   $_POST['supplier'];
        $status               =   $_POST['status'];
        $comments             =   $_POST['comments'];
        $emptyAssignedTo      =   $_POST['empty_assigned_to'];
        $emptyStatus          =   $_POST['empty_status'];
        $emptyCategory        =   $_POST['empty_category'];
        $dateCompleted        =   $_POST['date_completed'];
        $emptyFrequency       =   $_POST['empty_frequency'];
        $emptyNextDue         =   $_POST['empty_next_due'];
        $next_due = date('Y-m-d', strtotime($dateCompleted. " + {$due_every} $frequency"));
        if (empty($frequency)) {
          $frequency = $emptyFrequency;
        }
        if (empty($status)) {
          $status = $emptyStatus;
        }
        if (empty($assigned_to)) {
          $assigned_to = $emptyAssignedTo;
        }
        if (empty($category)) {
          $category = $emptyCategory;
        }
        //This line isn't working
        if ($status == 'open') {
          $next_due = $emptyNextDue;
        } else {
          $next_due = date('Y-m-d', strtotime($dateCompleted. " + {$due_every} $frequency"));
        }
        $stmt = $conn-&gt;prepare("UPDATE activities SET idFromKnowledgeBase = ?, userId = ?, title = ?, description = ?, assigned_to = ?, category = ?, cost = ?, last_completed = ?,next_due = ?, frequency = ?, supplier = ?, status = ?, comments = ? WHERE id = ?");
        $stmt-&gt;bind_param("ssssssssssssss", $idFromKnowledgeBase, $hiddenUserID, $title, $description, $assigned_to, $category, $cost, $dateCompleted, $next_due, $frequency, $supplier, $status, $comments, $activity_id);
        $stmt-&gt;execute();
        if($stmt-&gt;affected_rows &gt;0) {
          header('Location: ../all_activities.php?updated');
        }
        }
        ?&gt;
  </code></pre>
  
  <p>I've tried =, == and === for comparing $status.  If I take it back a notch and echo either 'open' or 'completed' depending on the value of $status, it works fine, echoing the correct answer each time.</p>
  
  <p>Advice is appreciated.</p>