I'm working on a online voting system, I want to update the 'status' in my database every time the user vote, how can I do it?

huangapple go评论66阅读模式
英文:

I'm working on a online voting system, I want to update the 'status' in my database every time the user vote, how can I do it?

问题

表名 = 用户

编号 姓名 用户名 密码 状态
1 投票者1 投票者1 密码1 未投票
2 投票者2 投票者1 密码2 未投票

我尝试了

UPDATE users SET status='已投票' WHERE

我不知道在 "where" 里放什么

英文:

Table name = user

id name username password status
1 voter1 voter1 password1 Unvoted
2 voter2 voter1 password2 Unvoted

I tried

UPDATE users SET status='Voted' Where

idk what to put in "where"

答案1

得分: 2

你应该在 `WHERE` 子句中使用选民的身份证明来唯一标识投票者。
这可能会在你的 `PHP` 中设置为会话变量。
```sql
UPDATE user 
SET status = '已投票' 
WHERE id = $userID

你的 PHP 代码应该如下所示(为了安全性使用准备语句和变量绑定):

// 准备并绑定
$statement = $conn->prepare("UPDATE user SET status = '已投票' WHERE id = ?");
$statement->bind_param("i", $userID);

<details>
<summary>英文:</summary>

You should use the voter&#39;s ID in the `WHERE` clause to uniquely identify who voted.  
This will likely be set in your `PHP` as a session variable.  
```sql
UPDATE user 
SET status = &#39;Voted&#39; 
WHERE id = $userID

You should have your PHP look like this (for security using prepared statements and variable binding):

// prepare and bind
$statement = $conn-&gt;prepare(&quot;UPDATE user SET status = &#39;Voted&#39; WHERE id = ?&quot;);
$statement-&gt;bind_param(&quot;i&quot;, $userID);

huangapple
  • 本文由 发表于 2023年3月12日 10:16:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/75710722.html
  • php
  • sql
  • sql-update

如何在 :?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定