Php 需要一个类似系统的帮手吗
以下是完整的代码:Php 需要一个类似系统的帮手吗,php,Php,以下是完整的代码: <?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 di
<?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'> $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" ";
$userid="$user-$id";
if("$fanname-$Posid" == $userid){echo"<td><p align='Left'><font size='2' color='black'>  $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'>  $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" ";
echo "</tr>";
mysql_close($conn);
?>
这段代码看起来很痛苦。还有很多事情你应该做的不同。首先,您应该避免使用mysql函数,因为它们已被弃用。使用mysqli或pdo。如果您选择mysqli,请确保采用面向对象的版本。我不确定“完整代码”是否是两个单独的脚本。。。如果不是,那么为什么要连接mysql和数据库两次呢?还有一种在if语句中检查某些内容的奇怪方式
一次只能看到1个like的原因是,在开始时,您将所有like从数据库中取出,然后执行while循环。然后将信息放入$fanname和$posid中。每个like都会更新这些值。因此,最终只会存储一个同类的信息
到目前为止,简单的修复方法是替换以下内容:
$fanname = $likehandle['Userlame'];
$posid = $likehandle['Post_ID'];
if("$fanname-$Posid" == $userid){
与
然后将其替换为:
$fanname = $likehandle['Userlame'];
$posid = $likehandle['Post_ID'];
if("$fanname-$Posid" == $userid){
与
这至少可以让你喜欢不止一件事
编辑:
对您的脚本还有一些建议:
似乎您从每个用户中选择了所有喜欢的内容。我相信那不是你真正想要的。您确实希望在查询中执行userlame={your_user_ID}的操作
执行此操作时,请更改喜欢的数组。您不需要使用fanname和posid创建密钥。只需创建一个以post_id为键的数组
然后,当您检查是否喜欢这篇新闻文章时,是否存在array\u key\u($id,$array\u of\u likes)。无需在其中发送您的用户名
但是更好的方法是在新闻查询中使用左连接查询。
大概是这样的:
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是空的。我知道代码太乱了,但这不是我使用的方式,我制作了一个测试php文件,然后清理干净,谢谢你的回答,我会尝试一下,非常感谢。@Abdel Moniemoragy我更新了一点我的帖子。更多关于更好剧本的建议。即使是更快的脚本,如果您应用查询的话。但是你可能需要做一些研究才能让它完全运行。请在发布代码之前介绍这个问题。这将使读者更容易理解这个问题。
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']