title: PHP PDO Update: SQLSTATE[HY093]: Invalid parameter number: parameter was not defined
body: <p>I know this problem came multiple times, but i cannot find the mistake in my code. In my update-branch every $stmt->bindValue(...) returns TRUE, but i catch the pdo exception </p>
  
  <blockquote>
    <p>SQLSTATE[HY093]: Invalid parameter number: parameter was not defined</p>
  </blockquote>
  
  <p>Inserting a new entry works fine.</p>
  
  <p>In my mysql-database the structure of table 'system' is:</p>
  
  <pre><code>id -&gt; int(11), primary key
  computer_name -&gt; varchar(255)
  cpu_speed -&gt; int(11)
  ram_size -&gt; int(11)
  mac_address -&gt; varchar(255)
  operating_system -&gt; varchar(255)
  </code></pre>
  
  <p>My error-throwing code:</p>
  
  <pre><code>// Search for mac_address.
  // If an entry with the same MAC exists update the entry.
  // Else, create a new entry
  $stmt = $pdo-&gt;prepare("SELECT * FROM system WHERE mac_address=:mac");
  $stmt-&gt;bindValue(":mac", $mac_address, PDO::PARAM_STR);
  $stmt-&gt;execute();
  $rows = $stmt-&gt;fetchAll(PDO::FETCH_ASSOC);
  
  // If no rows are returned, no entry exists =&gt; create a new one
  if(empty($rows))
  {  
    // Prepare statement
    $stmt = $pdo-&gt;prepare("INSERT INTO
         system(`computer_name`,`cpu_speed`,`ram_size`,`mac_address`, `operating_system`)
         VALUES(:computer_name, :cpu_speed, :ram_size, :mac_address, :operating_system)");
    $stmt-&gt;bindValue(":computer_name", $computer_name, PDO::PARAM_STR);
    $stmt-&gt;bindValue(":cpu_speed", $cpu_speed, PDO::PARAM_INT);
    $stmt-&gt;bindValue(":ram_size", $ram_size, PDO::PARAM_INT);
    $stmt-&gt;bindValue(":mac_address", $mac_address, PDO::PARAM_STR);
    $stmt-&gt;bindValue(":operating_system", $operating_system, PDO::PARAM_STR);
  }
  else  // Update existing entry
  {
    //computer_name   cpu_speed   ram_size    mac_address     operating_system 
    $stmt = $pdo-&gt;prepare("UPDATE system
        SET computer_name=:computer_name,
            cpu_speed=:cpu_speed,
            ram_size=:ram_size,
            operating_system=:operating_sytem,
            mac_address=:mac_address_in
         WHERE mac_address=:mac_address_query");
    echo$stmt-&gt;bindValue(":computer_name", $computer_name, PDO::PARAM_STR);
    echo$stmt-&gt;bindValue(":cpu_speed", $cpu_speed, PDO::PARAM_INT);
    echo$stmt-&gt;bindValue(":ram_size", $ram_size, PDO::PARAM_INT);
    echo$stmt-&gt;bindValue(":mac_address_in", $mac_address, PDO::PARAM_STR);
    echo$stmt-&gt;bindValue(":operating_system", $operating_system, PDO::PARAM_STR);
    echo$stmt-&gt;bindValue(":mac_address_query", $mac_address, PDO::PARAM_STR);
  }
  
  // Execute the command
  $stmt-&gt;execute();
  </code></pre>