随机发布的动态链接与帖子ID不一致

时间:2014-12-26 16:53:09

标签: php mysql pdo

下面的代码应该从我的表中显示一个随机的帖子;它会这样做,但它显示的帖子与ID中的ID不同。

如何让它显示网址中的内容?感谢。

$db = new PDO('mysql:host=localhost;dbname=db;charset=utf8', 'user', 'pass');           
              $sql = $db->query('SELECT id FROM posts ORDER BY RAND() LIMIT 1');
              $row = $sql->fetch(PDO::FETCH_ASSOC);
echo "<a href='random?page=".$row['id']."'>Randomize!</a><br>";

请在下面找到'random.php'页面的相关部分:

$db = new PDO('mysql:host=localhost;dbname=db;charset=utf8', 'user', 'pass');
$sql = $db->query('SELECT * from posts ORDER BY RAND() LIMIT 1');

              $row = $sql->fetch(PDO::FETCH_ASSOC);
              echo "<a href = 'random.php?page=".$row['id']."'>Re - Randomize!</a><br>";
              if ($row['sort'] == "image"){
              echo "<b>Update: </b>", $row['tag'], "<br>";
              echo "<img src='resize.php?w=240&img=./uploads/".$row['message']."'><br>";
              echo "<b>", "By: ", "</b>", ($row['username']), "<br>"; 
              echo "<b>", "At: ", "</b>", ($row['timestamp']), "<br>";
              if($row['locked'] !='locked'){
              echo "<a href ='edit.php?pid=".$row['id']."&login=true&edit=true' method='post' id ='form-id' data-ajax='false' '><b><span class = 'small_blue'>EDIT</span></b></a>";}
              echo "&nbsp;", "&nbsp;";
              echo "<span class = 'small_green'>", ($row['edited']), "</span>", "&nbsp;", "&nbsp;", ($row['locked']);

              echo "&nbsp;", "&nbsp;";

              }

              else if ($row['sort'] == "audio"){            
              echo "<b>Update: </b>", $row['tag'], "<br>";
              echo "<audio controls>";
              echo "<source src='./uploads/".$row['message']."'>";
              echo "</audio>", "<br>";
              echo "<b>", "By: ", "</b>", ($row['username']), "<br>"; 
              echo "<b>", "At: ", "</b>", ($row['timestamp']), "<br>";  
              if($row['locked'] !='locked'){
              echo "<a href ='edit.php?pid=".$row['id']."&login=true&edit=true' method='post' id ='form-id' data-ajax='false' '><b><span class = 'small_blue'>EDIT</span></b></a>";}
              echo "&nbsp;", "&nbsp;";
              echo "<span class = 'small_green'>", ($row['edited']), "</span>", "&nbsp;", "&nbsp;", ($row['locked']);                           
              }

              else if ($row['sort'] == "video"){            
              echo "<b>Update: </b>", $row['tag'], "<br>";
              echo "<video controls>";
              echo "<source src='./uploads/".$row['message']."'>";
              echo "</video>", "<br>";
              echo "<b>", "By: ", "</b>", ($row['username']), "<br>"; 
              echo "<b>", "At: ", "</b>", ($row['timestamp']), "<br>";
              if($row['locked'] !='locked'){
              echo "<a href ='edit.php?pid=".$row['id']."&login=true&edit=true' method='post' id ='form-id' data-ajax='false' '><b><span class = 'small_blue'>EDIT</span></b></a>";}
              echo "&nbsp;", "&nbsp;";
              echo "<span class = 'small_green'>", ($row['edited']), "</span>", "&nbsp;", "&nbsp;", ($row['locked']);
              }

              else {
              echo "<b>Update:</b><br>";
              echo ($row['message']), "<br>";
              echo "<b>", "By: ", "</b>", ($row['username']), "<br>"; 
              echo "<b>", "At: ", "</b>", ($row['timestamp']), "<br>";
              if($row['locked'] !='locked'){
              echo "<a href ='edit.php?pid=".$row['id']."&login=true&edit=true' method='post' id ='form-id' data-ajax='false' '><b><span class = 'small_blue'>EDIT</span></b></a>";}
              echo "&nbsp;", "&nbsp;";
              echo "<span class = 'small_green'>", ($row['edited']), "</span>", "&nbsp;", "&nbsp;", ($row['locked']);
                               }

2 个答案:

答案 0 :(得分:0)

如果我没有弄错,你想要在设置页面时检索帖子,否则随机化?

如果那是你想要的,你可以尝试这个(假设页面是整数,id是唯一的):

$id = filter_input(INPUT_GET, 'page', FILTER_SANITIZE_NUMBER_INT);
$q = ($id) ? "SELECT * from posts WHERE id=$id" : "SELECT * from posts ORDER BY RAND() LIMIT 1";
$db->query($q);

然后重新随机化只需链接到没有参数的页面:

echo "<a href='random.php'>Re - Randomize!</a><br>";

希望它有所帮助。

亲切的问候。

答案 1 :(得分:0)

我的random.php页面缺少$ _GET:

$tbl='posts';
          $id=$_GET['page'];

          $db = new PDO('mysql:host=localhost;dbname=db;charset=utf8', 'user', 'pass');
          $sql = $db->query("SELECT * from $tbl WHERE id = ".$_GET['page']." ");
              $row = $sql->fetch(PDO::FETCH_ASSOC);

全部谢谢!