Question

I fetch data from MySQL inside a while loop. When user click on any record, I display its detail in a Color Box i.e id = 2. Now Inside this color box, I want to run one query i.e WHERE id = 2 . The problem I'm facing is that my code execute all queries at once as the color box is inside the while loop. How can I run MySQL query outside the while loop as I cant access id outside of the loop. please check my code below,

<?php
$sql_msg = "SELECT * FROM messages";
$res_msg = mysql_query($sql_msg);
while($row = mysql_fetch_array($res_msg))
{
$id = $row['id'];
$title = $row['title'];
echo "<a href='#$id'><li>$title</li></a>";
?>

<div style='display:none'> // Color box popup window
 <div id='<?php echo $id; ?>'>

<?php
$Update_msg = mysql_query("UPDATE `messages` SET `read` = 1 WHERE `id` = $id AND `read` = 0");
?>

 </div>
</div>

<?php } ?> // While Loop End

I just want to Update the record of the popup window data

Was it helpful?

Solution 4

I run query in another page and pass ID through ajax and got my solution

<script>
function myFunction(msg)
{
    $.ajax
    ({
    type: "POST",
    url: "updatefile.php?id="+msg
    });
}
</script>

OTHER TIPS

put:

$Update_msg = mysql_query("UPDATE `messages` SET `read` = 1 WHERE `id` = $id AND `read` = 0");

outside of the while loop. When a click is made, use ajax to call the php file and make the update.

This would work. But it would reload the whole page and is just an outlining of what you should/could do.

Do not use this code unreviewed in your system, if it is in any kind public accessible.

You should also consider using mysqli and change the code as you want for ajax request. You should also make sure the files to update the database are properly secured, if they are somehow public.

messages.php (assuming that your file is named like that):

<ul>
<?php
  $sql_msg = "SELECT * FROM messages";
  $res_msg = mysql_query($sql_msg);

  // Using ":" syntax is common practice while mashing up
  // php and html source code.
  while ($row = mysql_fetch_assoc($res_msg)):
    $id = $row['id'];
    $title = $row['title'];
    // I only assume, that this column contains
    // your details.
    $detail = $row['detail'];

    // Update the message and mark it as read. The update query runs
    // against the server anyway, so the "`read` = 0" is unncessary
    // in terms of performance.
    $update_msg = mysql_query("UPDATE `messages` SET `read` = 1 WHERE `id` = " . $id);
  ?>
  <li>
    <a href="#<?php echo $id; ?>"><?php echo $title; ?></a>
  </li>

  <div style='display:none'><!-- Color box popup window -->
    <div id='<?php echo $id; ?>'>
    <?php
      echo $detail;
    ?>
    <a href="mark_as_unread.php?id=<?php echo $id; ?>">Mark as unread</a>
    </div>
  </div>
<?php
  endwhile; // While Loop End
?>
</ul>

_mark_as_unread.php:

<?php
  $id = isset($_GET['id']) ? intval($_GET['id']) : NULL;

  if (!$id) {
    echo 'Bad request.';
    exit;
  }

  $sql = "UPDATE `messages` SET `read` = 0 WHERE `id` = " . $id;
  $result = mysql_query($sql);

  echo ($result ? 'Success.' : 'Error.');
?>

look at it:

send post variable $id to php file

https://api.jquery.com/jQuery.post/

php file get $id from page and then update in mysql database and return true from file http://pl1.php.net/manual/en/reserved.variables.get.php

and when return a result data from php file update class CSS

api.jquery.com/addclass/

Good luck

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top