从mysql表发布的mysql / php echo?

时间:2013-11-23 21:23:55

标签: php

我正在创建一个网站,您可以在其中发布内容并撰写正在发生的内容,并为您提供类似墙的内容。你可以发布东西。在你的墙上是人们发布的所有东西(现在它只是你发布的东西)。我能够回应你发布的内容,而不是发布的内容。 EG:如果我发布“大家好”,我希望它能显示谁发布了它。我尝试更改我的代码,但没有运气,所以还原它。顺便说一下,我十二岁,所以我的一些编码不会很完美:D这是代码。

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");
}
}``
?>

2 个答案:

答案 0 :(得分:0)

首先,您需要在隐藏表单字段中发布用户名才能执行此操作。然后,只有您可以获得已发布该用户名的用户名。

在表单标记

之间使用以下内容

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

答案 1 :(得分:0)

问题在于$user是吗?我看到第一次使用变量$user

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

没有它实际上首先被声明。因此,您的代码无法从数据库中检索$user,因为它未定义。此外,它无法在数据库中插入任何内容,因为它未定义。找出用户的名字(可能通过使用会话),将其分配给$ user,然后您可以发布并检索其值。