title: MySQL insert only if not exist in two tables
body: <p>I have a little problem with MySQL query. I'm using PDO queries, which now just insert values into the tables (see below):</p>
  
  <pre><code>$stmt = $dbh -&gt; prepare("INSERT INTO tmp (user_id, test_id, question_id, answer_id) VALUES (?, ?, ?, ?)");
  $stmt -&gt; bindParam(1, $_SESSION['UserID']); // binds parameter for user id
  $stmt -&gt; bindParam(2, $_GET['start_test']); // binds parameter for test id
  $stmt -&gt; bindParam(3, $_POST['question']); // binds parameter for selected answer
  $stmt -&gt; bindParam(4, $_POST['select_answer']); // binds parameter for selected answer
  $stmt -&gt; execute();
  </code></pre>
  
  <p><code>tmp</code> table structure is the following:</p>
  
  <pre><code>CREATE TABLE IF NOT EXISTS `tmp` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `test_id` int(11) NOT NULL,
    `user_id` int(11) NOT NULL,
    `question_id` int(11) NOT NULL,
    `answer_id` int(11) NOT NULL,
    PRIMARY KEY (`id`)
  )
  </code></pre>
  
  <p>I want to insert <code>answer_id</code> value if it's wasn't there and check if user_id at the same time, otherwise just update, example:</p>
  
  <p>I have two users with user_id=1 and user_id=2, they're answering the questions and every answer for each student stores separately. User_id=1 picked answer_id = 3 and user_id=2 picked the answer_id = 3. However, then one user realised this was a wrong answer and wants to change it to another one (suppose answer_id=2), I want the query update the current answer_id=3 WHERE user_id=1 to answer_id=2</p>