Update rows after mysql select foreach

你说的曾经没有我的故事 提交于 2021-01-28 21:01:47

问题


I have a select where I have 3 results:

$stmt = $handler->prepare("SELECT id,comments,likes,views FROM sites WHERE usr_id = '$usr_id'");
$stmt->execute();

After this select I have 3 results. Now I want in another table update or insert a new row for each result

This is my complete code

I don't have any update or new insert in table. Can anybody please help me?

  $stmt = $handler->prepare("SELECT id,comments,likes,views FROM sites WHERE usr_id = '$usr_id'");
  $stmt->execute();
  while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
    $rows[]=$row;
    foreach($rows as $row){
    $site_id = $row[id];    

            $stmt = $handler->prepare("SELECT id FROM session WHERE site_id = '$site_id' AND usr_id = '$usr_id'");
            $stmt->execute();
            $no=$stmt->rowCount(); 

            if ($no > 0)
            {
                $stmt = $handler->prepare("UPDATE session SET comments = '$comments' , likes = '$likes' , views = '$views'  WHERE usr_id = $usr_id AND site_id = $site_id");
                $stmt->execute();

            }
            else
            {
                $stmt = $handler->prepare("INSERT INTO session(user_id,site_id,comments,likes,views)VALUES('$user_id','$site_id','$comments','$likes','$views')");
                $stmt->execute();

            }
        }
  }

回答1:


@Miken32's answer would be the ideal way.

A direct fix to your code would be this way:

$stmt1 = $handler->prepare("SELECT id,comments,likes,views FROM sites WHERE usr_id = :usr_id");
$stmt1->bindValue(':usr_id', $usr_id);
$stmt1->execute();
while ($row = $stmt1->fetch(PDO::FETCH_ASSOC)) {
    $stmt2 = $handler->prepare("SELECT id FROM session WHERE site_id = :site_id AND usr_id = :usr_id");
    $stmt2->bindValue(':usr_id', $usr_id);
    $stmt2->bindValue(':site_id', $row['id']);
    $stmt2->execute();

    if ($stmt2->rowCount() > 0) {
        $stmt3 = $handler->prepare("UPDATE session SET comments = :comments , likes = :likes , views = :views  WHERE usr_id = :usr_id AND site_id = :site_id");
    } else {
        $stmt3 = $handler->prepare("INSERT INTO session(user_id,site_id,comments,likes,views)VALUES(:usr_id,:site_id,:comments,:likes,:views)");
    }
    $stmt3->bindValue(':comments', $row['comments']);
    $stmt3->bindValue(':likes', $row['likes']);
    $stmt3->bindValue(':views', $row['views']);
    $stmt3->bindValue(':usr_id', $usr_id);
    $stmt3->bindValue(':site_id', $row['id']);
    $stmt3->execute();
}

But this is not the best way to go about it. INSERT ...UPDATE ON DUPLICATE KEY would be better.




回答2:


First issue, you weren't taking advantage of prepared statements at all. Use parameters (the ? in the query) and then fill them with values in the execute() call.

Also, prepare your query outside a loop, and execute it inside. This is one of the key advantages of preparing statements in advance, there is less overhead when they are only prepared once.

Finally, there's no need for checking the database before your query and then executing one of two queries. Just let MySQL check if the value exists already with INSERT...ON DUPLICATE KEY UPDATE syntax. This relies on the database being set up properly, so there should be a UNIQUE index on (session.usr_id, session.site_id).

This is untested, but should get you going:

$stmt1 = $handler->prepare("SELECT id,comments,likes,views FROM sites WHERE usr_id = ?");
$stmt2 = $handler->prepare("INSERT INTO session SET comments = ?, likes = ?, views = ?, usr_id = ?, site_id = ? ON DUPLICATE KEY UPDATE comments = VALUES(comments), likes = VALUES(likes), views = VALUES(views)");

$stmt1->execute(array($usr_id));
while($row = $stmt1->fetch(PDO::FETCH_ASSOC)) {
    $site_id = $row["id"];
    $stmt2->execute(array($comments, $likes, $views, $usr_id, $site_id));
}


来源:https://stackoverflow.com/questions/34911793/update-rows-after-mysql-select-foreach

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!