سؤال

Lets say that an user wants to edit his order in a online shop, but in the same time his order has being processed and the status of this order is set to "dispatched". Of course he can't really know that, because the order data was loaded just a while before it was dispatched. Now he can only get the message "Something went wrong..."

With a scenario like below, I can only send back to the user (via ajax) message that the order couldn't be updated (either the data wasn't changed or some of the values does not match in WHERE clause) :

   $edit_order = $this->db->prepare('UPDATE orders SET '.$some_fields_loop.' WHERE id=:order_id AND user_id=:user_id AND product_id=:product_id AND NOT status = :ready AND NOT archived = 1');

   $edit_order->bindValue( ... );

   $edit_order->execute();

   if ($edit_order->rowCount()) 
   {
       echo(json_encode(array('status'=>'success', 'msg'=>'Your order has just been updated'));
   } 
   else 
   {
       echo(json_encode(array('status'=>'error', 'msg'=>'Your order has not been updated'));
   }

But I want to be more specific and let him know what's happened exactly (and eventually modify the order DOM elements, based on ajax response).

The order_id is wrong, or user_id because his session has just expired and the session[user_id] doesn't match the order, or so on... and send back to the user corresponding message.

How do I do this in single query? Is that even possible, or should I run each query with single WHERE clause seprately (what I would really like to avoid) ?

هل كانت مفيدة؟

المحلول

SELECT * FROM orders WHERE id=:order_id

first, and then verify the result against desired values.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top