单击链接上的Sql UPDATE

时间:2013-11-21 20:40:41

标签: php mysql sql-update

我正在尝试在用户点击链接时更新一个值。

<?php
$data = mysql_query("SELECT * FROM comments a LEFT JOIN pins b ON a.pin_id = b.id INNER     JOIN board c on b.board_id = c.id WHERE a.to_id = '$myid' AND a.status = 'unviewed'") 
or die(mysql_error()); while($info = mysql_fetch_array( $data )) 
{ 
Print "<li>";
Print "<a href='/board/pins/".$info['board_id']."/".$info['pin_id']."'>";
Print "<img src='".$info['pin_url']."' width='50' align='left'>";
Print "<font size='1'>comment received on ".$info['date']."</font></a>";
Print "Collection: ".$info['board_name']."</li>";
} 
?>

链接就是上面的链接。

如何在点击链接时编辑上述代码以包含更新查询"UPDATE comments SET status='viewed' WHERE to_id = '$myid' AND id='$postid'"

编辑:

这是我的mark_viewed.php:

<?php
$con=mysqli_connect("XXX","XXX","XXX","XXX");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

// get values sent from address bar
$myid=$_GET['myid'];
$postid=$_GET['postid'];

mysqli_query($con,"UPDATE comments SET status='viewed' WHERE to_id ='$myid' AND id='$postid'");
mysqli_close($con);
?>

我的发送页面:

<?php  // display of the notifications dropdown menu
$query_select = "SELECT * FROM comments a LEFT JOIN pins b ON a.pin_id = b.id LEFT JOIN     board c on b.board_id = c.id WHERE a.to_id = '$myid' AND a.status = 'unviewed' ORDER BY     a.date DESC";
$result_select = mysql_query($query_select) or die(mysql_error());
$rows = array();
while($row = mysql_fetch_array($result_select))
    $rows[] = $row;
foreach($rows as $row){ 
    $myid = $row['user_id']; // my id
    $name = $row['board_name']; // collection name
    $boardid = $row['board_id']; // collection id
    $postid = $row['pin_id']; // post id
    $url = $row['pin_url']; // image url
    echo "<li><a href='/board/pins/$boardid/$postid' data-myid='$myid' data-    postid='$postid' class='markviewed'>";
    echo "<img src='$url' height='50' width='50'>";
    echo "New comment in $name.";
    echo "</a></li>";
}
?>

我在相同页面的头部有JS,但它不起作用。此页面$myid$postid上的变量很好,但它们不会在mark_viewed.php上发送/接收

1 个答案:

答案 0 :(得分:0)

尝试此代码(评论中建议的AJAX)。它假设它基本上是做什么:

  1. 将相关的ID打印为html作为属性。
  2. 将侦听器绑定到这些链接上的click事件
  3. 在ping http://domain.com/mark_viewed.php(查询更新评论的查询文件)后,它将浏览器重定向到所需位置(/ board / pins /...)
  4. //html part
    <?php
    $myid = 1234;
    $postid = 333;
    //echo prepared link with ids in it's data-attributes
    echo '<a href="/board/pins/"'.$info['board_id'].'/'.$info['pin_id'].'"  
       data-myid="' . $myid . '" 
       data-postid="' . $postid . '" 
       class="markviewed">mark ids as viewed and go to /board/pins/...</a>';
    ?>
    
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
    <script>
    $(document).ready(function() {
       //bind a listener to "click" event on links with class "markviewed"
       $('a.markviewed').click(function(event) {
    
          //prevent default behavior just in case
          event.preventDefault();
    
          //get ids from clicked <a>
          var myid = $(this).attr('data-myid');
          var postid = $(this).attr('data-postid');
    
          //ping the address to mark clicked link as viewed
          $.ajax('http://domain.com/mark_viewed.php?myid=' + myid + '&postid=' + postid);
    
          //redirect to the link in the href attribute
          window.location.href = $(this).attr('href');
    
       });
    });
    </script>