在php和mysql中为表单选择值会导致数据更改

yhqotfr8  于 2021-06-24  发布在  Mysql
关注(0)|答案(3)|浏览(257)

table

colorid -        1      2       3    4 
color            blue  yellow  green red 
currentcolor     yes    no     no    no

这是数据库。我有一个php代码,给你一个表单,选择颜色,你想使当前。它提供了颜色选项。一旦你选择了颜色,比如说在这个例子中,如果你选择绿色,并点击提交,它应该有值为绿色的currentcolor为是,并自动改变值为蓝色的currentcolor为否。。。如何在sql或php中实现这一点?现在我只有

update color (table) sent currentcolor = 'yes' where color = $info['color']

它将所选颜色的currentcolor值更改为yes,但仍保留前一种颜色的currentcolor值yes。谢谢!

guicsvcw

guicsvcw1#

将当前颜色的所有值设置为“否”,然后再将新颜色设置为“是”。

zazmityj

zazmityj2#

按照目前的结构,您有两种可能:

// set all colours to no before setting the current one:
update color set currentcolor = 'no'
// then
update color set currentcolor = 'yes' where color = $info['color']

// set those to NO, which are not the current colous
update color set currentcolor = 'no' where color != $info['color']

不管怎样,我想你还是去商店吧 currentcolor 在另一个表中,我们称之为meta

table META
item                value 
--------------------------
currentcolor        2
currentbackground   35
.
.
.
qxgroojn

qxgroojn3#

使用此更新语句可以解决您的目的:-

$sql = "update color
        set currentcolor = CASE
        WHEN color = '".$info['color']."' THEN 'yes'
        ELSE 'no'
        END" ;

相关问题