[英]How can I generate coding to only display what the user has posted in their profile
Within the user profile I am trying to only allow the user to see what they have posted to the database but with the coding this way it shows everything from the database in every users account. 在用户个人资料中,我试图仅允许用户查看他们发布到数据库的内容,但是通过这种编码方式,它可以显示每个用户帐户中数据库的所有内容。 How can I get it to only echo what the user has created. 我如何才能只回显用户创建的内容。
<?php
include("headers.php");
if($_POST['delete']) {
$title=$_POST['title'];
echo $title;
$result = mysql_query("DELETE FROM test2 where fname='$title'");
if($result)
echo "<div style='color:red;'>The ".$row['fname']." datas are deleted successfully.<br> <br></div>";
}
if($_POST['submit']) {
$title=$_POST['title'];
$result = mysql_query("SELECT * FROM test2 where fname='$title'");
$row = mysql_fetch_assoc($result);
@$image=$row ['photo'];
echo "<div style='width: 600px;border: 1px solid black;padding: 30px;float: center;text-align: center;margin: auto;margin-top: 140px;'> <span style=' font-size: 20px; font-weight: bold;'>Recent Post</span><br><br>
<table style='width:100%;border-bottom:1px solid gray;'><tr><td><table style='width:90%;'><tr><td>Name:</td><td>".$row['fname']."</td></tr>
<tr><td>Start Date:</td><td>".$row['stdate']."</td></tr>
<tr><td>End Date:</td><td>".$row['endate']."</td></tr>
<tr><td>Address:</td><td>".$row['addr1']."</td></tr>
<tr><td></td><td>".$row['addr2']."</td></tr>
<tr><td></td><td>".$row['city']."</td></tr>
<tr><td></td><td>".$row['state']."-".$row['zip']."</td></tr>
<tr><td>Description:</td><td>".$row['description']."</td></tr>
<tr><td>Link:</td><td><a href=".$row['link'].">".$row['link']."</a></td></tr>
</table></td><td><img src='image/".$image."' alt='image' style='width:100px;height:100px;'></td></tr></table><br/><br/><a href='index.php'>Go Back To Home </a></div>";
// echo "<div style='width:90%;float:center;border-bottom:1px solid blue;'></div>";
} else {
$result = mysql_query("SELECT * FROM test2");
echo "<div style='width: 600px;border: 1px solid black;padding: 30px;float: center;text-align: center;margin: auto;margin-top: 140px;'> <span style=' font-size: 20px; font-weight: bold;'>Recent Post</span><br><br>";
while($row = mysql_fetch_array($result))
{
@$image=$row ['photo'];
echo "<table style='width:100%;border-bottom:1px solid gray;'><tr><td><table style='width:90%;'><tr><td>Name:</td><td>".$row['fname']."</td></tr>
<tr><td>Start Date:</td><td>".$row['stdate']."</td></tr>
<tr><td>End Date:</td><td>".$row['endate']."</td></tr>
<tr><td>Address:</td><td>".$row['addr1']."</td></tr>
<tr><td></td><td>".$row['addr2']."</td></tr>
<tr><td></td><td>".$row['city']."</td></tr>
<tr><td></td><td>".$row['state']."-".$row['zip']."</td></tr>
<tr><td>Description:</td><td>".$row['description']."</td></tr>
<tr><td>Link:</td><td><a href=".$row['link'].">".$row['link']."</a></td></tr>
</table></td><td><img src='image/".$image."' alt='image' style='width:100px;height:100px;'></td></tr></table>";
}
echo "<br/><a href='index.php'>Go Back To Home </a></div>";
}
?>
It is uncertain but if you keep identity of user in session or in users profile you already know the identity of user so we can assume to get the identity of user through $_SESSION['userid'] , $_GET['userid'] or $_POST['userid'] . 不确定,但是如果您在会话中或用户个人资料中保留用户身份,则您已经知道用户身份,因此我们可以假设通过$ _SESSION ['userid'] , $ _GET ['userid']或$ _POST ['userid'] 。
$userId = $_SESSION['userid'];
OR 要么
$userId = $_GET['userid'];
whatever, you have to have user information to query from database. 无论如何,您必须具有用户信息才能从数据库中查询。 It can be user name or any info of an user instead of userid 它可以是用户名或用户的任何信息,而不是userid
$result = mysql_query("SELECT * FROM test2 where fname='$title' AND userid ='$userId'");
Also, you should have a look at preventing SQL Injection 另外,您应该看看防止SQL注入
Your question lacks a lot of information (such as your database schema). 您的问题缺少很多信息(例如您的数据库架构)。 But you will probably just need a WHERE
clause in your SQL which you specify the user. 但是您可能只需要在SQL中指定用户的WHERE
子句即可。 For example, 例如,
SELECT * FROM test2 WHERE fname='$title' and user=$userid
Or something of the like, depending on the schema. 或类似的东西,取决于模式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.