2015-07-18 71 views
-2

这里是全码:需要一个手,像系统

<?php 
    session_start(); 
    if(!isset($_SESSION["sess_user"])){ 
     header("location:../logout.php"); 
    } else {} 
    $user=$_SESSION['sess_user']; 
    ?> 

<?php 
    $con=mysql_connect('localhost','root','vertrigo') or die(mysql_error()); 
    mysql_select_db('user_registration') or die("cannot select DB"); 
    $sql=mysql_query("SELECT * FROM `likehand`"); 
    while($likehandle = mysql_fetch_array($sql)) 
      { 

       $fanname = $likehandle['Userlame']; 
       $Posid = $likehandle['Post_ID'];  

    } 
    ?> 

      <?php 
          echo "<div style='background-color: #CC98CC'>"; 
       echo "<tr align='center'bgcolor='#f0f0f0'>";  
       echo"<td><p align='center'><font size='1' color='black'><b>Welcome Back $user </b></a></font></p></td>"; 
       echo "</tr>"; 
       echo "</div>"; 

      echo"<p align='center'><i><b> 
<font style='opacity: 100' size='7' color='000000' face='forte'>News</font></b></i></p>"; 


      include("newsdb.php"); 

       if(isset($_POST['submit'])){ 
       if(!empty($_POST['search'])) { 
       $srhuser=$_POST['search']; 

$con=mysql_connect('localhost','root','vertrigo') or die(mysql_error()); 
mysql_select_db('user_registration') or die("cannot select DB"); 
$result=mysql_query("SELECT * FROM `news` where submitted_by='".$srhuser."'");}} 
else{$result=mysql_query("SELECT * FROM `news`\n" 
    . " ORDER BY `code` DESC"); 

} 

      while($test = mysql_fetch_array($result)) 
      { 

       $sbmitter = $test['submitted_by']; 
       $id = $test['code'];  
       $date = $test['date']; 
       $body = $test['body']; 
       $likes = $test['likes']; 
       $image = $test['url']; 
       $sql="SELECT * FROM `likehand`"; 

       echo "<div style='background-color: #C0C0C0'>"; 
       echo "<tr align='center'bgcolor='#f0f0f0'>";  
       echo"<td><i><left><font color='black'>&nbsp; &nbsp; &nbsp; $sbmitter Said:</center></font></i>"; 
       echo"<td><p align='center'><font size='4' color='black'>$body</a></font></p></td>"; 
       echo"<font color='Black'><p align='right'>created <a href='#'>";echo"<font color='blue'>"; 
       echo date('d-m-Y \a\t\ g:i <b>a</b></a>', strtotime($test["date"]));     // October 5, 2008 9:34 pm 
echo"&nbsp"; 
$userid="$user-$id"; 
       if("$fanname-$Posid" == $userid){echo"<td><p align='Left'><font size='2' color='black'>&nbsp&nbsp$likes like <a href ='unlike.php?code=$id'><font color='black'><input type='button' value='Unlike' name='B3' style='width: 50; height: 20'></a></a></font></p></td>"; 


}else{ echo"<td><p align='Left'><font size='2' color='black'>&nbsp&nbsp$likes like <a href ='like.php?code=$id'><font color='black'><input type='button' value='like' name='B3' style='width: 50; height: 20'></a></a></font></p></td>"; 
       }    if($sbmitter == $user) 
{    echo"<td><p align='right'><font size='2' color='black'><a href ='adminpannel/postdel.php?code=$id'><font color='black'><input type='button' value='delete' name='B3' style='width: 50; height: 20'></a></a></font></td>"; 
       echo"<td><font size='2' color='black'><a href ='/adminpannel/postedit.php?code=$id'><font color='black'><input type='button' value='edit' name='B3' style='width: 50; height: 20'></a></a></font></p></td>"; 
} 
} 


       echo "</div>"; 
       echo "<p></p>"; 
       echo"&nbsp;"; 



       echo "</tr>"; 

      mysql_close($conn); 
      ?> 

我认为这个问题是在这里,但不知道它是什么:

 $userid="$user-$id"; 
      if("$fanname-$Posid" == $userid){echo"<td><p align='Left'><font size='2' color='black'>&nbsp&nbsp$likes like <a href ='unlike.php?code=$id'><font color='black'><input type='button' value='Unlike' name='B3' style='width: 50; height: 20'></a></a></font></p></td>"; 


}else{ echo"<td><p align='Left'><font size='2' color='black'>&nbsp&nbsp$likes like <a href ='like.php?code=$id'><font color='black'><input type='button' value='like' name='B3' style='width: 50; height: 20'></a></a></font></p></td>"; 
       }    

我重新检查了数据库,并将其似乎它保存了正确的数据,问题是当用户按下像..喜欢字段增加一和帖子ID以及用户名被保存,但是当我喜欢多个帖子时,只有一个帖子显示“不同”按钮.. 有什么建议吗?

+1

请在发布代码前介绍问题。这会让读者更容易理解这个问题。 –

回答

0

这段代码真的很痛苦。还有很多事情你应该做得不同。首先你应该避免使用mysql函数,它们已被弃用。使用mysqli或者pdo。如果你使用mysqli,请确保采用面向对象的版本。我不确定'完整代码'是否是2个单独的脚本...如果不是,那么为什么要连接mysql和数据库两次?在if语句中检查某些事物的奇怪方法也是如此。

之所以你一次只看到1个,是因为在开始时,你将所有喜欢的东西从数据库中取出,然后做一段时间循环。然后你把信息放在$ fanname和$ posid中。每个喜欢都会更新这些值。所以最后只会存储一个像这样的信息。

简单的修复,截至目前,将取代这个:与

if(array_key_exists($userid, $array_of_likes)){ 

if("$fanname-$Posid" == $userid){ 

$fanname = $likehandle['Userlame']; 
$posid = $likehandle['Post_ID']; 

$array_of_likes[$fanname . "-" . $posid] = 1; 

然后更换此

这应该至少让你喜欢超过1件事。

编辑: 对你的脚本更多的建议: 看来你选择所有喜欢,从每个用户。我相信这不是你想要的。您确实想在查询中执行WHERE userlame = {your_user_ID}。

当你这样做,然后改变喜欢的数组。您不需要使用fanname和posid创建密钥。只需创建一个以post_id为关键字的数组。

然后,当你检查你是否喜欢这篇新闻发布时,请做array_key_exists($ id,$ array_of_likes)。无需发送您的用户名。

但是更好的方法是在新闻查询中使用LEFT JOIN查询。 事情是这样的:

SELECT n.*, l.POST_ID FROM news AS n 
    LEFT JOIN likehand AS l 
    ON l.userlame = $your_user_id AND l.Post_ID = n.code 
WHERE n.submittedby = $_POST['search'] 

我不能美化查询,但它是关于这一点。 它会从您搜索的人中选择所有的新闻消息。与此同时,它会检查是否存在与您匹配的帖子。如果你喜欢它,那么l.POST_ID是一个数字。如果你不喜欢它,l.POST_ID是空的。

+0

我知道代码太乱了,但它不是我使用的方式,我做了一个测试PHP文件混乱然后干净,谢谢你的答案,要尝试它 –

+0

工作,非常感谢。 –

+0

@ Abdel-MoniemMoragy我已经更新了我的文章。对更好的脚本提供更多建议。即使是更快的脚本,如果您应用查询。但是你可能需要做一些研究才能完全实现。 – Verkade89