Andi Wyder
Andi Wyder

Reputation: 95

Update rows after mysql select foreach

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();

            }
        }
  }

Upvotes: 1

Views: 1097

Answers (2)

meda
meda

Reputation: 45490

@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.

Upvotes: 0

miken32
miken32

Reputation: 42719

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));
}

Upvotes: 1

Related Questions