简体   繁体   中英

Multiple Comments are not showing, only the last comment- SQL database with php

I'm lost with this part... everything seems to work fine except - the comments are not "all" posting/showing on the page. Only the last comment shows. (comment "A" will disappear when I submit comment "B", comment "B" is the only one that shows, please help. Thank you!

<? include("connect.php"); ?>

<form method='post'>
  NAME: <input type='text' name='name' id='name' /><br />

  Email: <input type='text' name='email' id='email' /><br />

  Website: <input type='text' name='website' id='website' /><br />

  Comment:<br />
  <textarea name='comment' id='comment'></textarea><br />

<input type='hidden' name='articleid' id='articleid' value='<? echo $articleid["id"]; ?>' />
  <input type='submit' value='Submit'/>  
</form>

<? include("display_comments.php"); ?>

connect.php

<?
if( $_POST )
{
  $con = mysql_connect("localhost","....","...");

  if (!$con)
  {
    die('Could not connect: ' . mysql_error());
  }

  mysql_select_db("...", $con);

  $users_name = $_POST['name'];
  $users_email = $_POST['email'];
  $users_website = $_POST['website'];
  $users_comment = $_POST['comment'];

  $users_name = mysql_real_escape_string($users_name);
  $users_email = mysql_real_escape_string($users_email);
  $users_website = mysql_real_escape_string($users_website);
  $users_comment = mysql_real_escape_string($users_comment);

   $articleid = $_GET['id'];
  if( ! is_numeric($articleid) )
    die('invalid article id');

  $query = "
  INSERT INTO `db`.`comment` (`id`, `name`, `email`, `website`,
        `comment`, `timestamp`, `articleid`) VALUES (NULL, '$users_name',
        '$users_email', '$users_website', '$users_comment',
        CURRENT_TIMESTAMP, '$articleid');";

  mysql_query($query);

  echo "<h2>Thank you for your Comment!</h2>";

  mysql_close($con);
}

?> 

display.php

   <?

$con = mysql_connect("localhost","...","...");

if (!$con)
{
  die('Could not connect: ' . mysql_error());
}

mysql_select_db("...", $con);

$article_id = $_GET['id'];

if( ! is_numeric($article_id) )
  die('invalid article id');

$query = "SELECT * FROM `comment` WHERE `articleid` =$article_id LIMIT 0 , 30";


$comment = mysql_query($query);

echo "<h1>User Comments</h1>";


while($row = mysql_fetch_array($comment, MYSQL_ASSOC))
{
  $name = $row['name'];
  $email = $row['email'];
  $website = $row['website'];
  $comment = $row['comment'];
  $timestamp = $row['timestamp'];

  $name = htmlspecialchars($row['name'],ENT_QUOTES);
  $email = htmlspecialchars($row['email'],ENT_QUOTES);
  $website = htmlspecialchars($row['website'],ENT_QUOTES);
  $comment = htmlspecialchars($row['comment'],ENT_QUOTES);

  echo "  <div style='margin:30px 0px;'>
      Name: $name<br />
      Email: $email<br />
      Website: $website<br />
      Comment: $comment<br />
      Timestamp: $timestamp
    </div>
  ";
}

mysql_close($con);

?>

You use $comment in while the same you override $comment = mysql_query($query); change var in while

$one_comment = $row['comment'];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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