开发者

PHP Code- How to check duplicate entries in Mysql Database

I am working on Google checkout API notification URL. 开发者_StackOverflowI want to apply a check in my php code which will see if transaction ID present in transaction table of my DB, it will exit not process.

I am struggling into applying here. Can somebody help?

I want to check if trasaction ID exists in table of DB it will exit, it will not process:

case "new-order-notification":  
    $sql = "
        UPDATE
            transactions
        SET
            remote_trans_id=\"".$_REQUEST["google-order-number"]."\", updated=NOW() 
        WHERE
        id=".$_REQUEST["shopping-cart_items_item-1_merchant-item-id"]
        ;
    execute($sql, $conn);
break;

Regards


Just use INSERT ON DUPLICATE KEY UPDATE. This way mySql will do the checking for you, if is a new transaction will enter it otherwise will update the existing entry.


set the index to unique for the id


If you are trying to avoid double processing when a user hits the refresh or back button on their browser, there may be a better way to handle this.

In the top of your script, create a temp uniqid in a session variable.

session_start();
$_SESSION['temp_page_instance'] = uniqid();

Then, in your form, create a hidden input called page_instance, which will equal the temp_page_instance.

<input type="hidden" name="page_instance" value="<?php echo $_SESSION['temp_page_instance']; ?>" />

Then, at the bottom of your script set the actual page instance.

$_SESSION['page_instance'] = $_SESSION['temp_page_instance'];

In the logic that processes the form data, put a check in there that makes sure your $_POST['page_instance'] == $_SESSION['page_instance']. If not, the page has been reloaded, and it should not process the form again.

Does this help?


You can select all the id's, place them in a an associative array (and maybe save it in a file with serialize instead of always going to the table) like:

$all_ids =  array(
    'id1' => 1, 
    'id2' => 1,
    etc..

Then when you want to check if an id exists you just to

if (isset($all_ids[$id])) { ... }

Hope this helps :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜