简体   繁体   English

从MySQL表发布的MySQL / PHP的回声?

[英]mysql/php echo posted by from mysql table?

I'm creating a website where you can post things and write what's going on, and you have a wall-like thing for you. 我正在创建一个网站,您可以在该网站上发布内容并写出正在发生的事情,并且您会遇到一堵墙一样的东西。 You can post things. 您可以发布内容。 On your wall are all the things people have posted (for now it's just the things you have posted). 人们在墙上张贴的所有内容(目前只是您已张贴的内容)。 I am able to make it echo the things that you have posted, but not who it's posted by. 我能够使它与您发布的内容相呼应,但与谁发布的内容无关。 EG: If I post "hi everyone" I want it to show who posted it. EG:如果我张贴“嗨,大家好”,我希望它显示谁张贴。 I tried changing my code, but had no luck, so reverted it back. 我尝试更改代码,但没有运气,因此将其还原。 By the way, I'm twelve, so some of my coding won't be perfect :D Here's the code. 顺便说一句,我十二岁,所以我的一些编码并不完美:D这是代码。

Template.php (place where users can post things) Template.php(用户可以在其中发布内容的地方)

<?php include("header.php"); ?>
<html>
    <head>
        <link type="text/css" rel="stylesheet" href="stylesheet.css"/>
        <title>Simul</title>
    </head>
    <body style="overflow-y:hidden; overflow-x:scroll">
    <hr id="divider">
    <br>
    <br><br>
    <form action='write.php' method='POST' style="position: fixed" name="content">
    <h2 style="position:fixed">What's Going On?<br><br><textarea rows="8" cols="30" style="resize:none" name="content"></textarea>
    <input type="submit" id="cb3" value="Post" name="post">
    </form>
    <?php 
    $channel_check = mysql_query("SELECT content FROM wgo WHERE Posted_By='$user' ORDER by `date` DESC;");
    $numrows_cc = mysql_num_rows($channel_check);
    if ($numrows_cc == 0) {
    echo ''; // They don't have any channels so they need to create one
    ?>
    <h4> &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspYou haven't posted anything yet. You can post what's going on in your life, how you're feeling, or anything else that matters to you.</h64>
 <?php
}
else
{
?>
<div id="recentc">
<?php
echo"<h2 id='lp'> Latest Posts</h2>";
  while($row = mysql_fetch_assoc($channel_check)) {
  $channel_name = $row['content'];
 ?>
 <div>
  <?php echo "<b><h6 id='lp2'> $channel_name</h6></b><p />";
 }
}
?>
</div>
    </body>
</html>
   write.php 
    <?php include 'header.php'?>
<link type="text/css" rel="stylesheet" href="stylesheet.css"/>
<?php if (@$_POST['post']) {
$Posted_By = $user;
$content = $_POST['content'];
$date = date("Y-m-d");
if ($content == '') {
echo "Write What is Going On";
}
else {
$create_a_post = mysql_query("INSERT INTO wgo VALUES ('','$date','$Posted_By','$content')");
header ("Location:template.php");
}
}``
?>

First of all you need to post username in hidden form field for doing this .Then only you can get username of the person who have posted it. 首先,您需要在隐藏的表单字段中发布用户名。然后,只有您可以获取发布该文件的人的用户名。

Use the following in between form tag 在表单标签之间使用以下内容

<input type="hidden" id="user" name="user" value="<?php $username; ?>">

Your problem is with $user is it? 您的问题出在$user上吗? I see the variable $user is used the first time on the line 我第一次看到变量$user被第一次使用

 $channel_check = mysql_query("SELECT content FROM wgo WHERE Posted_By='$user' ORDER by `date` DESC;"); 

without it actually being declared at first. 最初并没有实际声明它。 So your code cannot retrieve $user from the database since it is undefined. 由于您的代码未定义,因此您的代码无法从数据库中检索$user Also, it cannot insert anything into the database because it is undefined. 同样,它无法将任何内容插入数据库,因为它是未定义的。 Find out the user's name (maybe through the use of session), assign it to $user and then you can post and retrieve its value. 找出用户名(也许通过使用会话),将其分配给$ user,然后就可以发布并检索其值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM