【问题标题】:Run MySql Query when color box open打开颜色框时运行 MySql Query
【发布时间】:2014-03-03 21:16:21
【问题描述】:

我在一个 while 循环中从 MySQL 获取数据。当用户单击任何记录时,我会在颜色框中显示其详细信息,即 id = 2。现在在此颜色框中,我想运行一个查询,即 WHERE id = 2。我面临的问题是我的代码一次执行所有查询,因为颜色框位于 while 循环内。如何在 while 循环之外运行 MySQL 查询,因为我无法在循环之外访问 id。请检查我下面的代码,

<?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

我只想更新弹窗数据的记录

【问题讨论】:

  • 此代码按原样损坏 - 您在调用 echo 后缺少 ?&gt;
  • @MarcB 我更新了我的问题
  • 如果人们从不使用数字索引,为什么还要使用mysql_fetch_array?应该改用mysql_fetch_assoc
  • 你可以通过 ajax 做到这一点
  • @RobertRozas 我无法在 while 循环之外访问 $id。能否请您给我写一个如何通过 Ajax 实现的简单示例

标签: php mysql colorbox


【解决方案1】:

放:

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

在while循环之外。单击时,使用 ajax 调用 php 文件并进行更新。

【讨论】:

  • 我无法在WHILE LOOP之外访问$id
  • 是的,你可以: div id=''>从div的id中获取
  • 对不起,我不明白,它总是运行第一个 id
【解决方案2】:

这会奏效。但它会重新加载整个页面,并且只是对您应该/可以做的事情的概述。

不要在您的系统中使用未经审查的代码,如果它是任何公共可访问的。

您还应该考虑使用 mysqli 并根据需要更改 ajax 请求的代码。 您还应该确保更新数据库的文件得到适当保护,如果它们是公开的。

messages.php(假设你的文件是这样命名的):

<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.');
?>

【讨论】:

    【解决方案3】:

    看看:

    将发布变量 $id 发送到 php 文件

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

    php 文件从页面获取 $id 然后在 mysql 数据库中更新并从文件返回 true http://pl1.php.net/manual/en/reserved.variables.get.php

    当从 php 文件更新类 CSS 返回结果数据时

    api.jquery.com/addclass/

    祝你好运

    【讨论】:

      【解决方案4】:

      我在另一个页面中运行查询并通过 ajax 传递 ID 并得到我的解决方案

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

      【讨论】:

        猜你喜欢
        • 2017-08-08
        • 1970-01-01
        • 2018-10-31
        • 2014-08-13
        • 2017-08-06
        • 2012-06-07
        • 1970-01-01
        • 1970-01-01
        • 2012-11-15
        相关资源
        最近更新 更多