我试图从表userid和votes中获取数据polls,然后更新users 表,但不知道如何做到这一点。
当我对i进行内爆时,我得到的用户i就像
293,934但是我不知道我能从表和更新用户表中得到所有的用户I和他们的选票.
$pollid = 0 + $_GET["pollid"];
$poll_res = mysql_query("SELECT userid FROM polls WHERE pollid = $pollid");
$idss = array();
while($poll_row = mysql_fetch_array($poll_res)) {
$idss[] = 0 + $poll_row['userid'];
}
$implode = implode(',', $idss);
$get_votes = mysql_query("SELECT votes FROM polls WHERE pollid = $pollid AND userid In($implode)");
// How I can update multilple user (rows) who are voted (found user votes and id from POLLS table) ?
// I just add $votes and $userid here so you know what I want to do....
mysql_query("UPDATE users SET total_votes = votes + $votes WHERE id = $userid");发布于 2022-09-20 18:18:29
Mysql在实际8.1版本之前被取消了几个版本,如果您有一些旧的php版本,您应该紧急更新它,然后使用更新的mysqli或pdo
对于sql注入问题,所有内容都在如何防止PHP中的SQL注入?中进行了解释,主页还提供了一些提示,https://www.php.net/manual/en/mysqli.quickstart.prepared-statements.php。
让我们假设您有以下数据库
CREATE TABLE polls(userid int,pollid INT, votes int);
INSERT INTO polls VALUES(1,1,1),(2,1,1),(3,1,0),(1,2,0),(2,2,0),(3,2,1);
CREATE tABLe users(id int, total_votes int);
INSERT INTO users VALUES (1,0),(2,0),(3,0);则只运行一个查询。
<?php
$poll = 1;
/* create a prepared statement */
$stmt = $mysqli->prepare("UPDATE users
JOIN ( SELECT userid ,votes FROM polls WHERE pollid = ?) t1
ON t1.userid = users.id SET users.total_votes = users.total_votes + t1.votes");
/* bind parameters for markers */
$stmt->bind_param("s", $poll);
/* execute query */
$stmt->execute();
/* fetch value */
printf("%d Row inserted.\n", $stmt->affected_rows);这个回来了。
2 Row inserted.只有2,因为用户id 3不更改total_votes,因为它只将0添加到当前值
https://stackoverflow.com/questions/73790642
复制相似问题