php - 是 php-mysql : update column with different values depending on condition involving PHP variable

标签 php mysql if-statement mysqli sql-update

我有一个带有字符串变量 $participant1, participant2 的网络表单,和$winner在 php 中。当发布表单时,它会更新列 participantwinner对于 my_table 中的两行。 winner 的列类型是 BOOLEAN .

这是我的表格当前的样子:

ID |Game |Participant | Winner
-------------------------------
1     1     John          NULL
2     1     Frank         NULL

提交的变量$winner将是 participant 的名称(“约翰”或“弗兰克”)。提交后,我想要 1为每个参与者设置字符串 $winner火柴。和0对于其他参与者。

即如果$participant1 =='John' and $winner=='John' 那么表格应该如下所示:

ID |Game |Participant | Winner
-------------------------------
1     1     John          1
2     1     Frank         0

我似乎无法弄清楚这部分。

我尝试过:

$participant1= mysqli_escape_string( $con, $params['participant1']);
$participant2= mysqli_escape_string( $con, $params['participant2']);
$Winner= mysqli_escape_string( $con, $params['Winner']);    

mysqli_query($con, "UPDATE my_table SET Winner = IF('$Winner'=='$participant1',1,0) WHERE Participant ='$participant1');

mysqli_query($con, "UPDATE my_table SET Winner = IF('$Winner'=='$participant2',1,0) WHERE Participant ='$participant2'");

Winner仍然显示NULL然后。有没有主要的 MySQL 方法来做到这一点?

最佳答案

嘿,将 == 替换为 =

mysqli_query($con, "UPDATE my_table SET Winner = IF('$Winner'='$participant1',1,0) WHERE Participant ='$participant1'");

测试代码:

$mysqli = new mysqli("localhost", "root", "", "test");
$a='John';
$b='Mary';

$w='John';

mysqli_query($mysqli, "UPDATE myguest SET Winner = IF('$a'='$w',1,0) WHERE FirstName ='$a'");
mysqli_query($mysqli, "UPDATE myguest SET Winner = IF('$b'='$w',1,0) WHERE FirstName ='$b'");   

关于php - 是 php-mysql : update column with different values depending on condition involving PHP variable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54893410/

相关文章:

javascript - 需要检查与点击元素相关的隐藏输入字段的值(多个同名)

php - 从 paypal 返回后 session 被销毁

php - 使用 MySQL PHP 进行基于时间的 IP block

c - 删除字符串中的一个字符?

r - 有条件地从数据框中删除行

javascript - `var` 中的 `if` 是否会影响 if 是否被评估?

PhpStorm 忽略项目中的 PHP 版本设置

php - 共享主机中的安全性

php - CodeIgniter 中有类似 $this->db->matched_rows() 的东西吗?

mysql - 如何在MYSQL中查询具有相同时间戳的记录?