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评论95阅读模式
英文:

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

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

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

  1. // 准备并绑定
  2. $statement = $conn->prepare("UPDATE user SET status = '已投票' WHERE id = ?");
  3. $statement->bind_param("i", $userID);
  1. <details>
  2. <summary>英文:</summary>
  3. You should use the voter&#39;s ID in the `WHERE` clause to uniquely identify who voted.
  4. This will likely be set in your `PHP` as a session variable.
  5. ```sql
  6. UPDATE user
  7. SET status = &#39;Voted&#39;
  8. WHERE id = $userID

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

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

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

发表评论

匿名网友

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

确定