php - MySQL increment and avoid incorrect referencing -


so i'm creating quote (estimate) generating module on online store.

well have created one, want advice on bothering me.

the quote generation happens customer adding products cart, , opting generate pdf-quote instead of checking out products.

the quote-number (document number) stored in database, , gives each quote unique reference number, obvious reasons.

now successful attempt generate quote takes place, following code pulls current reference number, increments 1, , updates database entry newly incremented reference number:

$wbquery = "select wq.configuration_key, wq.configuration_value " . table_configuration . " wq wq.configuration_key='" . $wbkey . "'";  $wbadd = $db->execute($wbquery);  $newwb = $wbadd->fields['configuration_value'] + 1;  $wbdone = "update " . table_configuration . " set configuration_value = '" . $db->prepare_input(trim($newwb)) . "' configuration_key = '" . $wbkey . "'";  $db->execute($wbdone);  $docno = $qtype.date('y').sprintf('%05d', $newwb); 

bof tldr;

so here, saved reference number, , add 1 it. update reference number in database value.

we assign reference number variable $docno, because we're printing pdf, , apart reference number, we're adding prefix, $qtype (which 2 letters) , date('y') 2 number representation of current year. we're forcing reference number add zeros infront of reference number ensure @ least 5 digits long.

eof tldr;

how ensure if measure or stretch of imagination, if 2 customers happen generate quote , precisely same time, there isn't slight conflict.

if customer generates quote @ same time customer b, there off-chance customer a's attempt pull current reference number, , customer b's attempt pull same reference number, , incorrectly share same reference?

or, there off-chance update database might happen out of turn, , incorrectly increment?

the value in database in column type text - , not have auto-increment properties.

the thing is, don't expect ever happen, quote-system used both customers , admin staff - want ensure that...it never happens.

any advice? thanks.

p.s. - have safeguard in place, adds random md5 hash filename pdf-quote saved as. ensure customer generates quote, never mistakenly served quote generated customer - should quotes share same reference number. originally, saved quote using basic $docno - realized pose privacy-risk, when added md5 unique hash filename.

you asking "race condition" (if want more research).

by sound of things, don't want incrementing in php. serve let mysql own auto-incrementing.

so answer/advice is, redesign table structure quote id not text , auto_increment.

otherwise, have go greater trouble safeguard against race condtions.


Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -