Answer the question
In order to leave comments, you need to log in
Why is PHP sending database queries out of order?
In general, I faced such a problem. There is a piece of code:
$validation = Booking::findByUserProduct( $user_id, $product_id );
$answer = Booking::queryAddReturn($validation, $product_id, $way, $user_id, $count);
public static function findByUserProduct( $user_id, $product_id ) {
$sql = 'SELECT * FROM booking WHERE `user_id`=:user_id AND `product_id`=:product_id';
$res = \Yii::$app->db->createCommand($sql)
->bindValue(':user_id', (int) $user_id)
->bindValue(':product_id', (int) $product_id)
->queryall();
return count($res) === 0;
}
public static function queryAddReturn($validation, $product_id, $way, $user_id, $count) {
if( $validation !== true ) {
return '<p class="text-light">Ошибка: товар уже заказан</p>';
}
else{
if( Booking::addOne( $product_id, $way, $user_id, $count ) )
return '<p class="text-light">Заказ успешно принят</p>';
return '<p class="text-light">Ошибка: не удалось обработать заявку</p>';
}
}
Answer the question
In order to leave comments, you need to log in
Well, if you want everything to be in order, then use transactions. I didn't look at your code. but the essence of transactions is this: it blocks other operations until the request is completed.
It turned out. everything was due to the fact that on the client side the code was called twice. More precisely, on one button, two identical handlers were assigned per click. Ajax request went twice.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question