Wanting to update with two conditions both with multiple values - php

I am building a status system that shows satus of units, if you press a button the status of the task will update if your name is like one of the roles for that task. however i don't want the status to downgrade, it needs to display the latest status. So what i wanted to do is like this
update status Set status=cleared where jobtask1=name or jobtask2=name AND status=in_progress OR status=in_wait
UPDATE data
SET status='cleared' WHERE person1='$name' OR person2='$name' OR person3='$name' OR person4='$name'
AND status='in_progress' OR status='preparation' OR status='in_wait' OR
status=''
So it results in checking if one of the persons is the person updating the status or if the status is like one of the specified statusses. I want it to check if the person is like the person updating the status, if so then check if the status is like one of the specified statusses and if both are correct update the status.

you could try to use SWITCH CASE logic
UPDATE data
SET status =
CASE
WHEN (person1='$name' OR person2='$name' OR person3='$name' OR person4='$name') THEN 'cleared'
WHEN (<second_condition>) THEN 'in_progress'
...
ELSE ''
END
You should also consider redesigning the DB according to normalization principles.

Thanks Salman and nacho for the solution, I just needed to add parenthesis.

Related

How can I show results in a select input, based on values shown in a seperate table?

I have three tables, and I'm just looking for a way to make this work.
tbl_campaigns has the columns "id" and "campaign". This one is fairly straight forward, it's just campaign names with an ID number that is auto-incremented so they have unique IDs.
tbl_users has an "id" column so each user has a unique ID number, standard stuff.
tbl_permissions creates a new row whenever a new user is created. This means its "id" column has unique ID values that match to the ID of a user in 'tbl_users'. The columns have been named to match the ID value of a campaign each time a new one is created, for example, the column "campaign_1" is relevant to the campaign in 'tbl_campaigns' with the ID of 1. The idea is this table data is filled with either 1's or 0's.
If a row with the ID of 1 has the number 1 for the column "campaign_1", then the user with the ID of 1 is approved for the campaign with the ID of 1 in the campaign table. If it were 0 then they're not approved for it. The same logic applies for columns "campaign_2", "campaign_3" etc..
Anyways, the issue I'm having is displaying this information on a front-end, as I only want the user to be able to see the campaigns they are approved to run in a drop-down list. When the user is logged in it stores their User ID in a session, I'm not sure if there's a way around it with this method.
Is there any way to get around this? Please note I've done this in procedural PHP as I'm still in my early days, so if anyone has a solution along these lines it would be much appreciated. Sorry if it's a little confusing. I am aware it's a bit ham-fisted, but I just want it to work first.
I believe that your schema needs to be improved, as the table structure should not have to change every time that you add a new campaign.
keep tables tbl_campaigns and tbl_users as they are
create table tbl_permissions with 4 fields (id, user_id, campaign_id and permission)
To check if a user has permission use a query like this:
SELECT permission FROM tbl_permissions WHERE user_id = ? AND campaign_id = ?
So, every time you create a campaign add a corresponding record to the tbl_permissions table. No need to add a new column.
I think the best practice to do this is as follows:
- Create HTML to show to the user(if you don't have it, let me know so i can work on one you can use)
- Create JS archive that will be in charge of calling PHP file and show the result in your HTML(if you don't know how to make it let me know so i can help you)
- Create PHP file, this is going to be in charge of consulting your data base and give the result disired for your select (if you don't know how to make it, let me know)
It is pretty easy to make this work, let me know if you need more help.

How can I prevent someone using a promo code twice?

I have a database that looks like this:
When user wants to use a promo code, it first checks if used_promo is null, that means the user has not used any promo code before. (User can only use 1 promo code)
I can do something like this
SELECT used_promo FROM users WHERE id=5
then I can check with PHP if used_promo is null and if it is, I can do
UPDATE users SET used_promo=2, balance=balance+100 WHERE id=5
Is it possible that the user can try to use the promo code multiple times at the same time, the SELECT will return that used_promo is null before it is set to 2 and the balance will be added multiple times?
In the scenario you've described it's theoretically possible to use the promo code multiple times. Use a single query, which will return an id if the update is successful or no rows otherwise.
UPDATE users
SET used_promo=2, balance=balance+100
WHERE id=5 AND used_promo is null
RETURNING id;
Create one more relational table "promo_used", where columns would be:
id
user_id
promo_code_id
timestamp
Once user apply promo code, this table will have new entry. This way, if user tries to apply same promo code again, we can check by searching through this table.
Hope this helps.
As understood you are worried that a user may use promo code multiple times before you update used_promo column. I don't know what your business model is. However, it is possible in a scenario like this:
User enters the promo code. You reduce the price of item accordingly, but you can't update the used_promo field since purchase is not complete. Without completing the purchase user enters the promo code for another product and gets a reduction again. And after doing this multiple time times user goes completing purchases. Since you don't check the validity of the code at the final step user can get all the price reductions.
So
Maintain the validity status of promo code
Always check the validity
Use atomic way to update validity status, used_promo and finalize the purchase. (Transactions)
of promo code and used_promo before finalizing purchase

Query Matching hasMany field in CakePHP

I can't find a working query that match my need.
I simply have a Bookings model and a Bookingsstatus :
Bookings hasMany Bookingsstatus
In that way, I can keep all changes to the status and when I want the status of a Booking, I just have to take the last associated entry.
BUT,
is there a query to get all Bookings currently matching a specific status ? (not a booking that has at some point the specific status)
I tried with matching but it's not working ... Does anyone has a lead ?
Thanks

all edits and additions must be approved by designated administrators/managers

ive a project in which a user can add record but that can published in the front end end only after it is verified and approved by admin. Please help me with the workflow and php code for this
here is a thought , because this is what i have implemented in my system
we have bunch of data entry guys , they add data . Then asks administrator to review once it done administrator publishes.
i have created one extra field flag and by default flag is 0 for newly entaied data.
once admin review the work he then updates the table setting flag to 1
That is not a problem. What you need to do is very simple.
First of all
You need to create a table in database which store all the data you want.
You need to create 1 more field which store any variable. Example ('0' or 'pending' as default ). Or you can store by using INSERT sql. What I mean is when you doing your form using php and then you insert the particular field as 0 or pending.
INSERT INTO table ('name', 'contact', 'field')VALUES ('$name','$contact','0/pending')
Create an admin site which use to verify and approve manually for the new user.
Do a transaction which can change the status of the field.
Update the field if admin approve to 1 or approved.
PS* if do not approve just let the field remain 0 or change the value to rejected.
Select the result by calling the specific value which according to the value you set in the field. For example you set the value is approved. Then what you should do is :
SELECT * FROM table WHERE field = approve
This should be what you want.
I can give you some raw steps.
Create a flag (column) is_published in you users table.
Set this flag default value as 0
Provide an option in the admin panel with approve or publish button.
On click of the above mentioned buttons run an ajax request update that particular column with value 1.
In the front end part list only those records where is_published = 1 or is_published != 0
Hope this can solve your problem.

How do I store order history in a database using php?

I have a site where users can place an order. The order goes through various stages before it is ready for delivery. I want to be able to log anytime anything happens relating to an order.
Here's an example of what I would like for it to look:
(2/13/12 4:41pm): Order initiated by Customer (2/13/12 4:41pm): Order
sent to Manager for approval (2/13/12 4:43pm): Order approved by
Manager (2/14/12 6:03pm): The order was edited by the user: City: 'Los
Angeles' to 'San Diego' (2/14/12 6:09pm): The order was edited by the
admin: Email: 'gearge#gmail.com' to 'george#gmail.com' (2/15/12
8:41pm): Order ready for Delivery
What is the best approach for me to store this type of order history? I have created a table in the DB called history where I would like to store the history as it pertains to each order. My columns are history_id(primary), order_id(foreign), date added(timestamp), and history_message(varchar).
Ideally I would like to create numbered codes for each step in the ordering process, such as an order approval, order edit, or order delivery and then just assign the number for that row instead of the actual history message, which is more characters. I feel that this way I won't overstuff my DB. The problem with this approach is that I would like to keep a log of the actual data that was changed(as you can see in the edits by the user and admin in the example) and I'm not sure how I can accomplish that without saving the complete history message for each row.
What is the best way to store order history in a database for my situation?
You can use trigger to update a child table keeping track of each order status.
Basically using insert trigger or do manual insert after update in order status by insert query you can copy required data from orders table to a new table we can call trackorders. tracking table will keep track of all changes and record with current status will be in main table.

Categories