Khekhekhe
Khekhekhe

Reputation: 53

Update a single value in a database table through form submission

Here is my table in the database :

 id  | account_name | account_number | account_type | address |            email             | ifsc_code | is_default_account |  phone_num  | User
-----+--------------+----------------+--------------+---------+------------------------------+-----------+--------------------+-------------+----------
 201 | helloi32irn  | 55265766432454 | Savings      |         | [email protected] | 5545      | f                  | 98654567876 | abc
 195 | hello        | 55265766435523 | Savings      |         | [email protected]         | 5545      | t                  | 98654567876 | axyz
 203 | what         | 01010101010101 | Current      |         | [email protected]         | 6123      | f                  | 09099990    | abc 

On form submission in the view, which only posts a single parameter which in my case is name= "activate" which corresponds to the column "is_default_account" in the table. I want to change the value of "is_default_account" from "t" to "f". For example here in the table, for account_name "hello" it is "t". And i want to deactivate it, i.e make it "f" and activate any of the other that has been sent trough the form

Upvotes: 0

Views: 100

Answers (2)

bladmiral
bladmiral

Reputation: 225

I think to accomplish what you want to do you should send at least two values from the form. One for the id of the account you want to update and the other for the action (activate here). You can also just send the id and have it toggle. There are many ways to do this but I can't figure out exactly what you are trying to do and whether you want SQL or Playframework code. Without limiting your update in somewhere (like id) you can't precisely control what specific rows get updated. Please clarify your question and add some more code if you want help on the playframework side, which I would think you do.

Upvotes: 0

mvp
mvp

Reputation: 116297

This will update your table and make account 'what' default (assuming that is_default_account is BOOLEAN field):

UPDATE table
SET is_default_account = (account_name = 'what')

You may want limit updates if table is more than just few rows you listed, like this:

UPDATE table
SET is_default_account = (account_name = 'what')
WHERE is_default_account != (account_name = 'what')
  AND <limit updates by some other criteria like user name>

Upvotes: 1

Related Questions