Handle race condition

Rewrite some core queries to handle race conditions more effectively

<!-- Reviewable:start -->
[<img src="https://reviewable.io/review_button.png" height=40 alt="Review on Reviewable"/>](https://reviewable.io/reviews/zencart/zc-v1-series/244)
<!-- Reviewable:end -->

Although this probably works, the syntax does not match the MySQL documentation. To be on the safe side I would recommend changing the syntax to match. Perhaps something similar to the following?

$sql = "INSERT " . TABLE_SESSIONS . " (sesskey, expiry, value) VALUES (:sesskey, :expiry, :value) ON DUPLICATE KEY UPDATE sesskey=:sesskey, expiry=:expiry, value=:value"
$sql = $db->bindVars($sql, ':sesskey', $key, 'string');
$sql = $db->bindVars($sql, ':expiry', $expiry, 'integer');
$sql = $db->bindVars($sql, ':value', $val, 'string');
$result = $db->Execute($sql);