英文:
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'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 = 'Voted'
WHERE id = $userID
You should have your PHP look like this (for security using prepared statements and variable binding):
// prepare and bind
$statement = $conn->prepare("UPDATE user SET status = 'Voted' WHERE id = ?");
$statement->bind_param("i", $userID);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论